SQL Fundamentals - Part Five (subqueries)
- A subquery is a query inside of a query.
- Ever wanted to perform a
SELECTinside of aSELECT? Well, you actually can. - You start becoming a query magician.
We will work with this table
table name: items
|name |category|price|
|:-----------:|:------:|:----:|
|iPhone 12 |γΉγγ |100000|
|Pixel 5 |γΉγγ |80000|
|Xperia 511 |γΉγγ |90000|
|γ«γ³γ980 |ζι€ζ© |50000|
|Dyson V10 |ζι€ζ© |40000|
|γγ«γγ₯γΌγC|ζι€ζ© |60000|
Display items where the price is higher than average
SELECT AVG("price")
FROM items
;
-- 70000
SELECT *
FROM items
WHERE "price" >= 70000;
This is fine, but it can be done in a single query like this:
SELECT *
FROM items
WHERE price >= (SELECT AVG("price")
FROM items
);
| name | category | price |
|---|---|---|
| iPhone 12 | γΉγγ | 100000 |
| Pixel 5 | γΉγγ | 80000 |
| Xperia 511 | γΉγγ | 90000 |
Yeah, we are querying a SELECT inside of a SELECT. That's what a subquery is.
Display items where the price is higher than average, but for each category
SELECT *
FROM items
WHERE price >= (SELECT AVG("price")
FROM items
GROUP BY "category");
This will return an error, because it will return two averages. The solution is to use aliases to discern them.
SELECT *
FROM items
AS i1
WHERE i1."price" >= (
SELECT AVG(i2."price")
FROM items
AS i2
WHERE i1."category" = i2."category"
);
| name | category | price |
|---|---|---|
| iPhone 12 | γΉγγ | 100000 |
| Xperia 511 | γΉγγ | 90000 |
| γ«γ³γ980 | ζι€ζ© | 50000 |
| γγ«γγ₯γΌγC | ζι€ζ© | 60000 |
- Subqueries will make your life easier, otherwise you have to write queries one by one.
π§ Subquery Tips:
- Use subqueries when filtering against aggregated or correlated data.
- Correlated subqueries reference the outer queryβuse aliases to stay sane.
- Subqueries inside
WHERE,SELECT, or evenFROMare all valid and powerful. - Avoid unnecessary subqueries in productionβthey can destroy performance.
- If you are getting errors, try writing the function without subqueries.
- SQL: Practice makes muscle memory.