Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire 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.
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.
See all of the SQL used in Querying Relational Databases in the Querying Relational Databases Cheat Sheet.
We've just learnt some great tools that make querying with SQL so powerful.
In most environments subqueries probably aren't used every day.
But when you're faced with the task of putting together a tricky dataset,
subqueries come in very handy.
Let's recap what we've learnt.
There are a couple of different reasons why you might need to use a subquery.
You can use a subquery when you don't necessarily know
all the values you want to include in a WHERE clause.
Or when you need to create a temporary data view, that
you can join to other data elsewhere in your database.
When using a subquery as part of an in statement you can put
any valid statement in parenthesis and the query puzzle will limit the results of
the outer query to the values that are returned by the subquery.
The key to this approach is that the subquery can only return
one column via the SELECT clause.
The number of ways that you construct to derive tables is
only limited by your imagination.
They sample syntax here only represents one possible way to use a derived
table but the things that apply every time you create a template table include,
you have to use parentheses just like any other subquery.
You need to alias the derived table so that the outer query can reference it.
The subquery is part of the FROM of the outer query and
you can join to it just like any other table.
It's time for another practice session.
Open up the accompanying SQL Playground with this video. We're
still using the library database with two locations in the North and
South. In the coding areas in the top left.
I have given you a number of reports that I'd like you to generate.
Try using subqueries where you can.
This concludes our course on Querying Relational Databases.
You have been presented with a lot of information.
Make sure you practice your new skills in the SQL Playground and
feel free to keep coming back to brush up on your skills and solidify them.
Once you're done practicing, it's time for your final assessment.
Good luck, and I'll see you next time.
You need to sign up for Treehouse in order to download course files.Sign up