UNION in SQL
Level 5180 keystrokes
UNION ALL combines the active customers and the active suppliers into one result, labelled by type and sorted by name; unlike UNION, it keeps duplicate rows and skips the duplicate check.
SQL code
-- First query: active customers1SELECT2 name,3 email, -- A constant column labels where each row comes from4 'customer' AS type5FROM6 customers-- Keeps only rows whose status is 'active'7WHERE8 status = 'active'-- UNION ALL appends all rows of the next query and keeps duplicates-- It is faster than UNION because it skips the duplicate check9UNION ALL-- Second query: active suppliers, with the same columns in the same order10SELECT11 name,12 email, -- These rows are labelled 'supplier'13 'supplier' AS type14FROM15 suppliers-- Again only active rows16WHERE17 status = 'active'-- ORDER BY sorts the combined result: Acme, Alice, Bob18ORDER BY19 name;Output
+-------+-------------------+----------+ | name | email | type | +-------+-------------------+----------+ | Acme | acme@example.com | supplier | | Alice | alice@example.com | customer | | Bob | bob@example.com | customer | +-------+-------------------+----------+ 3 rows in set
Practice typing this code. Results are not recorded in the rankings.
Symbols in this code
The symbols you type in this SQL code and how often they appear.
'× 8,× 4=× 2;× 1