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.
- 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 → - 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 →
Attempt
Write a query or design from the brief before opening any hints.
Validate
Run it against the included dataset and inspect the output.
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
Build the next SQL skill
LIMIT & OFFSET
Practice deterministic top-N, cutoff ties, offset pagination, composite keyset cursors, and resumable bounded batches.
Ranking & NTH Value
Solve deterministic ranking, top-N, distribution, positional-frame, and rolling-window problems.
SELECT Statements
Select columns, filter rows, remove duplicates, and order query results.
Open the interactive workspace and practice across SQL topics.