Course Content
Intermediate SQL
2. Nested Subqueries
4. Left, Right, and Inner Joins
Intermediate SQL
UNION clause
Sometimes it is convenient to combine the result of several queries into one continuous result. For this purpose, SQL has the UNION
operator.
But we have two different UNION
clauses:
UNION ALL
- duplicates are included;UNION
- duplicates are excluded.
Let's take a look at the syntax of using this operator.
It is time to practice!
Task
You have to make two queries and combine them using the UNION
operator:
- Find the longest flight (create a new column named
time
using theMAX()
function andtravel_time
column, take it from theflights
table); - Find the shortest flight (create a new column named
time
using theMIN()
function andtravel_time
column, take it from theflights
table).
Here's a short example of the flights
table:
flight_number | start_point_flight | end_point_flight | travel_time |
1 | Paris | New York | 8 |
2 | Chicago | New Delhi | 15 |
3 | New York | Dubai | 12 |
4 | Los Angeles | Honolulu | 5 |
Everything was clear?
Section 3. Chapter 8