Top Earner Per Department (CTE)
Using a CTE, find the highest-paid employee in each department. Return columns: last_name, first_name, max_salary, department_id. Order results by: e.department_id.
Use common table expressions to structure readable multi-step SQL queries. These 3 interactive challenges use real schemas and instant query validation.
Using a CTE, find the highest-paid employee in each department. Return columns: last_name, first_name, max_salary, department_id. Order results by: e.department_id.
Use a CTE to return the three highest-paid employees.
Concept guide
A common table expression, introduced with WITH, gives a temporary name to a query result. CTEs make multi-step SQL easier to read, test and maintain.
Both can express intermediate results. A CTE is named at the beginning of a statement and is often easier to read or reuse within that statement.
No. A CTE primarily improves structure and readability. Its performance depends on the database engine, query plan and how the CTE is used.
Calculate how many customers from each acquisition month (cohort) placed orders in their first month. Return columns: cohort_size, cohort_month, retention_rate, first_month_orders. Order results by: c.cohort_month.