ORDER BY & Sorting SQL Topic exerciseHardVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Build a Multi-Rule Employee Directory

Return the employee directory using the requested IT, HR, Sales priority; senior-before-2020 placement; salary descending; and last_name ascending.

  • CASE expressions
  • Sorting

Exercise brief

Understand the request

People operations lead An executive directory combines custom department priority, seniority, compensation, and alphabetical fallback rules.

Build an employee directory sorted as follows: 1. Department in custom order (IT first, then HR, then Sales) 2. Senior employees first within each dept (hired BEFORE 2020-01-01) 3. Then salary descending 4. Then last_name ascending Show first_name, last_name, hire_date, salary, department_id, dept_name.

Return

  • Return first_name, last_name, hire_date, salary, department_id, dept_name in this exact left-to-right order.

Constraints

  • Use CASE for the custom department and seniority ranks.
  • Keep salary and last_name as later tie-breakers.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • first_nameTEXT
  • last_nameTEXT
  • hire_dateDATE
  • salaryDECIMAL
  • department_idINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

Tackle one sort key at a time. Translate each English bullet into either a column or a CASE expression.

Hint 2

Ranking groups (IT/HR/Sales) → CASE returning 1/2/3. Boolean test (senior?) → CASE returning 0/1. Numeric/text columns → as-is with ASC or DESC.

Hint 3

ORDER BY CASE dept-rank, CASE senior-flag, salary DESC, last_name

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT first_name, last_name, hire_date, salary, department_id, CASE department_id WHEN 1 THEN 'IT' WHEN 2 THEN 'HR' ELSE 'Sales' END AS dept_name FROM employees ORDER BY CASE department_id WHEN 1 THEN 1 WHEN 2 THEN 2 ELSE 3 END, CASE WHEN hire_date < '2020-01-01' THEN 0 ELSE 1 END, salary DESC, last_name;

Why this works

Real-world ORDER BY clauses chain a custom group rank, a boolean flag, then conventional sorts. The explicit CASE form (rather than `hire_date < '2020-01-01' DESC`) is portable to SQL Server, where boolean-as-sort-key is rejected.

Success check

Every employee appears once and the final sequence satisfies all four business rules.

Expected result

Use this output to verify values, aliases, ordering, and row count.

first_namelast_namehire_datesalarydepartment_iddept_name
AlexMiller2019-08-14720001IT
MikeJohnson2021-06-10800001IT
AmyTaylor2021-12-03780001IT
JohnDoe2020-01-15750001IT
DavidBrown2022-02-28700001IT
JaneSmith2019-03-22550002HR
LisaDavis2020-09-12580002HR
EmmaThomas2023-01-10520002HR
SarahWilliams2018-11-05650003Sales
TomWilson2019-07-18620003Sales

Previewing 10 of 12 expected rows. Run the query in the editor to inspect the full result.

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.