Subqueries & Nested Queries Interview Questions
Comprehensive subqueries & nested queries interview questions and answers for SQL. Prepare for your next job interview with expert guidance.
Questions Overview
1. What is a subquery in SQL and what is its basic purpose?
Basic2. What is the difference between a correlated and non-correlated subquery?
Basic3. What is a derived table and how is it used?
Basic4. What are scalar subqueries and when should they be used?
Basic5. How do you use EXISTS operator with subqueries?
Moderate6. What is the difference between IN and EXISTS in subqueries?
Moderate7. How can you use subqueries in the SELECT clause?
Moderate8. What are the performance implications of correlated subqueries?
Advanced9. How do you handle NULL values in subqueries?
Advanced10. What is a common table expression (CTE) and how does it differ from a subquery?
Advanced11. How can you update a table using a subquery?
Moderate12. What is a lateral join and how does it relate to subqueries?
Advanced13. How do you use subqueries with aggregate functions?
Moderate14. What are the limitations of subqueries in SQL?
Advanced15. How can you use subqueries for data insertion?
Moderate16. What is the ANY/SOME operator and how is it used with subqueries?
Advanced17. How does the ALL operator work with subqueries?
Advanced18. What is a nested subquery and what are its potential impacts?
Advanced19. How can you optimize subquery performance?
Advanced20. What is a materialized subquery and when is it useful?
Advanced21. How do you delete records using subqueries?
Moderate22. What is the difference between a subquery and a join?
Basic23. How do you use subqueries with window functions?
Advanced24. What is a recursive subquery and when would you use it?
Advanced25. How do you handle errors in subqueries?
Moderate26. What is the impact of subqueries on transaction isolation?
Advanced27. How do you use subqueries in dynamic SQL?
Advanced28. What are inline views and how are they used?
Moderate29. How do you use subqueries in CASE expressions?
Advanced1. What is a subquery in SQL and what is its basic purpose?
BasicA subquery is a query nested inside another query. Its basic purpose is to return data that will be used in the main query as a condition or as a derived table. Subqueries can be used in SELECT, FROM, WHERE, and HAVING clauses.
2. What is the difference between a correlated and non-correlated subquery?
BasicA correlated subquery references columns from the outer query and is executed once for each row processed by the outer query. A non-correlated subquery is independent of the outer query and executes once for the entire query.
3. What is a derived table and how is it used?
BasicA derived table is a subquery in the FROM clause that acts as a temporary table for the duration of the query. It must have an alias and can be used like a regular table in the main query.
4. What are scalar subqueries and when should they be used?
BasicScalar subqueries are subqueries that return exactly one row and one column. They're used when a single value is needed, such as in comparisons or calculations, and can appear in SELECT, WHERE, or HAVING clauses.
5. How do you use EXISTS operator with subqueries?
ModerateEXISTS checks whether a subquery returns any rows. It's often used with correlated subqueries to test for the existence of related records, returning TRUE if the subquery returns any rows and FALSE if it doesn't.
6. What is the difference between IN and EXISTS in subqueries?
ModerateIN compares a value against a list of values returned by the subquery, while EXISTS checks for the presence of any rows. EXISTS often performs better with large datasets as it stops processing once a match is found.
7. How can you use subqueries in the SELECT clause?
ModerateSubqueries in the SELECT clause must return a single value per row of the outer query. They're often used to calculate values or retrieve related data from other tables for each row in the result set.
8. What are the performance implications of correlated subqueries?
AdvancedCorrelated subqueries can impact performance as they execute once for each row in the outer query. This can be inefficient for large datasets and might be better replaced with JOINs or other query constructs.
9. How do you handle NULL values in subqueries?
AdvancedNULL values require special handling in subqueries, especially with NOT IN operations. It's important to either filter out NULLs or use NOT EXISTS instead of NOT IN to avoid unexpected results due to NULL comparison behavior.
10. What is a common table expression (CTE) and how does it differ from a subquery?
AdvancedA CTE is a named temporary result set that exists within the scope of a single statement. Unlike subqueries, CTEs can be referenced multiple times within a query and can be recursive. They often improve readability and maintenance.
11. How can you update a table using a subquery?
ModerateTables can be updated using subqueries in the SET clause or WHERE clause. The subquery can provide values for the update or identify which rows to update. Care must be taken with correlated subqueries to avoid updating the same table being referenced.
12. What is a lateral join and how does it relate to subqueries?
AdvancedA lateral join allows subqueries in the FROM clause to reference columns from preceding items in the FROM clause. This enables row-by-row processing with access to outer query columns, similar to correlated subqueries.
13. How do you use subqueries with aggregate functions?
ModerateSubqueries can be used with aggregate functions to compare individual values against group results, such as finding rows where a value exceeds the average. They can appear in HAVING clauses or as scalar subqueries in the SELECT list.
14. What are the limitations of subqueries in SQL?
AdvancedSubqueries have limitations including: cannot contain ORDER BY (except in TOP/LIMIT clauses), cannot be used with UNION/INTERSECT/EXCEPT in certain contexts, and must return single values in scalar contexts. Performance can also be a limitation with complex nested queries.
15. How can you use subqueries for data insertion?
ModerateSubqueries can be used in INSERT statements to populate new records based on existing data. They can provide values for specific columns or complete rows, and can be combined with SELECT statements to insert multiple rows.
16. What is the ANY/SOME operator and how is it used with subqueries?
AdvancedANY/SOME compares a value with each value returned by a subquery, returning TRUE if any comparison is true. It's often used with comparison operators like '>', '<', or '=' to find matches against multiple values.
17. How does the ALL operator work with subqueries?
AdvancedThe ALL operator compares a value with every value returned by a subquery, returning TRUE only if all comparisons are true. It's useful for finding values that satisfy conditions against an entire set of results.
18. What is a nested subquery and what are its potential impacts?
AdvancedA nested subquery is a subquery within another subquery. While they can solve complex problems, each level of nesting can impact performance and readability. They should be used judiciously and potentially refactored using JOINs or CTEs.
19. How can you optimize subquery performance?
AdvancedSubquery performance can be optimized by: using EXISTS instead of IN for large datasets, avoiding correlated subqueries when possible, using JOINs instead of subqueries where appropriate, and ensuring proper indexing on referenced columns.
20. What is a materialized subquery and when is it useful?
AdvancedA materialized subquery is one where the results are computed once and stored temporarily, rather than being recomputed for each row. This can improve performance for complex subqueries referenced multiple times in the main query.
21. How do you delete records using subqueries?
ModerateSubqueries in DELETE statements can identify which records to remove based on complex conditions or relationships with other tables. Care must be taken with correlated subqueries to avoid affecting the subquery's results during deletion.
22. What is the difference between a subquery and a join?
BasicWhile both can relate data from multiple tables, subqueries create a nested query structure while joins combine tables horizontally. Joins often perform better but subqueries can be more readable for certain operations like existence checks.
23. How do you use subqueries with window functions?
AdvancedSubqueries can contain window functions to perform calculations before the results are used in the main query. This is often done in derived tables or CTEs where the window function results need further processing.
24. What is a recursive subquery and when would you use it?
AdvancedA recursive subquery is used in a CTE to query hierarchical or graph-like data structures. It combines a base case with a recursive part to traverse relationships like organizational charts or bill of materials.
25. How do you handle errors in subqueries?
ModerateError handling in subqueries involves checking for NULL results, handling no-data scenarios, ensuring single-row returns for scalar subqueries, and using CASE expressions or COALESCE to handle exceptional cases.
26. What is the impact of subqueries on transaction isolation?
AdvancedSubqueries operate within the same transaction as the main query, but complex subqueries can affect lock duration and concurrency. Correlated subqueries may hold locks longer due to row-by-row processing.
27. How do you use subqueries in dynamic SQL?
AdvancedSubqueries in dynamic SQL must be properly formatted and escaped. They can be used to create flexible queries based on runtime conditions, but care must be taken to prevent SQL injection and ensure proper parameter handling.
28. What are inline views and how are they used?
ModerateInline views are subqueries in the FROM clause that create temporary result sets. They're useful for breaking down complex queries, pre-aggregating data, or applying transformations before joining with other tables.
29. How do you use subqueries in CASE expressions?
AdvancedSubqueries in CASE expressions must return scalar values and can be used to create conditional logic based on queries against other tables or aggregated data. They're useful for complex categorical assignments or calculations.