SQL practice: CTEs: Naming Your Steps

Chapter 18 · 10 questions · included in full access

  1. Question 1 · easy

    Use a CTE to find the top 3 customers by total spend.

    Solve it in your browser
  2. Question 2 · easy

    Use a CTE for total quantity sold per product, then show only products above 150.

    Solve it in your browser
  3. Question 3 · medium

    Use a CTE to calculate revenue per branch, then show only branches above the average branch revenue.

    Solve it in your browser
  4. Question 4 · medium

    Use a CTE to count orders per customer, then show only customers with more than 2.

    Solve it in your browser
  5. Question 5 · medium

    Chain two CTEs: one for revenue per product, one that adds a rank, then show the top 5.

    Solve it in your browser
  6. Question 6 · medium

    Use a CTE to work out each employee's total handled revenue, then join it to employees to show names.

    Solve it in your browser
  7. Question 7 · medium

    Use a CTE for total spend per customer, then show the overall average of those totals.

    Solve it in your browser
  8. Question 8 · medium

    Rewrite this nested query as a CTE: SELECT * FROM (SELECT role, AVG(salary) AS avg_pay FROM employees GROUP BY role) t WHERE avg_pay > 45000;

    Solve it in your browser
  9. Question 9 · hard

    Chain three CTEs: revenue per branch → add a rank → join to branch names, showing only the bottom 3.

    Solve it in your browser
  10. Question 10 · hard

    Find customers whose total spend is above the average customer's spend. It fails with 'relation "customer_totals" does not exist'. Fix it.

    Solve it in your browser

These questions are part of full access.

The first chapters of every track are free. Full access is a one-time payment.

See pricing