Query 1: Find out total of sales of each salesman in all the regions.
Use:
SELECT s.name,
COALESCE(SUM(x.sales), 0)
FROM SALESMAN s
LEFT JOIN (SELECT r1.s_id,
r1.sales
FROM REGION_1 r1
UNION ALL
SELECT r2.s_id,
r2.sales
FROM REGION_2 r2
UNION ALL
SELECT r3.s_id,
r3.sales
FROM REGION_3 r3
UNION ALL
SELECT r4.s_id,
r4.sales
FROM REGION_4 r4) x ON x.s_id = s.s_id
Query 2: Find out total of sales of a particual salesman in all the regions.
Add the WHERE clause to the query above:
SELECT s.name,
COALESCE(SUM(x.sales), 0)
FROM SALESMAN s
LEFT JOIN (SELECT r1.s_id,
r1.sales
FROM REGION_1 r1
UNION ALL
SELECT r2.s_id,
r2.sales
FROM REGION_2 r2
UNION ALL
SELECT r3.s_id,
r3.sales
FROM REGION_3 r3
UNION ALL
SELECT r4.s_id,
r4.sales
FROM REGION_4 r4) x ON x.s_id = s.s_id
WHERE s.s_id = ?
...replace the ?
with the salesperson's s_id value.
The Lesson: UNION vs UNION ALL
UNION
and UNION ALL
will allow you to combine two queries, but they have to have the same data types in the column positions. For example, say query one returns data types in the following order:
- Integer
- String
Any subsequent query that is union'd to the first has to return the same data types in the same position. Pay special attention to the fact of data types - it doesn't guarantee the data within the column.
The second issue to UNIONs is knowing the difference between UNION
and UNION ALL
. UNION
will remove duplicates if they exist (equivalent to using DISTINCT
), which is not desired in the given scenario. UNION ALL
will not remove duplicates, and is faster because of this.