Hello again! In our previous lessons, we explored the functionality of Logical AND/OR operations and delved into conditional operators in SQL such as LIKE
, IN
, and BETWEEN
. We delved deeply into how these tools not only allow us to filter and retrieve data accurately but also provide more nuanced control over our queries. In this unit, we'll build on those skills and introduce a new SQL concept—subqueries—which will enable us to perform even more complex data analysis.
So, what is a subquery? A subquery, also known as an "inner query" or "nested query," is a query nested within another SQL query. It allows us to solve convoluted problems that require multiple steps, making our SQL statements even more powerful. Essentially, a subquery can retrieve data for the primary or outer SQL query to utilize. Like other SQL queries you're now familiar with, they begin with a SELECT
statement and conclude with an appropriate clause such as WHERE
or FROM
.
For instance, let's consider a straightforward subquery that calculates the average price of products in the Products
table:
SQL1SELECT AVG(product_price) FROM Products; 2 3-- Output: 4-- AVG(product_price) 5-- ------------------- 6-- 10.874
This subquery, when used within a larger query, can help us compare each product's price to the average and filter out products that meet certain conditions.
Subqueries can be nested within other subqueries or queries. Nesting is the act of placing one item inside another. In the case of subqueries, nesting results in an outer query, possibly containing one or more subqueries. These subqueries can, in turn, contain further subqueries, enabling SQL to solve exceptionally complex tasks.
While the concept might appear intimidating initially, don't worry! The beauty of nested subqueries is that they can always be broken down into smaller, easier-to-understand steps.
Let's examine a straightforward example before we proceed to break down the final task. Suppose we want to find products that have a price greater than the average price of all products. Here's how we could accomplish this with a subquery:
SQL1SELECT
2 p.product_id AS ProductID,
3 p.product_name AS ProductName,
4 o.order_date AS OrderDate
5FROM Products p
6INNER JOIN OrderItems oi ON p.product_id = oi.product_id
7INNER JOIN Orders o ON oi.order_id = o.order_id
8WHERE p.product_price > (
9 SELECT AVG(product_price)
10 FROM Products
11);
12
13-- Sneak peek of the output:
14-- | ProductID | ProductName | OrderDate |
15-- |-----------|---------------------------------|------------|
16-- | 2 | Math Problems Flashcards | 2022-04-03 |
17-- | 4 | History Events Flashcards | 2022-03-24 |
In this example:
- We introduce aliases
p
forProducts
ando
forOrderItems
to simplify references within the query. - First, we perform an
INNER JOIN
on theProducts
andOrderItems
tables on theproduct_id
field, and also doINNER JOIN
on theOrders
table for retrieving theorder_date
. - Next, we filter using a
WHERE
clause that compares each product's price to the average price (SELECT AVG(product_price) FROM Products
) of all products. This is our subquery. - This query retrieves products where the price is above the average, along with the order date of orders containing those products.
You have now seen how to use subqueries to tackle complex SQL problems!
Great job making it this far! In this lesson, we learned about subqueries and their ability to break complex SQL tasks down into manageable parts. We firmly believe in learning by doing, so we have arranged a set of practice exercises where you can apply the concept of subqueries and become more comfortable with them.
In the next lesson, we'll continue to delve deeper into more advanced SQL query techniques. Keep up the great work! You're doing amazing!