0
0
MySQLquery~20 mins

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

Choose your learning style9 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.