0
0
SQLquery~5 mins

UNION combining result sets in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the SQL UNION operator do?
The UNION operator combines the results of two or more SELECT queries into a single result set, removing duplicate rows.
Click to reveal answer
beginner
How does UNION differ from UNION ALL?
UNION removes duplicate rows from the combined result, while UNION ALL includes all rows, even duplicates.
Click to reveal answer
intermediate
What must be true about the SELECT statements used with UNION?
Each SELECT must have the same number of columns, and the columns must have compatible data types in the same order.
Click to reveal answer
intermediate
Can you use ORDER BY with UNION? If yes, where?
Yes, ORDER BY can be used only once at the end of the entire UNION query to sort the combined result set.
Click to reveal answer
intermediate
What happens if the SELECT statements in a UNION have different column names?
The column names in the final result come from the first SELECT statement; later SELECTs' column names are ignored.
Click to reveal answer
What does the UNION operator do in SQL?
ACombines results and keeps duplicates
BFilters rows based on a condition
CJoins tables based on a key
DCombines results and removes duplicates
Which of the following is true about SELECT statements in a UNION?
AThey must have the same number of columns
BThey can have different numbers of columns
CThey must have the same column names
DThey must come from the same table
What is the difference between UNION and UNION ALL?
AUNION ALL removes duplicates; UNION keeps duplicates
BUNION removes duplicates; UNION ALL keeps duplicates
CUNION sorts results; UNION ALL does not
DUNION works only with two SELECTs; UNION ALL works with many
Where can you place ORDER BY when using UNION?
AOnly at the end of the entire UNION query
BAfter each SELECT statement
CBefore the first SELECT
DORDER BY is not allowed with UNION
If the first SELECT has columns (id, name) and the second SELECT has columns (user_id, username), what will the final column names be after UNION?
Auser_id, name
Buser_id, username
Cid, name
Did, username
Explain how the UNION operator works and what rules must be followed when combining SELECT statements.
Think about how two lists of data can be merged without repeats.
You got /5 concepts.
    Describe the difference between UNION and UNION ALL and when you might use each.
    Consider if you want to keep or remove repeated rows.
    You got /4 concepts.