CHAPTER 6: Building on Subqueries, Common Table Expressions, and Unions Flashcards
Q: What is a subquery in T-SQL?
A: A subquery is a nested query—a query within another query—that can return results for use in the main query.
Q: Where can subqueries be used in a SQL query?
A: Subqueries can be used in the SELECT, FROM, and WHERE clauses, or as part of other expressions.
Q: How is a subquery used in an IN list?
A: A subquery can replace a hard-coded list in the WHERE clause using the syntax:
SELECT <columns>
FROM <table>
WHERE <column> IN (SELECT <column> FROM <table>);</column></column></columns>
Q: What is the difference between joining tables and using a subquery in the WHERE clause?
A: A join includes columns from both tables in the result, while a subquery returns a filtered list of values for comparison in the main query.
Q: How do you use a subquery to find rows not in another table?
A: Add NOT before IN in the query:
SELECT <columns>
FROM <table1>
WHERE <column> NOT IN (SELECT <column> FROM <table2>);</table2></column></column></table1></columns>
Q: What happens if a subquery returns a NULL in a NOT IN clause?
A: If the subquery returns any NULL values, the NOT IN clause will return no rows due to SQL’s three-valued logic (TRUE, FALSE, UNKNOWN).
Q: How can you handle NULL values in a subquery?
A: Add a WHERE condition in the subquery to exclude NULL values:
SELECT <columns>
FROM <table1>
WHERE <column> NOT IN (SELECT <column> FROM <table2> WHERE <column> IS NOT NULL);</column></table2></column></column></table1></columns>
Q: Why is it important to eliminate NULL in subqueries?
A: To ensure accurate results when using operators like NOT IN, which cannot handle NULL values correctly without additional filtering.
Q: When should you use a subquery instead of a join?
A: Use subqueries when you need to filter data without including additional columns from the related table in the result.
Q: Can subqueries return more than one column?
A: Subqueries in the WHERE clause must return only one column, but subqueries in other clauses (e.g., FROM) can return multiple columns.
Q: What is the purpose of the EXISTS keyword in T-SQL?
A: EXISTS checks whether a subquery returns any rows. If rows exist, it evaluates to TRUE for the outer query.
Q: How does the EXISTS clause work in the WHERE statement?
A: It uses a correlated subquery to determine if rows in the subquery match the outer query condition.
SELECT CustomerID, AccountNumber
FROM Sales.Customer AS Cust
WHERE EXISTS (SELECT 1 FROM Sales.SalesOrderHeader AS SOH WHERE SOH.CustomerID = Cust.CustomerID);
Q: What does NOT EXISTS do in T-SQL?
A: It returns TRUE for rows in the outer query if no rows in the subquery meet the condition.
SELECT CustomerID, AccountNumber
FROM Sales.Customer AS Cust
WHERE NOT EXISTS (SELECT 1 FROM Sales.SalesOrderHeader AS SOH WHERE SOH.CustomerID = Cust.CustomerID);
Q: Why can you use SELECT * or SELECT 1 in EXISTS subqueries?
A: The subquery only checks for the presence of rows, not the returned data. Both forms are functionally identical.
Q: What is the purpose of CROSS APPLY?
A: CROSS APPLY joins each row from the left table with the results of a subquery. The subquery runs once for each row in the left table and returns only rows that match.
Q: How does CROSS APPLY differ from a regular join?
A: CROSS APPLY allows you to use a subquery that can reference columns from the outer query, enabling row-by-row computation.
Q: What is OUTER APPLY in T-SQL?
A: OUTER APPLY works like a LEFT OUTER JOIN. It returns all rows from the left table, including rows where the subquery does not return results (those will have NULL values).
Q: Provide an example of using CROSS APPLY.
SELECT CustomerID, AccountNumber, SalesOrderID
FROM Sales.Customer AS Cust
CROSS APPLY (SELECT SalesOrderID FROM Sales.SalesOrderHeader AS SOH WHERE Cust.CustomerID = SOH.CustomerID) AS A;
Q: Provide an example of using OUTER APPLY.
SELECT CustomerID, AccountNumber, SalesOrderID
FROM Sales.Customer AS Cust
OUTER APPLY (SELECT SalesOrderID FROM Sales.SalesOrderHeader AS SOH WHERE Cust.CustomerID = SOH.CustomerID) AS A;
Q: When should you use EXISTS over a join or subquery in the WHERE clause?
A: Use EXISTS when you only need to verify the existence of matching rows in a subquery without returning actual data from it.
Q: What is the main difference between CROSS APPLY and OUTER APPLY?
A: CROSS APPLY behaves like an INNER JOIN, only returning rows where the subquery produces results, whereas OUTER APPLY behaves like a LEFT OUTER JOIN, returning all rows from the left table.
Q: Why might EXISTS perform better than other techniques?
A: EXISTS stops evaluating as soon as it finds a match, making it efficient for large datasets with selective filtering.
Q: What is the purpose of the UNION operator in T-SQL?
A: UNION combines the results of two or more queries into a single result set, removing duplicate rows by default.
Q: What is the difference between UNION and UNION ALL?
A: UNION removes duplicate rows, while UNION ALL includes all rows, even duplicates, which improves performance if deduplication is unnecessary.
Q: What must match between the queries in a UNION statement?
A: Each query must return the same number of columns, and the columns must have compatible data types.
Q: Provide the syntax for a simple UNION query.
SELECT col1, col2 FROM table1
UNION
SELECT col1, col2 FROM table2;
Q: Why should you use UNION ALL when possible?
A: UNION ALL avoids the overhead of removing duplicates, which improves performance, especially with large datasets.
Q: Where must the ORDER BY clause be placed in a UNION query?
A: The ORDER BY clause must be placed at the end of the entire UNION query, not after individual queries.
Example:
SELECT col1 FROM table1
UNION
SELECT col1 FROM table2
ORDER BY col1;
Q: How does SQL Server determine column names in a UNION query?
A: The column names and data types are determined by the first query in the UNION.
Q: What happens if the columns or data types don’t match between queries in a UNION?
A: SQL Server will throw an error if the column counts differ or if incompatible data types cannot be implicitly converted.
Q: Provide an example of a UNION query with duplicate elimination.
SELECT CustomerID FROM Sales.Customers
UNION
SELECT CustomerID FROM Sales.ArchivedCustomers;
Q: Provide an example of a UNION ALL query to combine results without deduplication.
SELECT ProductID, Name FROM Products_Current
UNION ALL
SELECT ProductID, Name FROM Products_Discontinued;
Q: What error will occur if the number of columns doesn’t match in a UNION query?
A: SQL Server will throw an error indicating a mismatch in the number of columns.
Q: Why is it important to use compatible data types in UNION queries?
A: SQL Server follows precedence rules, and incompatible types (e.g., mixing strings and integers) can cause errors or unexpected implicit conversions.
Q: What is a good use case for a UNION query?
A: Combining production and archived data or displaying data from different divisions of a company as a unified result.
Q: Provide an example of a UNION query that uses the same table with different WHERE clauses.
SELECT CustomerID, OrderDate FROM Orders WHERE Year(OrderDate) = 2023
UNION
SELECT CustomerID, OrderDate FROM Orders WHERE Year(OrderDate) = 2022;