Lessons › SQL › Go further in SQL: Business questions
SQL lesson
Which orders beat their own store's average?
Which orders beat their own store's average? Each order is compared with a different number, its own store's average. Work out every store's average first, then join it back to the orders and compare.
Try this lesson free →Example
Each group's average, then compare
WITH store_avg AS (
SELECT store, AVG(revenue) AS avg_revenue
FROM orders
GROUP BY store
)
SELECT o.order_id, o.store, o.revenue
FROM orders o
JOIN store_avg a ON o.store = a.store
WHERE o.revenue > a.avg_revenue| order_id | store | revenue |
|---|---|---|
| 1 | Lagos | 70 |
| 5 | Accra | 120 |
| 6 | Lagos | 110 |
| 9 | Abuja | 90 |
| 11 | Accra | 150 |
| 12 | Lagos | 65 |
Step by step
- Only the ones above their store's average.
WITH store_avg AS (SELECT store, AVG(revenue) AS avg_revenue FROM orders GROUP BY store) SELECT o.order_id, o.store, o.revenue FROM orders o JOIN store_avg a ON o.store = a.store WHERE o.revenue > a.avg_revenue - The averages come from orders too.
WITH store_avg AS (SELECT store, AVG(revenue) AS avg_revenue FROM orders GROUP BY store) SELECT o.order_id, o.store, o.revenue FROM orders o JOIN store_avg a ON o.store = a.store WHERE o.revenue > a.avg_revenue - WHERE o.revenue > a.avg_revenue.
WITH store_avg AS (SELECT store, AVG(revenue) AS avg_revenue FROM orders GROUP BY store) SELECT o.order_id, o.store, o.revenue FROM orders o JOIN store_avg a ON o.store = a.store WHERE o.revenue > a.avg_revenue - A CTE called store_avg, joined back on store.
WITH store_avg AS (SELECT store, AVG(revenue) AS avg_revenue FROM orders GROUP BY store) SELECT o.order_id, o.store, o.revenue FROM orders o JOIN store_avg a ON o.store = a.store WHERE o.revenue > a.avg_revenue
Practice
1. Which orders beat their own store's average order? Show order_id, store and revenue.
WITH store_avg AS (SELECT store, AVG(revenue) AS avg_revenue FROM orders GROUP BY store) SELECT o.order_id, o.store, o.revenue FROM orders o JOIN store_avg a ON o.store = a.store WHERE o.revenue > a.avg_revenue2. Which orders beat the average of all orders? Show order_id, store and revenue.
SELECT order_id, store, revenue FROM orders WHERE revenue > (SELECT AVG(revenue) FROM orders)3. Which orders beat their own product's average order? Show order_id, product and revenue.
WITH product_avg AS (SELECT product, AVG(revenue) AS avg_revenue FROM orders GROUP BY product) SELECT o.order_id, o.product, o.revenue FROM orders o JOIN product_avg a ON o.product = a.product WHERE o.revenue > a.avg_revenue4. Which customers spent more than the average customer? Show customer_id and spend.
WITH spend AS (SELECT customer_id, SUM(revenue) AS spend FROM orders GROUP BY customer_id) SELECT customer_id, spend FROM spend WHERE spend > (SELECT AVG(spend) FROM spend)5. Which stores have an average order above the average of all orders? Show store and avg_revenue.
SELECT store, AVG(revenue) AS avg_revenue FROM orders GROUP BY store HAVING AVG(revenue) > (SELECT AVG(revenue) FROM orders)Learn 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 →