The document provides a comprehensive list of the top 25 SQL interview questions and answers, covering key concepts such as SQL types, clauses, views, indexing, and performance optimization. It includes explanations of various SQL functions and features, such as window functions, CTEs, and SQL injection prevention. Each question is accompanied by concise definitions or examples to aid understanding for interview preparation.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
0 views
SQL
The document provides a comprehensive list of the top 25 SQL interview questions and answers, covering key concepts such as SQL types, clauses, views, indexing, and performance optimization. It includes explanations of various SQL functions and features, such as window functions, CTEs, and SQL injection prevention. Each question is accompanied by concise definitions or examples to aid understanding for interview preparation.
HAVING clauses? WHERE filters rows before grouping; HAVING filters groups after aggregation. Example: SELECT department, COUNT(*) FROM employees WHERE status='active' GROUP BY department HAVING COUNT(*) > 5;
4. What is a view in SQL?
A view is a virtual table based on the result of a SQL query. It doesn't store data itself. Example: CREATE VIEW active_employees AS SELECT * FROM employees WHERE status='active';
5. What is a Common Table Expression (CTE)?
A temporary result set defined using the WITH clause. Example: WITH dept_count AS ( SELECT department, COUNT(*) AS total FROM employees GROUP BY department ) SELECT * FROM dept_count;
6. Explain the difference between UNION and
UNION ALL. UNION removes duplicates. UNION ALL includes duplicates and is faster.
7. What is the difference between RANK(),
DENSE_RANK(), and ROW_NUMBER()? ROW_NUMBER() gives unique numbers. RANK() skips ranks after ties. DENSE_RANK() doesn’t skip ranks. 8. What are window functions? Use OVER() to perform calculations across a set of related rows. Example: SELECT name, department, RANK() OVER(PARTITION BY department ORDER BY salary DESC) FROM employees;
9. What is indexing in SQL and why is it used?
Indexes speed up data retrieval but slow down inserts and updates.
10. What is a covering index?
An index that includes all columns needed by a query—making it faster by avoiding table lookups.
11. What is a correlated subquery?
A subquery that references a column from the outer query. Example: SELECT name FROM employees e WHERE salary > ( SELECT AVG(salary) FROM employees WHERE department = e.department ); 12. What is a materialized view? Unlike regular views, it stores data physically and needs refreshing.
13. What is the purpose of the MERGE
statement? It combines INSERT, UPDATE, DELETE operations into a single statement.
14. What is the difference between IN and
EXISTS? IN: works well with small lists. EXISTS: faster for larger or correlated queries.
15. What is COALESCE()?
Returns the first non-null value. Example: SELECT name, COALESCE(phone, 'N/A') FROM users;
16. What is a PIVOT in SQL?
Transforms rows into columns. Example (SQL Server): SELECT * FROM ( SELECT dept, gender FROM employees ) src PIVOT (COUNT(gender) FOR gender IN ([M], [F])) AS pvt;
17. What is a recursive CTE and when would you
use it? A CTE that references itself. Used for hierarchical data. Example:
WITH RECURSIVE numbers(n) AS (
SELECT 1 UNION ALL SELECT n + 1 FROM numbers WHERE n < 5 ) SELECT * FROM numbers;
18. What is the difference between temp tables
and table variables? Temp tables: stored in tempdb, support indexing and transactions. Table variables: stored in memory, better for small datasets.
19. What are lateral joins (or CROSS APPLY)?
Allow a subquery in the FROM clause to reference columns from previous tables.
20. What is the GROUPING SETS clause?
Allows multiple GROUP BY combinations in a single query. Example: SELECT dept, job, SUM(salary) FROM employees GROUP BY GROUPING SETS ((dept), (job));
21. What are scalar vs table-valued functions?
Scalar: returns a single value. Table-valued: returns a table.
22. What is SQL injection and how do you prevent
it? A security vulnerability. Prevent it using parameterized queries or stored procedures.
23. What is a computed column?
A column based on expressions. Example: ALTER TABLE orders ADD total AS (price * quantity);
24. What is sharding in SQL databases?
Dividing data across multiple databases for better performance and scalability.
25. How do you optimize a slow SQL query?
Use indexing, avoid SELECT *, analyze execution plans, reduce row scans and join cost.