CHAPTER 6: Building on Subqueries, Common Table Expressions, and Unions Flashcards
(91 cards)
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.