Skip to main content
SQL โ€ข LESSON 111

CASE in ORDER BY

How can we prioritize certain departments in the result?

Advancedโฑ 3 Minutesโญ 1240 XP
๐Ÿค” THE QUESTION

How can we prioritize certain departments in the result?

๐Ÿ’ก WHAT IS IT?

CASE inside ORDER BY assigns custom priority sorting integers to specific categorical values.

๐ŸŽฏ WHAT IS IT USED FOR?

Custom sorting hierarchies, putting high-priority tickets or core departments first.

๐Ÿ’ป EXAMPLE
SELECT name, department
FROM employees
ORDER BY CASE
    WHEN department = 'Engineering' THEN 1
    WHEN department = 'Analytics' THEN 2
    ELSE 3
END;

๐ŸŽฏ Mission Objectives

Practice typing production-grade SQL code for CASE in ORDER BY.

  • โœ“ CASE in ORDER BY
  • โœ“ Custom sort priority
  • โœ“ Non-alphabetical ordering
  • โœ“ Priority sorting