What if you could instantly see all your data together without copying and pasting?
Why combining result sets is useful in MySQL - The Real Reasons
Imagine you have two lists of friends from different social events written on separate pieces of paper. You want to see all your friends together in one list, but you have to manually copy and merge these lists every time you want to check.
Manually copying and merging lists is slow and easy to mess up. You might miss some names, write duplicates, or spend too much time updating the combined list whenever a new friend is added to either event.
Combining result sets in a database lets you automatically merge data from different tables or queries into one neat list. This saves time, avoids mistakes, and keeps your combined data always up to date.
SELECT name FROM friends_event1; -- then copy and paste results SELECT name FROM friends_event2; -- then copy and paste results again
SELECT name FROM friends_event1 UNION SELECT name FROM friends_event2;
It makes it easy to see and work with all related data together, no matter where it originally came from.
A company wants to see all customers who bought products online and in-store. Combining these two lists with a single query helps them understand total sales and customer behavior quickly.
Manual merging is slow and error-prone.
Combining result sets automates and simplifies data merging.
This helps keep data accurate and up to date.
Practice
UNION or UNION ALL useful in MySQL?Solution
Step 1: Understand the purpose of combining result sets
Combining result sets lets you merge rows from different tables or queries into one output.Step 2: Identify the benefit of combined data
This helps to view or report data together without changing the original tables.Final Answer:
It allows you to see data from multiple tables together in one list. -> Option AQuick Check:
Combining result sets = viewing merged data [OK]
- Thinking UNION speeds up queries automatically
- Believing UNION deletes data permanently
- Confusing combining with creating new tables
Solution
Step 1: Recall syntax for combining queries without duplicates
TheUNIONkeyword combines results and removes duplicates.Step 2: Identify correct syntax
The correct syntax isSELECT ... UNION SELECT ....UNION ALLkeeps duplicates, JOIN and AND are incorrect here.Final Answer:
SELECT * FROM table1 UNION SELECT * FROM table2; -> Option AQuick Check:
UNION = combine without duplicates [OK]
- Using JOIN instead of UNION to combine result sets
- Confusing UNION ALL with UNION
- Using AND keyword incorrectly
table1 has rows: (1, 'Apple'), (2, 'Banana')table2 has rows: (2, 'Banana'), (3, 'Cherry')What is the result of this query?
SELECT id, name FROM table1 UNION SELECT id, name FROM table2;
Solution
Step 1: Understand UNION removes duplicates
The row (2, 'Banana') appears in both tables but will appear only once in the result.Step 2: Combine unique rows from both tables
Rows (1, 'Apple') and (3, 'Cherry') are unique and included as is.Final Answer:
[(1, 'Apple'), (2, 'Banana'), (3, 'Cherry')] -> Option CQuick Check:
UNION removes duplicates, so only one (2, 'Banana') [OK]
- Expecting duplicate rows to appear twice
- Confusing UNION with UNION ALL output
- Ignoring that UNION sorts and removes duplicates
SELECT * FROM table1 UNION SELECT * FROM table2;
Solution
Step 1: Understand UNION behavior
UNION removes duplicate rows from combined results.Step 2: Identify correct way to keep duplicates
To keep duplicates, use UNION ALL instead of UNION.Final Answer:
It will remove duplicate rows, so duplicates are lost. -> Option DQuick Check:
UNION removes duplicates, use UNION ALL to keep them [OK]
- Thinking UNION keeps duplicates
- Believing UNION requires ORDER BY
- Confusing UNION with JOIN behavior
sales_2023 and sales_2024. Both have columns product_id and units_sold. You want a report showing all sales from both years including duplicates, but also want to sort by units_sold descending. Which query correctly achieves this?Solution
Step 1: Choose UNION ALL to keep duplicates
Since duplicates must be included, UNION ALL is needed.Step 2: Apply ORDER BY after the combined query
ORDER BY must be applied to the entire combined result, so it goes after both SELECTs.Step 3: Use column position or name in ORDER BY
UsingORDER BY 2 DESCsorts by the second columnunits_solddescending correctly.Final Answer:
SELECT product_id, units_sold FROM sales_2023 UNION ALL SELECT product_id, units_sold FROM sales_2024 ORDER BY 2 DESC; -> Option BQuick Check:
UNION ALL + ORDER BY after combined query = correct [OK]
- Using UNION instead of UNION ALL loses duplicates
- Placing ORDER BY inside first SELECT only
- Not sorting combined result properly
Ready to prove it?
Pass the test and this pattern moves unknown
