0
0
SQLquery~5 mins

Set operations with ORDER BY in SQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are set operations in SQL?
Set operations combine results from two or more SELECT queries into a single result. Common set operations include UNION, INTERSECT, and EXCEPT.
Click to reveal answer
beginner
How does ORDER BY work with set operations?
ORDER BY sorts the final combined result of the set operation. It must be placed after all SELECT statements and set operators.
Click to reveal answer
intermediate
Can you use ORDER BY inside each SELECT of a set operation?
No, ORDER BY inside individual SELECTs is ignored unless you use TOP or LIMIT. The final ORDER BY after the set operation controls sorting.
Click to reveal answer
beginner
What happens if you use UNION without ORDER BY?
The combined result will have unique rows but the order is not guaranteed. To see sorted results, use ORDER BY after the UNION.
Click to reveal answer
beginner
Write a simple SQL query using UNION and ORDER BY.
Example: SELECT name FROM students UNION SELECT name FROM teachers ORDER BY name ASC;
Click to reveal answer
Where should ORDER BY be placed when using set operations like UNION?
ABetween the SELECT statements
BAfter the last SELECT statement and set operation
CBefore the first SELECT statement
DInside each SELECT statement
What does UNION do in SQL?
ACombines results and removes duplicates
BCombines results and keeps duplicates
CReturns only common rows
DReturns rows from the first query only
Can ORDER BY inside individual SELECT statements affect the final output order in a UNION?
AYes, it controls the final order
BNo, ORDER BY is ignored everywhere
CYes, but only in some databases
DNo, only the final ORDER BY after UNION affects order
Which set operation returns only rows common to both queries?
AINTERSECT
BUNION
CEXCEPT
DJOIN
What will happen if you omit ORDER BY after a UNION?
AQuery will fail
BResult rows will be sorted by default
CResult rows will not be sorted
DDuplicates will appear
Explain how to use ORDER BY with set operations like UNION in SQL.
Think about where the sorting happens after combining results.
You got /3 concepts.
    Describe the difference between UNION and INTERSECT and how ORDER BY affects their results.
    Focus on what rows each set operation returns and when sorting applies.
    You got /3 concepts.