What is a Subquery?
A subquery is a query that is embedded within another SQL query. It’s typically enclosed in parentheses and can be used in various places, such as theSELECT, FROM, or WHERE clauses.
Imagine you want to answer questions like:
- Which products have a price higher than the average price?
- Who ordered the most expensive product?
Scenario
Let’s revisit our familiar tables: Products Table:| product_id | product_name | category | price | stock |
|---|---|---|---|---|
| 1 | Wireless Mouse | Electronics | 1200 | 25 |
| 2 | Yoga Mat | Fitness | 800 | 50 |
| 3 | Bluetooth Speaker | Electronics | 2500 | 15 |
| 4 | Water Bottle | Fitness | 300 | 100 |
| 5 | Smartwatch | Electronics | 5000 | 10 |
| order_id | product_id | order_date | quantity |
|---|---|---|---|
| 101 | 1 | 2025-01-01 | 2 |
| 102 | 2 | 2025-01-02 | 5 |
| 103 | 3 | 2025-01-02 | 1 |
| 104 | 5 | 2025-01-03 | 2 |
| 105 | 4 | 2025-01-03 | 10 |
Subquery in the WHERE Clause
Let’s find products that cost more than the average price of all products.
- The subquery
(SELECT AVG(price) FROM products)calculates the average price of all products. - The main query retrieves products where the price is greater than this average.
| product_name | price |
|---|---|
| Bluetooth Speaker | 2500 |
| Smartwatch | 5000 |
Subquery in the FROM Clause
Subqueries can also act like a temporary table. Let’s find the average price of each category.
- The subquery selects
categoryandpricefrom theproductstable. - The main query calculates the average price for each category using
GROUP BY.
| category | avg_price |
|---|---|
| Electronics | 2900 |
| Fitness | 550 |
Subquery in the SELECT Clause
What if you want to display each product’s price along with the overall average price?
- The subquery calculates the overall average price.
- This value is displayed alongside each product’s price.
| product_name | price | avg_price |
|---|---|---|
| Wireless Mouse | 1200 | 1400 |
| Yoga Mat | 800 | 1400 |
| Bluetooth Speaker | 2500 | 1400 |
| Water Bottle | 300 | 1400 |
| Smartwatch | 5000 | 1400 |
Subquery with IN Clause
Let’s find the names of products that have been ordered.
- The subquery retrieves all
product_idvalues from theorderstable. - The main query finds matching products using
IN.
| product_name |
|---|
| Wireless Mouse |
| Yoga Mat |
| Bluetooth Speaker |
| Smartwatch |
| Water Bottle |
Subquery with EXISTS
Now let’s find products that have not been ordered.
- The subquery checks if a matching
product_idexists in theorderstable. NOT EXISTSensures only unmatched products are returned.
(No products in this example since all have been ordered.)
Correlated Subquery
A correlated subquery is a subquery that refers to the outer query. Let’s find products whose order quantity is more than their stock.- The subquery checks for each product if its
quantityin theorderstable exceeds itsstock. - The subquery is evaluated for each row in the outer query.
Summary
Subqueries are like SQL’s secret weapon—helping you solve complex problems step by step. Whether you’re filtering data withWHERE, creating temporary tables with FROM, or enhancing your output with SELECT, subqueries are indispensable.
In the next article, we’ll tackle how to insert, update or delete data from the table. Keep practicing!