QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Interview tasks

Task: the employee with the highest salary in each department.

The highest salary with the name in each department comes from the window function RANK() with PARTITION BY department and a filter on rank 1. It is wrapped in a CTE or subquery, because it is not available in WHERE. RANK shows everyone sharing the maximum, while ROW_NUMBER with a tie-break or DISTINCT ON gives exactly one. In PostgreSQL NULL comes first under DESC, so NULLS LAST is needed.

SQL · Tasks

Not just the maximum value but the person's name is needed. Why is GROUP BY not enough here?

SELECT department, MAX(salary) FROM employees GROUP BY department returns the top salary in a department, but without a name. Adding name to the list does not work: the DBMS does not know whose name to show when it is not grouped. The task checks whether the candidate knows how to connect an aggregate with the row it came from. The sample data is below.

namedepartmentsalary
AnnaDev300
BohdanDev300
OlhaDev250
IvanQA280
KaterynaQANULL
PetroHRNULL

How it works and why. A window function RANK() OVER (PARTITION BY department ORDER BY salary DESC) numbers the rows inside a department and keeps them all, so the name stays available. Equal salaries get the same rank, so rnk = 1 returns everyone sharing the maximum. A window function cannot be placed in WHERE (it is computed after it), so it is wrapped in a CTE or a subquery and filtered outside. A JOIN with an aggregate first computes the department maximum, then finds the rows whose salary equals it. The result is the same, without window functions.

Example: when NULL stops being harmless. The classic form:

WITH r AS (
  SELECT name, department, salary,
         RANK() OVER (PARTITION BY department
                      ORDER BY salary DESC) AS rnk
  FROM employees
)
SELECT department, name, salary FROM r WHERE rnk = 1;

Diagnosis on our data in PostgreSQL: for DESC NULL comes first by default, so in QA first rank went to Kateryna with an empty salary, and Ivan with 280 got second and disappeared. It looks like a normal result until QA is checked against the table. The fix: ORDER BY salary DESC NULLS LAST or a WHERE salary IS NOT NULL filter.

RANK = 1: where NULL ends up first

Default DESC
deptnamesalary
DevAnna300
DevBohdan300
HRPetroNULL
QAKaterynaNULL wrong
DESC NULLS LAST
deptnamesalary
DevAnna300
DevBohdan300
HRPetroNULL
QAIvan280
Figure 1. In QA first place went to Kateryna with no salary, and Ivan disappeared from the result. HR shows the only employee, whose salary is unknown.

The NULLS LAST version also shows the HR department, where no salaries exist at all: the only employee with NULL. With an IS NOT NULL filter the HR department would drop out of the result. Which behaviour is right is for the requirement to decide, and it is worth saying aloud.

-- without window functions: everyone with the department maximum
SELECT e.department, e.name, e.salary
FROM employees e
JOIN (SELECT department, MAX(salary) AS max_salary
      FROM employees
      GROUP BY department) m
  ON m.department = e.department
 AND m.max_salary = e.salary;
-- Dev Anna 300, Dev Bohdan 300, QA Ivan 280

-- PostgreSQL: exactly one per department
SELECT DISTINCT ON (department) department, name, salary
FROM employees
ORDER BY department, salary DESC NULLS LAST, name;
-- Dev Anna 300, HR Petro NULL, QA Ivan 280

Limits and alternatives. The JOIN with an aggregate handles NULL correctly: MAX skips them, and in the HR department the maximum is NULL, so m.max_salary = e.salary finds nothing with NULL and HR disappears. ROW_NUMBER() instead of RANK() always gives exactly one of the equal ones (which one depends on the tie-break in ORDER BY). DISTINCT ON is PostgreSQL-specific: it keeps the first row of each group in ORDER BY order, and the name after the salary works as a tie-break. Of the three ways the window function is the most flexible: changing rnk = 1 to rnk <= 3 gives the top three.

Common mistakes.

  • SELECT name, MAX(salary) … GROUP BY department: an error, because name is not grouped.
  • Ignoring NULL in ORDER BY … DESC: in PostgreSQL NULL takes first place.
  • ROW_NUMBER where all the equal ones are needed: some people silently disappear.
  • A window function straight in WHERE: an error, a CTE or subquery is needed.

Check yourself

What does the RANK query without NULLS LAST return for the QA department on our data in PostgreSQL?

Answer

Kateryna with NULL: under DESC NULL comes first and gets rank 1. Ivan with 280 becomes second and is not in the result. The result is wrong although the query runs without error.

Exactly one person per department is needed, even if two share the maximum. How to do it?

Answer

Use ROW_NUMBER() with a tie-break in ORDER BY (for example by name) and take rn = 1, or DISTINCT ON (department) in PostgreSQL. The tie-break makes the choice reproducible.

In an interview. Junior: GROUP BY with MAX gives only the maximum salary, not the name, so I use the RANK window function partitioned by department and take rank one. An alternative without window functions is to join the table with a subquery that computes the department maximum. Middle/Senior: RANK shows everyone sharing the maximum, while ROW_NUMBER or DISTINCT ON gives exactly one, and here I ask what is needed. I also watch NULL: in PostgreSQL it comes first under DESC, so I add NULLS LAST and settle with the requirement whether to show a department with no salaries.

More articles in this chapter