Module 5 · Advanced queries

Practice: combine UNION, CASE and functions

By the end you will be able to prepare two compatible lists and combine them with tags.

About 25 min

0 of 52 lessons

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

Not started

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.

Combine and classify people

Guided practice

Combine and classify people

Not started

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.

Challenge

Solve this variation. Spot the part that has changed before you look for help.

Practice

Combine a variant

Not started

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.

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.

Next: Save reusable queries with views