Bird
Raised Fist0
MySQLquery~20 mins

Why combining result sets is useful in MySQL - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Combining Result Sets
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use UNION to combine result sets?

Imagine you have two tables: Customers_US and Customers_EU. You want to get a list of all customers from both tables without duplicates. Why is combining result sets with UNION useful here?

AIt duplicates all rows from both tables, including repeated customers.
BIt creates a new table combining both tables permanently.
CIt only shows customers from the first table and ignores the second.
DIt merges rows from both tables and removes duplicates, giving a single list of unique customers.
Attempts:
2 left
💡 Hint

Think about how to get a combined list without repeated entries.

❓ query_result
intermediate
2:00remaining
What is the output of this UNION query?

Given two tables:

Table A:
id | name
1 | Alice
2 | Bob

Table B:
id | name
2 | Bob
3 | Carol

What is the result of this query?

SELECT id, name FROM A
UNION
SELECT id, name FROM B;
A2 | Bob<br>3 | Carol
B1 | Alice<br>2 | Bob<br>3 | Carol
C1 | Alice<br>2 | Bob<br>2 | Bob<br>3 | Carol
D1 | Alice<br>3 | Carol
Attempts:
2 left
💡 Hint

Remember that UNION removes duplicates.

📝 Syntax
advanced
2:00remaining
Which query correctly combines two SELECTs with UNION ALL?

You want to combine all rows from two tables including duplicates. Which query is correct?

ASELECT name FROM Customers_US UNION ALL SELECT name FROM Customers_EU;
BSELECT name FROM Customers_US UNION SELECT name FROM Customers_EU ALL;
CSELECT name FROM Customers_US ALL UNION SELECT name FROM Customers_EU;
DSELECT name FROM Customers_US UNIONALL SELECT name FROM Customers_EU;
Attempts:
2 left
💡 Hint

Check the correct syntax for UNION ALL.

❓ optimization
advanced
2:00remaining
How does UNION ALL improve performance over UNION?

You have two large tables and want to combine their rows including duplicates. Why might UNION ALL be faster than UNION?

ABecause UNION ALL skips the step of removing duplicates, saving time.
BBecause UNION ALL creates indexes automatically.
CBecause UNION ALL sorts the results faster than UNION.
DBecause UNION ALL compresses data before combining.
Attempts:
2 left
💡 Hint

Think about what extra work UNION does compared to UNION ALL.

🔧 Debug
expert
2:00remaining
Why does this UNION query cause an error?

Consider these two tables:

Employees: id INT, name VARCHAR(50)

Departments: dept_id INT, dept_name VARCHAR(50)

What error will this query cause?

SELECT id, name FROM Employees
UNION
SELECT dept_id FROM Departments;
AError: UNION cannot combine different data types.
BError: Column names must match exactly in both SELECTs.
CError: The number of columns in each SELECT must be the same.
DNo error, query runs successfully.
Attempts:
2 left
💡 Hint

Check how many columns each SELECT returns.

Practice

(1/5)
1. Why is combining result sets using UNION or UNION ALL useful in MySQL?
easy
A. It allows you to see data from multiple tables together in one list.
B. It speeds up the database server automatically.
C. It deletes duplicate rows from all tables permanently.
D. It creates new tables with combined data.

Solution

  1. Step 1: Understand the purpose of combining result sets

    Combining result sets lets you merge rows from different tables or queries into one output.
  2. Step 2: Identify the benefit of combined data

    This helps to view or report data together without changing the original tables.
  3. Final Answer:

    It allows you to see data from multiple tables together in one list. -> Option A
  4. Quick Check:

    Combining result sets = viewing merged data [OK]
Hint: Combining shows data together, not deleting or creating tables [OK]
Common Mistakes:
  • Thinking UNION speeds up queries automatically
  • Believing UNION deletes data permanently
  • Confusing combining with creating new tables
2. Which of the following is the correct syntax to combine two SELECT queries without duplicates in MySQL?
easy
A. SELECT * FROM table1 UNION SELECT * FROM table2;
B. SELECT * FROM table1 JOIN SELECT * FROM table2;
C. SELECT * FROM table1 UNION ALL SELECT * FROM table2;
D. SELECT * FROM table1 AND SELECT * FROM table2;

