Going Deeper: Professional SQLExtra· 30 min read

DISTINCT, UNION & GROUP_CONCAT

Remove duplicate rows with DISTINCT, stack two result sets on top of each other with UNION, and roll many values into one cell with GROUP_CONCAT.

What you will learn

  • Remove duplicate rows from a result with DISTINCT
  • Combine two result sets vertically with UNION and UNION ALL
  • Collapse grouped values into one string with GROUP_CONCAT

Three tools for shaping result sets

So far every query returned rows exactly as they were stored. This lesson adds three tools that reshape the result set itself: DISTINCT drops repeated rows, UNION stacks one query’s rows underneath another’s, and GROUP_CONCAT squeezes many values from a group into a single comma-separated cell. They turn raw rows into the tidy lists and combined reports real apps display.

We will use this orders table for the examples:

iduser_idcitytotal
1011Mumbai500
1022Delhi250
1031Mumbai900
1043Delhi300
1052Mumbai150

DISTINCT — one row per unique value

A plain SELECT city FROM orders would list Mumbai three times and Delhi twice — once per order. When you only want the list of distinct cities, you add DISTINCT right after SELECT. It keeps just one copy of each unique value (or each unique combination, if you list several columns).

List each city only once
SELECT DISTINCT city FROM orders;

DISTINCT looks at the city value of every row and throws away repeats, so you get a clean list of the unique cities rather than one entry per order. If you wrote SELECT DISTINCT city, user_id, MySQL would keep one row per unique pair of city and user instead.

Note: Output: city Mumbai Delhi The five order rows collapsed to the two cities that appear. DISTINCT is the standard way to answer “what are the different values in this column?” — handy for building dropdown lists of categories, cities or statuses.

UNION — stack two result sets vertically

A JOIN glues tables side by side (more columns). UNION does the opposite — it stacks two query results on top of each other (more rows). The only rule is that both SELECT statements must return the same number of columns, in a compatible order. It is perfect for combining lists from different tables into one column, such as merging a list of customers and a list of suppliers into a single contact list.

Imagine a separate suppliers table with a name column, and you want one combined list of every customer name and every supplier name:

Combine names from two tables into one list
SELECT name FROM users
UNION
SELECT name FROM suppliers;

Each SELECT produces a list of names; UNION places the second list under the first and returns them as one result. By default UNION also removes duplicates — if a name appears in both tables, it shows once. If you want to keep every row including duplicates (which is faster, because MySQL skips the de-duplication step), use UNION ALL instead.

Note: Output (sample): name Asha Ravi Meera Globex Pvt Ltd Acme Supplies The customer names and supplier names became one combined list. Use UNION when you want a single de-duplicated list, and UNION ALL when you want every row (and a little more speed). Remember: UNION combines rows; JOIN combines columns.

GROUP_CONCAT — many values into one cell

With GROUP BY you get one row per group, but sometimes you want to see the actual values in each group, not just a count. GROUP_CONCAT is an aggregate function that joins all the values in a group into a single comma-separated string — ideal for “list the order ids each customer placed” on one line.

Show all of each user’s order ids in one cell
SELECT user_id, GROUP_CONCAT(id ORDER BY id) AS order_ids
FROM orders
GROUP BY user_id;

Reading it: GROUP BY user_id forms one group per user as usual. Inside each group, GROUP_CONCAT(id ORDER BY id) takes every order id in that group and joins them into one string, sorted by id. So instead of three separate rows for user 1’s orders, you get a single cell reading 101,103.

Note: Output: userid orderids 1 101,103 2 102,105 3 104 Each user’s order ids are rolled into one comma-separated cell. You can change the separator with GROUP_CONCAT(id SEPARATOR ' | '). This is exactly how an app shows “Orders: 101, 103” next to a customer’s name without running a second query.

Tip: Quick mental map: DISTINCT = unique values in a column, UNION = stack two queries into more rows, GROUP_CONCAT = squeeze a group’s values into one cell. All three are about shaping the result set, not the stored data.

Q. You have two SELECT queries and want their rows combined into one list, with duplicate rows removed. Which keyword?

Answer: UNION stacks two result sets and removes duplicate rows. UNION ALL keeps duplicates; JOIN combines columns side by side; GROUP_CONCAT joins values within a group into one string.

✍️ Practice

  1. Use DISTINCT to list the unique cities (or categories) in one of your tables.
  2. Use GROUP_CONCAT to show all order ids (or product names) for each customer on a single line.

🏠 Homework

  1. Write a UNION that combines names from two different tables into one de-duplicated contact list.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →