Deviopath › PostgreSQL

Big Spenders with a CTE

Medium · CTEs

A CTE (WITH name AS (SELECT ...)) gives a subquery a name, so you can build a query in readable steps: first compute something, then use it like a table.

Using a CTE, first compute each customer's total of paid orders. Then return name and total_spent for customers whose total is at least 50. Sort by total_spent descending, then name ascending.

Next: Monthly Revenue