Advanced SQL Queries Flashcards

Subqueries and nested SELECTs Correlated subqueries Common Table Expressions (CTEs) with WITH Recursive CTEs CASE statements Window functions: RANK, DENSE_RANK, ROW_NUMBER, LAG/LEAD Advanced filtering and pagination (34 cards)

1
Q

What is a subquery in SQL?

A

A subquery is a SELECT query nested inside another SQL query, often used in WHERE, FROM, or SELECT clauses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a correlated subquery?

A

A correlated subquery references columns from the outer query and is executed once per row of the outer query.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a Common Table Expression (CTE)?

A

A CTE is a temporary named result set defined using the WITH keyword, used for better query readability and reusability.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a recursive CTE?

A

A CTE that calls itself to perform hierarchical or recursive operations like traversing trees or graphs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What does the CASE statement do in SQL?

A

CASE adds conditional logic to queries, returning specific values based on when-then clauses.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are SQL window functions?

A

Functions like RANK(), ROW_NUMBER(), LAG(), and LEAD() that perform calculations across a result set partition without collapsing rows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does the RANK() function do?

A

Assigns a unique rank to rows within a partition, allowing for gaps in ranking if there are ties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does the DENSE_RANK() function do?

A

Similar to RANK(), but assigns the next consecutive rank without gaps for tied values.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does ROW_NUMBER() do in SQL?

A

Assigns a unique sequential number to each row in a result set partition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of LAG() and LEAD()?

A

They access preceding (LAG) or following (LEAD) row values without using self-joins.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is advanced pagination in SQL?

A

Using OFFSET and FETCH/LIMIT clauses to return subsets of rows for paginated results.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a benefit of using CTEs?

A

Improves readability and helps break complex queries into modular parts.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a downside of recursive CTEs?

A

They can be inefficient or lead to infinite loops if not properly limited with termination conditions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a best practice when using subqueries?

A

Use them for filtering and aggregation only when necessary, and prefer joins or CTEs for readability and performance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a common use case for window functions?

A

Calculating running totals, rankings, time-based comparisons, or pagination without grouping.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do advanced queries impact system design?

A

Enable powerful analytics directly in SQL, reducing the need for application-side processing.

17
Q

What’s the architectural implication of using CTEs heavily?

A

Overuse can lead to performance hits if the database engine doesn’t optimize them as reusable expressions.

18
Q

What are performance concerns with nested SELECTs?

A

They may result in inefficient execution plans if not optimized or indexed correctly.

19
Q

What’s a good monitoring practice for advanced SQL?

A

Use EXPLAIN or query plans to understand and optimize execution paths.

20
Q

What is a tradeoff of using CASE statements?

A

Improves logic control within SQL but can make queries harder to maintain and debug.

21
Q

What is a real-world use case for recursive CTEs?

A

Finding hierarchical paths like employee-manager chains or category-subcategory trees.

22
Q

What’s a gotcha with OFFSET in pagination?

A

Large offsets can degrade performance because the database still processes skipped rows.

23
Q

How can LAG/LEAD help in time series data?

A

By comparing current rows to previous/following rows without joins, simplifying trends or deltas.

24
Q

What’s a potential gotcha with correlated subqueries?

A

They can lead to poor performance because they execute once per outer query row.

25
What’s a common interview question on CTEs?
How would you use a CTE to get the second highest salary in a department?
26
What is the benefit of window functions over GROUP BY?
Window functions retain all original rows and allow row-wise calculations without collapsing data.
27
How do recursive CTEs affect performance?
They can be CPU intensive and may require careful use of MAXRECURSION limits.
28
What is the difference between subquery and CTE?
Subqueries are inline and harder to reuse, while CTEs allow modular, named query reuse.
29
What is a safe way to debug complex queries?
Break them into smaller CTEs or subqueries and validate intermediate results.
30
What is a best practice for CASE logic?
Use it sparingly and keep it readable; move complex logic to application code when possible.
31
What is a use case for DENSE_RANK over RANK?
When you want consistent, gap-free ranking for tied results.
32
How can window functions be misused?
Improper partitioning or ordering can lead to incorrect results or performance issues.
33
Can you combine CTEs with window functions?
Yes, CTEs can simplify window logic and make advanced queries more modular and readable.
34
What is an advanced filtering technique using subqueries?
Filter on aggregates or use EXISTS/NOT EXISTS to control row inclusion based on another query.