SQL Subquery PDF
SQL Subquery PDF
SQL Subquery PDF
SERIES
How to Write
SUBQUERY
Subqueries are a powerful SQL resource, allowing us to combine data
from multiple tables in a single query.
Our sample database will have two tables. The first table is called
best_10_places and it stores the 10 best places for different kinds of
activities (like snorkeling, skiing, and trekking).
The table has columns for the place name, the activity we can do
there, the ranking of this place, and the closest city. Have a look:
If you want to travel to any of these beautiful places, you’ll need a
ticket; the one_way_ticket table has one record for any pair of cities
that are connected by any kind of transportation.
We will use this table to determine how to get from one city to
another.
The columns contain info on the origin city, destination city, ticket
price, travel time, and transportation type (e.g. rail, air, etc.). Below is
a subset of this table:
linkedin.com/in/ileonjose
Now we’re ready for the first example.
So, in the next example you’ll see two queries: the main query (also
called the outer query) and the subquery (in red):
However, a few of them can only be used with scalar subqueries: =, >,
>=, < and <=. If you use one of these operators, your subquery must
be scalar.
Let’s see an example with a scalar subquery.
Again, the subquery is executed first; its result (the price of a Paris-
Bariloche ticket, or $970) is compared with the column ticket_price
in the outer query.
Let’s suppose the person who asked about the best place for
snorkeling wants to explore other destinations; in fact, they’d like to
see the top three snorkeling places.
In the next example, we will see how to use a subquery in the FROM
clause.
Suppose the travel agency’s owner wants to show every city along
with the ticket cost and the number of “best places” near this city.
To obtain the quantity of “best places” for each city, we will use a
subquery (shown in red) in the FROM clause to create a pseudo table.
Then the outer query will JOIN with one_way_ticket and the pseudo
table.
SELECT city_destination, ticket_price, pseudo_table.quantity
FROM one_way_ticket
JOIN (
SELECT closest_city AS city, count(*) AS quantity
FROM best_10_places
GROUP BY 1
) pseudo_table
ON one_way_ticket.pseudo_table.city
One of the most powerful operators you can use with subqueries is
the EXISTS operator.
As we can see in the example below, the EXISTS operator must come
before the subquery.
For the next example, let’s suppose our customer from Paris wants to
travel to a place where they can do both trekking and snorkeling.
linkedin.com/in/ileonjose
SELECT city_destination, transportation, ticket_price, travel_time
FROM one_way_ticket
WHERE EXISTS (
SELECT closest_city
FROM best_10_places
WHERE activity_type = 'snorkeling'
AND closest_city = one_way_ticket.city_destination
)
AND EXISTS (
SELECT closest_city
FROM best_10_places
WHERE activity_type = 'trekking'
AND closest_city = one_way_ticket.city_destination
)
AND city_origin = 'Paris'
The result shows the records related to cities with trekking and
snorkeling activities:
One interesting point in the previous subquery is the reference to the
one_way_ticket.city_destination column in the outer query.
This pair of operators works in conjunction with the =, <>, >, >=, < and
<= operators, adding more expressivity to the language.
Due the high number of possible combinations with ALL and ANY,
I’ve included a table with the most common uses of these operators:
Let’s apply this operator to a real-life example.
Suppose we want to promote all the “world’s best places” you can
visit with a ticket under $1,000.
The results of the previous query are shown below. You can go to the
best place for any activity (snorkeling, skiing, and trekking) for less
than $1,000!
If you find this helpful, Repost
linkedin.com/in/ileonjose