Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
UNION
6. How UNION works
INTERSECT
EXCEPT

Instruction

Let's start off with a new keyword UNION. What is a union? Well, to make a long story short, it combines results of two or more queries. Let's analyze the example:

SELECT *
FROM cycling
WHERE country = 'Germany'
UNION
SELECT *
FROM skating
WHERE country = 'Germany';

As you can see, we first selected all medals for Germany from the table cycling, then we used the keyword UNION and finally we selected all medals for Germany from the table skating.

You may be tempted to ask: Could we split this instruction into two separate queries? Of course we could. But using a UNION, we get all results for the first table PLUS the results of the second table shown together. Remember to only put the semicolon (;) at the very end of the whole instruction!

Exercise

Show all the medals for the period between 2010 and 2014 for skating and cycling. Use the UNION keyword.

Stuck? Here's a hint!

Type:

SELECT *
FROM cycling
WHERE year BETWEEN 2010 AND 2014
UNION
SELECT *
FROM skating
WHERE year BETWEEN 2010 AND 2014;