SQL Advanced Flashcards

(41 cards)

1
Q

What is a recursive Common Table Expression (CTE)?

A

A CTE that references itself to perform iterative queries such as hierarchical data traversal

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

How do you write a recursive CTE to find all subordinates of an employee?

A

Use WITH RECURSIVE subordinates AS (SELECT id manager_id FROM employees WHERE id = ? UNION ALL SELECT e.id e.manager_id FROM employees e INNER JOIN subordinates s ON e.manager_id = s.id) SELECT * FROM subordinates

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

What is the difference between ROW_NUMBER() RANK() and DENSE_RANK()?

A

ROW_NUMBER assigns unique sequential numbers RANK leaves gaps on ties DENSE_RANK assigns consecutive ranks without gaps

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

What are window frames in SQL window functions?

A

Specifies the subset of rows used for the calculation relative to the current row

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

How do you calculate a moving average using window functions?

A

Use AVG() OVER (ORDER BY date ROWS BETWEEN n PRECEDING AND CURRENT ROW)

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

What is the EXPLAIN command used for?

A

To analyze and display the query execution plan for performance tuning

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

What are execution plans?

A

Detailed steps the database takes to run a query showing index usage and join methods

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

How do you optimize slow queries?

A

Analyze execution plans add indexes rewrite joins avoid unnecessary subqueries limit result sets

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

What is a deadlock in SQL?

A

A situation where two or more transactions are waiting indefinitely for each other’s locks

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

How do you prevent deadlocks?

A

Keep transactions short access objects in a consistent order use lower isolation levels avoid user interaction in transactions

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

What is isolation level in databases?

A

Controls how transaction integrity is visible to other transactions (eg READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE)

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

What isolation level prevents dirty reads but allows non-repeatable reads?

A

READ COMMITTED

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

What is a dirty read?

A

Reading uncommitted data from another transaction

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

What is a phantom read?

A

New rows added by another transaction appear in subsequent reads within the same transaction

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

How do you implement pagination efficiently in SQL?

A

Use OFFSET and LIMIT with indexed columns or keyset pagination for large datasets

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

What is keyset pagination?

A

Pagination using WHERE clauses with indexed columns instead of OFFSET to improve performance

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

What are materialized views and when would you use them?

A

Physical storage of query results for faster reads used when query performance is critical but data freshness can be delayed

18
Q

How do you refresh a materialized view?

A

Using REFRESH MATERIALIZED VIEW command syntax varies by DBMS

19
Q

What are stored procedures?

A

Precompiled SQL code stored in the database that can accept parameters and perform operations

20
Q

What are the benefits of stored procedures?

A

Improved performance security and encapsulation of business logic

21
Q

What is a trigger in SQL?

A

A procedure that automatically executes in response to certain events on a table (INSERT UPDATE DELETE)

22
Q

What is the difference between AFTER and BEFORE triggers?

A

BEFORE triggers run before the operation AFTER triggers run after the operation completes

23
Q

What is normalization up to Boyce-Codd Normal Form (BCNF)?

A

An advanced normal form ensuring every determinant is a candidate key to eliminate redundancy

24
Q

What are foreign key constraints with ON DELETE CASCADE?

A

Deletes rows in child tables automatically when a referenced row in the parent table is deleted

25
How do you handle JSON data in SQL?
Use JSON functions like JSON_EXTRACT JSONB in PostgreSQL or JSON_VALUE in SQL Server to query and manipulate JSON
26
What is a lateral join?
A join that allows a subquery to reference columns from preceding tables in the FROM clause
27
How does a lateral join differ from a regular join?
It executes the subquery for each row of the preceding table useful for correlated subqueries
28
What is partitioning in databases?
Splitting a large table into smaller manageable pieces for performance and maintenance
29
What types of partitioning exist?
Range list hash and composite partitioning
30
What is sharding?
Horizontal partitioning across multiple servers to scale database systems
31
What is a bitmap index and when is it useful?
An index type using bitmaps efficient for low cardinality columns like gender or boolean flags
32
How do you detect and resolve index fragmentation?
Use DBMS specific commands to analyze fragmentation and rebuild or reorganize indexes
33
What is a hash join?
A join algorithm using a hash table to find matching rows efficient for large unsorted datasets
34
What is the difference between a hash join and a nested loop join?
Hash join uses hashing efficient for large joins nested loop is simpler but less efficient for large data
35
What is a CTE's performance impact?
Generally improves readability but can affect performance if not optimized some DBMS materialize CTEs
36
How do you optimize queries using EXISTS instead of IN?
EXISTS can be more efficient with correlated subqueries as it stops searching once a match is found
37
What is a pivot query?
Transforms rows into columns for easier reporting
38
How do you write a pivot query in SQL Server?
Using PIVOT operator with aggregation functions
39
What is the use of the WITH TIES option?
Includes additional rows that tie with the last row in the result set when using ORDER BY and TOP or FETCH
40
What is the difference between DELETE and TRUNCATE regarding transactions?
DELETE logs individual row deletions and can be rolled back TRUNCATE is faster but often can’t be rolled back
41
How do you implement temporal tables?
Tables that track historical data automatically using system-versioned temporal table features