SQL Topic collection

ORDER BY & Sorting SQL Topic exercises

Practice deterministic SQL ordering with tie-breakers, custom priorities, NULL placement, expressions, joined data, aggregates, and portable top-N patterns.

Exercises
15
Difficulty
Beginner to Advanced

What this collection tests

Skills and query patterns

ASC and DESC, multi-column ordering, deterministic ties, CASE priorities, NULL placement, collation-aware sorting, and top-N

Curated exercises

Choose an exercise

Work in the live editor where supported. Advanced guided labs state their engine boundary before you open the workspace.

  1. Exercise 1Easy

    Sort Salaries from Lowest to Highest

    Return first_name, last_name, salary, and department_id for every employee, ordered by salary from lowest to highest.

    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  2. Exercise 2Easy

    Rank Salaries from Highest to Lowest

    Return first_name, last_name, salary, and department_id for every employee, ordered by salary descending.

    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  3. Exercise 3Easy

    Show Most Recent Hires First

    Return first_name, last_name, and hire_date for every employee, ordered from the most recent hire to the oldest.

    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  4. Exercise 4Easy

    Sort Within Department Groups

    Return first_name, last_name, department_id, and salary, ordered by department_id ascending and salary descending within each department.

    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  5. Exercise 5Easy

    Make a Sort Deterministic

    Return sort_id, label, and score from sorting_cases, ordered by score descending and then by the unique sort_id.

    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  6. Exercise 6Medium

    Rank by Calculated Compensation

    Return first_name, last_name, salary, and calculated total_compensation, ordered from highest to lowest total compensation.

    • CASE expressions
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  7. Exercise 7Medium

    Sort by a SELECT Alias

    Return first_name, last_name, full_name, and salary, ordered by the full_name alias ascending.

    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  8. Exercise 8Medium

    Apply a Custom Department Priority

    Return first_name, last_name, department_id, and salary using the requested custom department priority, then salary descending within each priority.

    • CASE expressions
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  9. Exercise 9Medium

    Put Missing Deadlines Last

    Return sort_id, label, due_date, and priority from sorting_cases. Sort by due_date ascending with NULL deadlines last, priority descending within equal deadlines, and sort_id ascending as the final tie-breaker.

    • NULL handling
    • Filtering
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  10. Exercise 10Medium

    Normalize Case Before Sorting

    Return sort_id and label from sorting_cases, ordered by LOWER(label) and then sort_id.

    • String functions
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  11. Exercise 11Medium

    Rank Aggregated Department Headcount

    Return department_id and headcount, grouped by department and ordered by headcount descending, then department_id ascending.

    • Aggregation
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  12. Exercise 12Medium

    Sort Employees by Joined Department Name

    Join employees to departments and return first_name, last_name, salary, and department_name, ordered by department_name ascending and salary descending.

    • Joins
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  13. Exercise 13Medium

    Return a Stable Top Three

    Return sort_id, label, and score from sorting_cases for the three highest-scoring cases, ordered by score descending and sort_id ascending.

    • Sorting
    • Top-N

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  14. Exercise 14Hard

    Sort a Combined Operations Queue

    UNION ALL score >= 95 rows as 'high_score' with NULL due_date rows as 'missing_due_date'. Return sort_id, label, and queue_source; finally sort by queue_source, sort_id.

    • NULL handling
    • Filtering
    • Sorting

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
  15. Exercise 15Hard

    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

    SQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

    Open exercise
01

Attempt

Write a query or design from the brief before opening any hints.

02

Validate

Run it against the included dataset and inspect the output.

03

Explain

Compare the verified answer and explain each choice aloud.

Learn before practicing ORDER BY & Sorting

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.