I took this query from this question.
SELECT *
FROM A
WHERE x='abc'
OR y=0
order by case when x='abc' then 0 else 1 end;
This query supposedly will prioritize x='abc' cases. But I'm really confused why is this happening? Isn't ORDER BY followed by a column name or column number? Also, I researched on the syntax of ORDER BY and they don't tell anything about this. I also tried something like this but it says: "1st ORDER BY term out of range - should be between 1 and 1":
SELECT A
FROM B
ORDER BY 2
So, can anyone explain this query or at least point to a good documentation? Thank you very much.
The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:
SELECT
*
FROM
A
WHERE
A.x = 'abc'
OR A.y = 0
ORDER BY
CASE
WHEN A.x = 'abc' THEN 0
ELSE 1 END