Course Content
Intermediate SQL
2. Nested Subqueries
4. Left, Right, and Inner Joins
Intermediate SQL
Nested Queries
Hi again! In this section, we are going to work with the flight
database. It contains the following three tables: passenger
, departure
, and flights
. Let's take a look at them closer and learn their connections.
Let's look at the first table - passenger
.
Let's look at the second table - departure
.
Let's look at the third table - flights
.
Let's see the connection between these three tables.
There is a very common practice of using nested subqueries. Let's take a look at the example, and then discuss the syntax.
Note
To get all columns you have to use the asterisk
*
with theSELECT
statement.
How does this request work? Initially, the nested query will be executed—specifically, the one located within the second part enclosed in parentheses. Subsequently, the primary query, which is initially defined, is filtered based on the result obtained from the nested subquery.
Analysis: Accordingly, numbers of departures that occurred on 2022-07-16 will be found first. (This means that the nested subquery worked). Let's look at the result of the nested request.
Accordingly, the main request will work, displaying all the information of passengers whose departure numbers are 1 and 6. And as a result, if we run the whole query, we will get the following information:
It is time to practice!
Task
You have to create a nested query. To do this, you need to create two queries: a main query and a nested query.
- Let's start with the main query: get all the information from the
passenger
table, where the value of thenumber_of_departure
column must be less or equal to the value that we will receive in the nested query. - For the nested query, you need to extract the
number_of_departure
column from thedeparture
table, with the following departure timedeparture_time = '13:15:00'
.
Everything was clear?