Given two tables TableA and TableB with a single column id, what is the result of this query?
SELECT id FROM TableA EXCEPT SELECT id FROM TableB;
Assume:
TableA: {1, 2, 3, 4}
TableB: {3, 4, 5}SELECT id FROM TableA EXCEPT SELECT id FROM TableB;
EXCEPT returns rows from the first query that are not in the second.
The query returns ids in TableA that are not in TableB. Since 1 and 2 are only in TableA, the result is {1, 2}.
Consider the following two queries on tables Employees and Managers with column employee_id:
Query 1: SELECT employee_id FROM Employees EXCEPT SELECT employee_id FROM Managers;
Query 2: SELECT employee_id FROM Employees WHERE employee_id NOT IN (SELECT employee_id FROM Managers);
Which statement is true about their results?
Think about how EXCEPT and NOT IN handle duplicates and NULLs.
Both queries return the set of employee_ids in Employees but not in Managers. EXCEPT removes duplicates by default, and NOT IN also filters out those in Managers. If Managers.employee_id has NULLs, NOT IN may behave differently, but assuming no NULLs, results match.
Which of the following SQL queries will cause a syntax error?
Check if the columns in both SELECT statements match in number and type.
Option D has mismatched columns: first SELECT returns 'id', second returns 'name'. EXCEPT requires same number and compatible types of columns.
You want to find rows in Orders that are not in ArchivedOrders. Which query is generally more efficient?
Consider how joins and indexes affect performance.
Option C uses a LEFT JOIN with IS NULL filter, which is often optimized well by databases using indexes. EXCEPT and NOT IN can be less efficient on large datasets.
Given two tables T1 and T2 with a single column val:
T1: {1, 2, 2, NULL}
T2: {2, NULL}What is the result of the query:
SELECT val FROM T1 EXCEPT SELECT val FROM T2;
SELECT val FROM T1 EXCEPT SELECT val FROM T2;
Remember how EXCEPT treats duplicates and NULL values.
EXCEPT removes duplicates and excludes any values present in the second query. NULLs are treated as equal, so NULL in T1 is excluded because NULL is in T2. The only value left is 1.