UNION with Column Aliases
UNION with Column Aliases is a SQL statement in the Set Operations category. Column names in set operation results come from the first SELECT. ORDER BY can reference the first SELECTs aliases. The syntax is SELECT column AS alias FROM table1 UNION SELECT column FROM table2 ORDER BY alias;. It returns aliased combined result. A typical example: SELECT employee_name AS name FROM employees UNION ALL SELECT customer_name FROM customers ORDER BY name; -- Result column is named "name" (from first query) -- ORDER BY can use this alias -- Data types: compatible types required across databases -- e.g., VARCHAR and CHAR are compatible, INT and VARCHAR may error A close relative is UNION, which combines results from two queries, removing duplicate rows. Each SELECT must have the same number of columns. A close relative is UNION ALL, which like UNION but does NOT remove duplicate rows. Faster because no dedup is needed.