Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the basic rule for column matching in SQL set operations like UNION?
The number of columns must be the same in all queries, and the data types of corresponding columns should be compatible.
Click to reveal answer
beginner
Can column names differ in the SELECT statements used in a UNION operation?
Yes, column names can differ. The result set uses the column names from the first SELECT statement.
Click to reveal answer
intermediate
Why must data types be compatible in set operations?
Because SQL combines rows from different queries into one result, incompatible data types can cause errors or unexpected results.
Click to reveal answer
beginner
What happens if the number of columns differs between SELECT statements in a UNION?
The SQL query will fail with an error because the columns do not match in number.
Click to reveal answer
intermediate
How does SQL handle NULL values in set operations when columns match?
NULL values are treated as valid data and included in the result set where they appear in any of the combined queries.
Click to reveal answer
In a UNION operation, what must be true about the SELECT statements?
AThey must have the same number of columns with compatible data types.
BThey must have the same column names.
CThey must select from the same table.
DThey must use the same WHERE clause.
✗ Incorrect
The number of columns and their data types must match, but column names and tables can differ.
If the first SELECT statement has columns (id, name) and the second has (user_id, username), what will be the column names in the UNION result?
Auser_id, username
Bid, name
Cid, username
Duser_id, name
✗ Incorrect
The result set uses the column names from the first SELECT statement.
What error occurs if SELECT statements in a UNION have different numbers of columns?
ANo error, SQL adjusts automatically
BData type mismatch error
CSyntax error
DColumn count mismatch error
✗ Incorrect
SQL requires the same number of columns; otherwise, it raises a column count mismatch error.
Are data types required to be exactly the same in set operations?
AYes, exactly the same
BNo, any data types are allowed
CNo, they must be compatible
DOnly numeric types must match
✗ Incorrect
Data types must be compatible so SQL can combine the columns without errors.
How does SQL treat NULL values in set operations?
AIt treats NULL as a valid value and includes it
BIt ignores NULL values
CIt converts NULL to zero
DIt causes an error
✗ Incorrect
NULL values are included as valid data in the combined result.
Explain the rules for matching columns in SQL set operations like UNION or INTERSECT.
Think about how SQL combines rows from multiple queries.
You got /4 concepts.
What errors or issues can arise if column matching rules are not followed in set operations?
Consider what happens if SQL tries to combine incompatible columns.
You got /4 concepts.
Practice
(1/5)
1.
Which rule must be followed when using SQL set operations like UNION or INTERSECT?
easy
A. Both queries must have the same number of columns with compatible data types.
B. The second query must have more columns than the first.
C. Column names must be identical in both queries.
D. Set operations only work with numeric columns.
Solution
Step 1: Understand set operation requirements
Set operations combine results from multiple queries, so columns must match in number and type to align data correctly.
Step 2: Check column name and type rules
Column names do not need to match; only the first query's column names are used. Data types must be compatible, and the number of columns must be the same.
Final Answer:
Both queries must have the same number of columns with compatible data types. -> Option A
Quick Check:
Column count and type compatibility = A [OK]
Hint: Match column count and types for set operations [OK]
Common Mistakes:
Assuming column names must match
Using different number of columns
Trying set operations on incompatible data types
2.
Which of the following SQL queries correctly uses UNION with matching columns?
-- Table A: (id INT, name VARCHAR) -- Table B: (user_id INT, username VARCHAR)
A) SELECT id, name FROM A UNION SELECT user_id, username FROM B; B) SELECT id FROM A UNION SELECT user_id, username FROM B; C) SELECT id, name FROM A UNION SELECT user_id FROM B; D) SELECT id, name FROM A UNION SELECT user_id, username, email FROM B;
easy
A. SELECT id, name FROM A UNION SELECT user_id FROM B;
B. SELECT id FROM A UNION SELECT user_id, username FROM B;
C. SELECT id, name FROM A UNION SELECT user_id, username FROM B;
D. SELECT id, name FROM A UNION SELECT user_id, username, email FROM B;
Solution
Step 1: Check column counts in each query
SELECT id, name FROM A UNION SELECT user_id, username FROM B; selects 2 columns from both queries, matching counts. Other options have mismatched column counts (1 vs 2, 2 vs 1, or 2 vs 3).
Step 2: Verify column types compatibility
Columns in SELECT id, name FROM A UNION SELECT user_id, username FROM B; are INT and VARCHAR in both queries, which are compatible.
Final Answer:
SELECT id, name FROM A UNION SELECT user_id, username FROM B; -> Option C
Quick Check:
Equal columns and compatible types = A [OK]
Hint: Count columns and check types match in both queries [OK]
Common Mistakes:
Mismatched column counts cause errors
Ignoring column type compatibility
Assuming extra columns are allowed
3.
Given the tables and query below, what will be the output?
Table X: id | value 1 | 'A' 2 | 'B'
Table Y: code | val 2 | 'B' 3 | 'C'
Query: SELECT id, value FROM X UNION SELECT code, val FROM Y;
medium
A. Syntax error due to column name mismatch
B. [ (1, 'A'), (2, 'B'), (3, 'C') ]
C. [ (1, 'A'), (3, 'C') ]
D. [ (1, 'A'), (2, 'B'), (2, 'B'), (3, 'C') ]
Solution
Step 1: Check column counts and types
Both queries select 2 columns with compatible types (INT and VARCHAR). Column names differ but that is allowed.
Step 2: Understand UNION behavior
UNION removes duplicates. Rows (2, 'B') appear in both tables, so only one copy appears in result.
Final Answer:
[ (1, 'A'), (2, 'B'), (3, 'C') ] -> Option B
Quick Check:
UNION removes duplicates = D [OK]
Hint: UNION removes duplicates, column names don't matter [OK]
Common Mistakes:
Expecting duplicate rows in UNION result
Thinking column names must match
Confusing UNION with UNION ALL
4.
Identify the error in the following SQL set operation:
SELECT id, name FROM Customers UNION SELECT customer_id FROM Orders;
medium
A. Column names do not match between queries.
B. UNION cannot be used with SELECT statements.
C. Data types of columns are incompatible.
D. The second query has fewer columns than the first.
Solution
Step 1: Compare column counts in both queries
The first query selects 2 columns (id, name), the second selects only 1 column (customer_id). This mismatch causes an error.
Step 2: Check other possible errors
Column names do not need to match, and UNION works with SELECT statements. Data types are unknown but column count mismatch is enough to cause error.
Final Answer:
The second query has fewer columns than the first. -> Option D
Quick Check:
Column count mismatch = B [OK]
Hint: Ensure both queries select same number of columns [OK]
You want to combine employee and manager names into one list using a set operation. Which query correctly applies set operation column matching rules?
A) SELECT emp_id, emp_name FROM Employees UNION SELECT manager_id, manager_name FROM Managers; B) SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers; C) SELECT emp_name, dept FROM Employees UNION SELECT manager_name, manager_id FROM Managers; D) SELECT emp_id, emp_name, dept FROM Employees UNION SELECT manager_id, manager_name FROM Managers;
hard
A. SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers;
B. SELECT emp_id, emp_name FROM Employees UNION SELECT manager_id, manager_name FROM Managers;
C. SELECT emp_name, dept FROM Employees UNION SELECT manager_name, manager_id FROM Managers;
D. SELECT emp_id, emp_name, dept FROM Employees UNION SELECT manager_id, manager_name FROM Managers;
Solution
Step 1: Identify columns to combine
You want a list of names only, so selecting one column (name) from each table is appropriate.
Step 2: Check column counts and types
SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers; selects one VARCHAR column from each table, matching count and compatible types. Other options have mismatched column counts or incompatible columns.
Final Answer:
SELECT emp_name FROM Employees UNION SELECT manager_name FROM Managers; -> Option A
Quick Check:
Same column count and type for names = B [OK]
Hint: Select same number and type of columns to combine [OK]