Let's consider the task.
Find out all available products of the producer 'B'. Output: model, type.
One laptop and three PCs are in the database from maker 'B', and all the PCs are of the same model.
If we'll use UNION ALL, we get all these items. Using UNION, the duplicates will be excluded from the result:
Console
SELECT p.model, p.type FROM pc JOIN Product p ON PC.model=p.model WHERE maker='B'
UNION ALL
SELECT p.model, p.type FROM printer pr JOIN Product p ON pr.model=p.model WHERE maker='B'
UNION ALL
SELECT p.model, p.type FROM laptop lp JOIN Product p ON lp.model=p.model WHERE maker='B';
model
|
type
|
1121 | PC |
1121 | PC |
1121 | PC |
1750 | Laptop |
|
Console
SELECT p.model, p.type FROM pc JOIN Product p ON PC.model=p.model WHERE maker='B'
UNION
SELECT p.model, p.type FROM printer pr JOIN Product p ON pr.model=p.model WHERE maker='B'
UNION
SELECT p.model, p.type FROM laptop lp JOIN Product p ON lp.model=p.model WHERE maker='B';
model
|
type
|
1121 | PC |
1750 | Laptop |
|
The solution on the base of UNION corresponds to the somewhat different task, namely:
Find out which models of the maker 'B' are available. Output: model, type.