Before you start, you need
Transform text with functions · Join text values into a readable column · Create categories with CASE · Combine results with UNION
The problem
You will combine customers and employees in one list. Each row will show where the person comes from, their name and a calculated label. If someone were in both tables, they would appear once per source.
Start with one query for customers and another for employees. Both must put the columns in the same order: source, name and group_label. The fixed text customer or employee shows where each row came from. If you wanted a more readable label, you could join source and name with ||, as in the concatenation lesson.
Apply the text function and the CASE in each query, using its own table's attributes. Check the two lists separately before combining them with UNION ALL, which keeps every occurrence.
Finally, sort the whole list. In the challenge, the name format and the label rules will change; keep the positions and meaning of the columns.
How to think about it
Why is it worth including source if the same name could be in both customers and employees?
- Prepare each branch with source and name in the same positions. The fixed text tells you where each occurrence comes from.
- Apply the text function in each source before combining them. Then add the classification specific to that table.
- UNION ALL keeps every occurrence. The challenge uses a different format and different group rules, keeping the same structure.
Guided practice
Getting things wrong is part of learning: you can run your query as many times as you like and nothing is lost. If you get stuck, open a hint (each one reveals a bit more) and save the solution for last.
Guided practice
Prepare the first branch
Return 'customer' as source and LOWER(name) as name from customers.
Expected result: 12 rows with the columns source, name, in that order. Row order does not matter.
View tables and data
Open this panel to see the full data.
Prepare only the customers branch, with a source label.
The label customer is fixed text; the function applies to name.
Return source first, then LOWER(name) AS name.
Solution explained
SELECT 'customer' AS source, LOWER(name) AS name FROM customers;Two columns make a compatible branch, without mixing sources yet.
Combine and classify people
Guided practice
Combine and classify people
Combine every customer and employee with UNION ALL. Return source ('customer' or 'employee'), name in lower case and group_label ('priority' for customers in London or employees in Sales, and 'other' otherwise). Sort by group_label, source and name.
Expected result: 22 rows with the columns source, name, group_label, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
Prepare the same columns in both branches, and classify using each source's attributes.
Repeat the structure for employees and change the CASE condition to department = 'Sales'.
Combine with UNION ALL and put ORDER BY group_label, source, name at the end.
Solution explained
SELECT 'customer' AS source,
LOWER(name) AS name,
CASE WHEN city = 'London' THEN 'priority' ELSE 'other' END AS group_label
FROM customers
UNION ALL
SELECT 'employee' AS source,
LOWER(name) AS name,
CASE WHEN department = 'Sales' THEN 'priority' ELSE 'other' END AS group_label
FROM employees
ORDER BY group_label, source, name;Each SELECT produces the same structure; CASE applies its source's rule, LOWER normalises the name and ORDER BY arranges the combined result.
Challenge
Solve this variation. Spot the part that has changed before you look for help.
Practice
Combine a variant
Combine customers and employees with UNION ALL. Return source ('customer' or 'employee'), UPPER(name) as name and a CASE as group_label: 'priority' for customers with active = 1 and for employees in Sales; 'other' for everyone else. Sort by source and name.
Expected result: 22 rows with the columns source, name, group_label, in that order. Keep the row order requested.
View tables and data
Open this panel to see the full data.
The classification criteria change depending on the source.
Prepare three compatible columns in each branch.
Write a complete CASE in each SELECT, and a single ORDER BY at the end.
Solution explained
SELECT 'customer' AS source, UPPER(name) AS name, CASE WHEN active = 1 THEN 'priority' ELSE 'other' END AS group_label FROM customers UNION ALL SELECT 'employee' AS source, UPPER(name) AS name, CASE WHEN department = 'Sales' THEN 'priority' ELSE 'other' END AS group_label FROM employees ORDER BY source, name;Each branch applies its own rule, but group_label means the same thing in both: priority or other. That way the combined column can be read from top to bottom.
Key takeaways
- The branches must share positions and meaning.
- Classify using each source's data.
- UNION ALL keeps every occurrence.
- A final ORDER BY sorts the composite list.