Skip to main content
SQL • LESSON 111

CASE in ORDER BY

How can we prioritize certain departments in the result?

Advanced3 Minutes1240 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