Set Operations with Different Columns
Set Operations with Different Columns is a SQL statement in the Set Operations category. Corresponding columns must have compatible data types, but column names come from the first query. The syntax is SELECT col1, col2 FROM table1 UNION SELECT col_a, col_b FROM table2;. It returns compatible combined result. A typical example: SELECT employee_id AS id, full_name AS name FROM employees UNION SELECT customer_id, company_name FROM customers; -- Result columns named: id, name (from first query) -- Column count MUST match -- With constants: SELECT id, name, 'Employee' AS type FROM employees UNION ALL SELECT id, name, 'Customer' AS type FROM customers ORDER BY name; 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.