Skip to content
Quantoros

Return the 5th highest distinct salary using ORDER BY with LIMIT and OFFSET, aliased fifthhighest. Table definitions…

SQLLevel: IntermediateRanking and Top-N

Return the 5th highest distinct salary using ORDER BY with LIMIT and OFFSET, aliased fifth_highest.

Table definitions (from the source exercise):

CREATE TABLE employees(emp_id INTEGER PRIMARY KEY, name TEXT, department_id INTEGER, manager_id INTEGER, salary INTEGER, hire_date TEXT, job_title TEXT, gender TEXT);
Hints
  • Define one output row and the source-table keys.
  • Check duplicates, NULLs and the order of filtering and aggregation.

Answer

Approach: DISTINCT first, sort descending, skip 4, take 1.

SELECT DISTINCT salary AS fifth_highest
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 4;

The source uses SQLite with a MySQL compatibility layer. Date and string functions are dialect dependent; the query is preserved as published.

Source and licence: SQL Practice Lab — Ebrahim Sharifi · MIT

Related questions