Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
UNION clause | Joining Tables
course content

Course Content

Intermediate SQL

UNION clauseUNION 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:

  1. UNION ALL - duplicates are included;
  2. 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:

  1. Find the longest flight (create a new column named time using the MAX() function and travel_time column, take it from the flights table);
  2. Find the shortest flight (create a new column named time using the MIN() function and travel_time column, take it from the flights table).

Here's a short example of the flights table:

flight_numberstart_point_flightend_point_flighttravel_time
1ParisNew York8
2ChicagoNew Delhi15
3New YorkDubai12
4Los AngelesHonolulu5

Everything was clear?

Section 3. Chapter 8
toggle bottom row
course content

Course Content

Intermediate SQL

UNION clauseUNION 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:

  1. UNION ALL - duplicates are included;
  2. 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:

  1. Find the longest flight (create a new column named time using the MAX() function and travel_time column, take it from the flights table);
  2. Find the shortest flight (create a new column named time using the MIN() function and travel_time column, take it from the flights table).

Here's a short example of the flights table:

flight_numberstart_point_flightend_point_flighttravel_time
1ParisNew York8
2ChicagoNew Delhi15
3New YorkDubai12
4Los AngelesHonolulu5

Everything was clear?

Section 3. Chapter 8
toggle bottom row
some-alt