SQL Advanced Flashcards
(41 cards)
What is a recursive Common Table Expression (CTE)?
A CTE that references itself to perform iterative queries such as hierarchical data traversal
How do you write a recursive CTE to find all subordinates of an employee?
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
What is the difference between ROW_NUMBER() RANK() and DENSE_RANK()?
ROW_NUMBER assigns unique sequential numbers RANK leaves gaps on ties DENSE_RANK assigns consecutive ranks without gaps
What are window frames in SQL window functions?
Specifies the subset of rows used for the calculation relative to the current row
How do you calculate a moving average using window functions?
Use AVG() OVER (ORDER BY date ROWS BETWEEN n PRECEDING AND CURRENT ROW)
What is the EXPLAIN command used for?
To analyze and display the query execution plan for performance tuning
What are execution plans?
Detailed steps the database takes to run a query showing index usage and join methods
How do you optimize slow queries?
Analyze execution plans add indexes rewrite joins avoid unnecessary subqueries limit result sets
What is a deadlock in SQL?
A situation where two or more transactions are waiting indefinitely for each other’s locks
How do you prevent deadlocks?
Keep transactions short access objects in a consistent order use lower isolation levels avoid user interaction in transactions
What is isolation level in databases?
Controls how transaction integrity is visible to other transactions (eg READ UNCOMMITTED READ COMMITTED REPEATABLE READ SERIALIZABLE)
What isolation level prevents dirty reads but allows non-repeatable reads?
READ COMMITTED
What is a dirty read?
Reading uncommitted data from another transaction
What is a phantom read?
New rows added by another transaction appear in subsequent reads within the same transaction
How do you implement pagination efficiently in SQL?
Use OFFSET and LIMIT with indexed columns or keyset pagination for large datasets
What is keyset pagination?
Pagination using WHERE clauses with indexed columns instead of OFFSET to improve performance
What are materialized views and when would you use them?
Physical storage of query results for faster reads used when query performance is critical but data freshness can be delayed
How do you refresh a materialized view?
Using REFRESH MATERIALIZED VIEW command syntax varies by DBMS
What are stored procedures?
Precompiled SQL code stored in the database that can accept parameters and perform operations
What are the benefits of stored procedures?
Improved performance security and encapsulation of business logic
What is a trigger in SQL?
A procedure that automatically executes in response to certain events on a table (INSERT UPDATE DELETE)
What is the difference between AFTER and BEFORE triggers?
BEFORE triggers run before the operation AFTER triggers run after the operation completes
What is normalization up to Boyce-Codd Normal Form (BCNF)?
An advanced normal form ensuring every determinant is a candidate key to eliminate redundancy
What are foreign key constraints with ON DELETE CASCADE?
Deletes rows in child tables automatically when a referenced row in the parent table is deleted