Consider two tables, TableA and TableB, each with a single column value. TableA contains values (1, 2, 3) and TableB contains values (3, 4, 5). What will be the result of the following SQL query?
SELECT value FROM TableA UNION SELECT value FROM TableB;
SELECT value FROM TableA UNION SELECT value FROM TableB;
Remember that UNION removes duplicate rows.
The UNION operation combines rows from both tables and removes duplicates. Since 3 appears in both tables, it appears only once in the result.
Given the same tables TableA (values 1, 2, 3) and TableB (values 3, 4, 5), what is the output of this query?
SELECT value FROM TableA INTERSECT SELECT value FROM TableB;
SELECT value FROM TableA INTERSECT SELECT value FROM TableB;
INTERSECT returns only the common rows between both tables.
Only the value 3 is present in both TableA and TableB, so the result contains only 3.
Using the same tables TableA (values 1, 2, 3) and TableB (values 3, 4, 5), what does this query return?
SELECT value FROM TableA EXCEPT SELECT value FROM TableB;
SELECT value FROM TableA EXCEPT SELECT value FROM TableB;
EXCEPT returns rows from the first table that are not in the second table.
Values 1 and 2 are in TableA but not in TableB, so they are returned.
What is the difference in output between these two queries given TableA (1, 2, 3) and TableB (3, 4, 5)?
Query 1: SELECT value FROM TableA UNION SELECT value FROM TableB; Query 2: SELECT value FROM TableA UNION ALL SELECT value FROM TableB;
SELECT value FROM TableA UNION SELECT value FROM TableB; SELECT value FROM TableA UNION ALL SELECT value FROM TableB;
Think about how UNION and UNION ALL treat duplicate rows.
UNION removes duplicate rows, while UNION ALL includes all rows from both tables, including duplicates.
Given two tables, TableX with 5 unique rows and TableY with 7 unique rows, where 3 rows are common to both tables, how many rows will the following query return?
SELECT * FROM TableX INTERSECT SELECT * FROM TableY;
SELECT * FROM TableX INTERSECT SELECT * FROM TableY;
INTERSECT returns only rows present in both tables.
Since 3 rows are common, the INTERSECT returns exactly those 3 rows.