Solution

  1. Step 1: Recall syntax for combining queries without duplicates

    The UNION keyword combines results and removes duplicates.
  2. Step 2: Identify correct syntax

    The correct syntax is SELECT ... UNION SELECT .... UNION ALL keeps duplicates, JOIN and AND are incorrect here.
  3. Final Answer:

    SELECT * FROM table1 UNION SELECT * FROM table2; -> Option A
  4. Quick Check:

    UNION = combine without duplicates [OK]
Hint: UNION removes duplicates; use UNION ALL to keep them [OK]
Common Mistakes:
  • Using JOIN instead of UNION to combine result sets
  • Confusing UNION ALL with UNION
  • Using AND keyword incorrectly
3. Given two tables:
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;
medium
A. [(2, 'Banana'), (3, 'Cherry')]
B. [(1, 'Apple'), (2, 'Banana'), (2, 'Banana'), (3, 'Cherry')]
C. [(1, 'Apple'), (2, 'Banana'), (3, 'Cherry')]
D. [(1, 'Apple'), (3, 'Cherry')]

Solution

  1. Step 1: Understand UNION removes duplicates

    The row (2, 'Banana') appears in both tables but will appear only once in the result.
  2. Step 2: Combine unique rows from both tables

    Rows (1, 'Apple') and (3, 'Cherry') are unique and included as is.
  3. Final Answer:

    [(1, 'Apple'), (2, 'Banana'), (3, 'Cherry')] -> Option C
  4. Quick Check:

    UNION removes duplicates, so only one (2, 'Banana') [OK]
Hint: UNION removes duplicates; expect unique rows only [OK]
Common Mistakes:
  • Expecting duplicate rows to appear twice
  • Confusing UNION with UNION ALL output
  • Ignoring that UNION sorts and removes duplicates
4. What is wrong with this query if you want to combine all rows from two tables including duplicates?
SELECT * FROM table1 UNION SELECT * FROM table2;
medium
A. It will create a new table instead of showing results.
B. It will cause a syntax error because UNION needs ORDER BY.
C. It will combine rows but only from the first table.
D. It will remove duplicate rows, so duplicates are lost.

Solution

  1. Step 1: Understand UNION behavior

    UNION removes duplicate rows from combined results.
  2. Step 2: Identify correct way to keep duplicates

    To keep duplicates, use UNION ALL instead of UNION.
  3. Final Answer:

    It will remove duplicate rows, so duplicates are lost. -> Option D
  4. Quick Check:

    UNION removes duplicates, use UNION ALL to keep them [OK]
Hint: UNION removes duplicates; use UNION ALL to keep duplicates [OK]
Common Mistakes:
  • Thinking UNION keeps duplicates
  • Believing UNION requires ORDER BY
  • Confusing UNION with JOIN behavior
5. You have two tables: 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?
hard
A. SELECT product_id, units_sold FROM sales_2023 UNION SELECT product_id, units_sold FROM sales_2024 ORDER BY units_sold DESC;
B. SELECT product_id, units_sold FROM sales_2023 UNION ALL SELECT product_id, units_sold FROM sales_2024 ORDER BY 2 DESC;
C. SELECT product_id, units_sold FROM sales_2023 UNION ALL SELECT product_id, units_sold FROM sales_2024 ORDER BY units_sold ASC;
D. SELECT product_id, units_sold FROM sales_2023 ORDER BY units_sold DESC UNION ALL SELECT product_id, units_sold FROM sales_2024;

Solution

  1. Step 1: Choose UNION ALL to keep duplicates

    Since duplicates must be included, UNION ALL is needed.
  2. 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.
  3. Step 3: Use column position or name in ORDER BY

    Using ORDER BY 2 DESC sorts by the second column units_sold descending correctly.
  4. 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 B
  5. Quick Check:

    UNION ALL + ORDER BY after combined query = correct [OK]
Hint: Use UNION ALL then ORDER BY after both selects [OK]
Common Mistakes:
  • Using UNION instead of UNION ALL loses duplicates
  • Placing ORDER BY inside first SELECT only
  • Not sorting combined result properly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard