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
, HAVING
, or FROM
.
For instance, let's consider a straightforward subquery that fetches the average energy of all songs from the SongFeatures
table.
SQL1SELECT AVG(Energy) FROM SongFeatures;
This subquery, when used within a larger query, can help us compare a song's energy to the average and filter out songs 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 songs that have a duration longer than the average song duration. Here's how we could accomplish this with a subquery:
SQL1SELECT Name
2FROM Songs
3WHERE DurationMS > (SELECT AVG(DurationMS) FROM Songs);
In the example above, the subquery (SELECT AVG(DurationMS) FROM Songs)
calculates the average song duration. This value is then used by the outer query to filter all the songs that are longer than this average duration. Pretty cool, right?
While the previous example was relatively straight forward, subqueries often involve more complex problems. Let's tackle the problem step-by-step.
Our task for this lesson is to retrieve the names of songs along with their energy levels. However, we only want songs that have a higher energy level than the average. This seems like the perfect case for a subquery!
First, let's understand what an alias means in SQL. An alias is a temporary name assigned to a table or column for the duration of a query. This is particularly useful when dealing with long or complex table/column names or when joining tables, as it can make our SQL statement more concise and easier to follow.
Here's how we can compose our query:
SQL1SELECT
2 s.Name AS SongName,
3 sf.Energy -- Using aliases 's' for 'Songs' and 'sf' for 'SongFeatures'
4FROM
5 Songs s -- 's' is an alias for the 'Songs' table
6INNER JOIN
7 SongFeatures sf -- 'sf' is an alias for the 'SongFeatures' table
8ON
9 s.SongID = sf.SongID
10WHERE
11 sf.Energy > (
12 SELECT
13 AVG(Energy)
14 FROM
15 SongFeatures
16 )
17ORDER BY
18 sf.Energy DESC;
In this query:
s
and sf
for our tables Songs
and SongFeatures
, respectively. This aspect simplifies subsequent references within the query.INNER JOIN
on the Songs
and SongFeatures
tables on the SongID
field.WHERE
clause that compares each song's energy level to the average energy (SELECT AVG(Energy) FROM SongFeatures
) of all songs. This is our subquery.Energy
field.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!