Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The final video in this stage will recap what we learned about subqueries and then back to the SQL playground for some more practice.
Subqueries are one of the most advanced concepts to learn in SQL query writing. Seeing lots of examples and getting lots of practice will help solidify the concept.
As you begin using subqueries, be advised that a poorly structured subquery written against a large table or tables can impact overall query performance. Be careful if you are writing subqueries against data sets that consume tables with row counts in the million-plus row range.
Correlated Subqueries
This course does not cover the concept called Correlated Subqueries, which are slightly different in how they tie in with the outer query. Most of the time a regular subquery will do what you need, but correlated subqueries can be handy, too. These are especially heavy weight on the database engine, as they run the subquery many times; once per row in the outer query.
Information on correlated subqueries
Cheat Sheet
See all of the SQL used in Querying Relational Databases in the Querying Relational Databases Cheat Sheet.
-
0:00
We've just learnt some great tools that make querying with SQL so powerful.
-
0:05
In most environments subqueries probably aren't used every day.
-
0:09
But when you're faced with the task of putting together a tricky dataset,
-
0:13
subqueries come in very handy.
-
0:16
Let's recap what we've learnt.
-
0:18
There are a couple of different reasons why you might need to use a subquery.
-
0:22
You can use a subquery when you don't necessarily know
-
0:25
all the values you want to include in a WHERE clause.
-
0:29
Or when you need to create a temporary data view, that
-
0:32
you can join to other data elsewhere in your database.
-
0:36
When using a subquery as part of an in statement you can put
-
0:40
any valid statement in parenthesis and the query puzzle will limit the results of
-
0:44
the outer query to the values that are returned by the subquery.
-
0:49
The key to this approach is that the subquery can only return
-
0:52
one column via the SELECT clause.
-
0:55
The number of ways that you construct to derive tables is
-
0:58
only limited by your imagination.
-
1:00
They sample syntax here only represents one possible way to use a derived
-
1:05
table but the things that apply every time you create a template table include,
-
1:10
you have to use parentheses just like any other subquery.
-
1:14
You need to alias the derived table so that the outer query can reference it.
-
1:18
The subquery is part of the FROM of the outer query and
-
1:23
you can join to it just like any other table.
-
1:26
It's time for another practice session.
-
1:29
Open up the accompanying SQL Playground with this video. We're
-
1:33
still using the library database with two locations in the North and
-
1:36
South. In the coding areas in the top left.
-
1:40
I have given you a number of reports that I'd like you to generate.
-
1:43
Try using subqueries where you can.
-
1:46
This concludes our course on Querying Relational Databases.
-
1:50
You have been presented with a lot of information.
-
1:53
Make sure you practice your new skills in the SQL Playground and
-
1:56
feel free to keep coming back to brush up on your skills and solidify them.
-
2:01
Once you're done practicing, it's time for your final assessment.
-
2:04
Good luck, and I'll see you next time.
You need to sign up for Treehouse in order to download course files.
Sign up