DataLane

16. Salaries above the department average

hardProNot started~14 minWindow functionsSubqueries

Asked in screens shaped like: Google, Microsoft, Bloomberg

Return employees earning more than their own department’s average, and show that average beside them. The trap: compute the average with a window and filter it in WHERE, and the average is recomputed over only the surviving rows.

Requirements

  • Columns: employee_id, name, dept, salary, dept_avg
  • dept_avg is the average salary of the whole department, rounded to 2 decimals
  • Keep only employees whose salary exceeds their department average
  • Order by dept, then salary descending

Expected output

employee_id, name, dept, salary, dept_avg

Row order is graded, so ORDER BY matters here.

SQL editor

Loading SQL engine…

Ctrl/Cmd + Enter submits

Run your query to see the result set.

Pro problem

Hard problems unlock with Pro

Easy pads stay free. Medium and hard SQL and Python problems — editor, tests, hints, and solutions — open after you upgrade to Pro or coaching.

See plansPractice free easy problems
↑↓ navigate openesc close