For Engineering (departmentid 1), return name, salary, RANK() and DENSERANK() by salary descending. Notice how the…
SQLLevel: IntermediateRanking and Top-N
For Engineering (department_id 1), return name, salary, RANK() and DENSE_RANK() by salary descending. Notice how the 95,000 tie affects each.
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
- Which rows belong to the partition and frame for each current row?
- Check ordering, ties, frame boundaries and the difference from GROUP BY.
Answer
Approach: Two window functions side by side, same ORDER BY.
SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rnk,
DENSE_RANK() OVER (ORDER BY salary DESC) AS drnk
FROM employees
WHERE department_id = 1;
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