Mr. A Mr. A - 15 days ago 7
MySQL Question

How does the ORDER BY CASE Clause Work in the following Code?

How does this part of code work?

ORDER BY
CASE WHEN subject IN ('Chemistry','Physics') THEN 1 ELSE 0 END,
subject,
winner;

Answer

First it checks if subjet is Chemistry or Physics. If it does the sorting score will be 1.

If the subject does not contain any of "Chemistry" or "Physics" the score will be 0.

After that it sorts the results by score so the results that contains Chemistry or Physics will come first after that those that dont contain.

Comments