Lessons › SQL › Chapter 5: Joins, subqueries and CTEs
SQL lesson
CTEs with WITH
A CTE lets you write a query in two steps. First you work something out and give it a name. Then your main query uses that name like a table. For example: first total each customer's spend, then keep the customers who spent over 100.
Try this lesson free →Example
Two easy steps instead of one hard one
WITH customer_totals AS (
SELECT customer_id, SUM(revenue) AS total_revenue
FROM orders
GROUP BY customer_id
)
SELECT *
FROM customer_totals
WHERE total_revenue > 100| customer_id | total_revenue |
|---|---|
| C101 | 210.49 |
| C103 | 210.75 |
| C104 | 140.0 |
Step by step
- WITH gives your first query a name. Write the name, then AS, then the query in brackets.
WITH customer_totals AS (SELECT customer_id, SUM(revenue) AS total_revenue FROM orders GROUP BY customer_id) SELECT * FROM customer_totals WHERE total_revenue > 100 - Totals each customer's spend. AS total_revenue names the column, so the next step can use it.
WITH customer_totals AS (SELECT customer_id, SUM(revenue) AS total_revenue FROM orders GROUP BY customer_id) SELECT * FROM customer_totals WHERE total_revenue > 100 - The main query reads from the name, just like a table.
WITH customer_totals AS (SELECT customer_id, SUM(revenue) AS total_revenue FROM orders GROUP BY customer_id) SELECT * FROM customer_totals WHERE total_revenue > 100
Practice
1. Using orders, total each customer's revenue in a CTE called customer_totals, then show the customers whose total_revenue is over 100.
WITH customer_totals AS (SELECT customer_id, SUM(revenue) AS total_revenue FROM orders GROUP BY customer_id) SELECT * FROM customer_totals WHERE total_revenue > 1002. Using the same CTE, show the customers who spent over 100 with the biggest spender first.
WITH customer_totals AS (SELECT customer_id, SUM(revenue) AS total_revenue FROM orders GROUP BY customer_id) SELECT * FROM customer_totals WHERE total_revenue > 100 ORDER BY total_revenue DESC3. Using store_sales, total each store's revenue in a CTE called store_totals, then show the stores whose total_revenue is over 300.
WITH store_totals AS (SELECT store_id, SUM(revenue) AS total_revenue FROM store_sales GROUP BY store_id) SELECT * FROM store_totals WHERE total_revenue > 3004. Using employees, average each department's salary in a CTE called dept_pay, then show the departments whose avg_salary is over 60000.
WITH dept_pay AS (SELECT department, AVG(salary) AS avg_salary FROM employees GROUP BY department) SELECT * FROM dept_pay WHERE avg_salary > 600005. Using products, find each category's highest price in a CTE called top_prices, then show the categories whose max_price is under 100.
WITH top_prices AS (SELECT category, MAX(price) AS max_price FROM products GROUP BY category) SELECT * FROM top_prices WHERE max_price < 100Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →