ORDER BY clause with UNION
As the following query shows, when you include an ORDER
BY clause, it must follow the final SELECT statement and use an integer,
not an identifier, to refer to the ordering column. Ordering takes
place after the set operation is complete.
Figure 1. Query
SELECT DISTINCT stock_num, manu_code FROM stock
WHERE unit_price < 25.00
UNION
SELECT stock_num, manu_code FROM items
WHERE quantity > 3
ORDER BY 2;
The compound query above selects the same
rows as Figure 2 but
displays them in order of the manufacturer code, as the result shows.
Figure 2. Query result
stock_num manu_code
5 ANZ
9 ANZ
302 HRO
301 KAR
302 KAR
201 NKL
5 NRG
103 PRC
106 PRC
5 SMT