35 Important SQL Interview Questions with Detailed Answers:
1. Explain order of execution of SQL.
Order: FROM → JOIN → ON → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. SQL queries are processed in this logical sequence, not the way they are written.
2. What is difference between WHERE and HAVING?
WHERE filters rows before aggregation, while HAVING filters groups after aggregation.
3. What is the use of GROUP BY?
GROUP BY aggregates data across rows with the same values in specified columns, commonly used with aggregate functions.
4. Explain all types of joins in SQL?
INNER JOIN: Returns matching rows from both tables.
LEFT JOIN: All rows from the left, matched rows from right.
RIGHT JOIN: All rows from the right, matched rows from left.
FULL JOIN: All rows from both, with NULLs where no match.
SELF JOIN: Joins table to itself.
CROSS JOIN: Cartesian product of both tables.
5. What are triggers in SQL?
Triggers are procedural code executed automatically in response to certain events on a table or view (INSERT, UPDATE, DELETE).
6. What is stored procedure in SQL?
A stored procedure is a set of SQL statements saved and executed on demand, useful for modularizing code.
7. Explain all types of window functions?
RANK(): Gives rank with gaps.
DENSE_RANK(): Ranks without gaps.
ROW_NUMBER(): Unique row index.
LEAD(): Access next row.
LAG(): Access previous row.
8. What is difference between DELETE and TRUNCATE?
DELETE: Row-wise deletion, can have WHERE clause, logs each row.
TRUNCATE: Deletes all rows, faster, minimal logging, cannot rollback easily.
9. What is difference between DML, DDL and DCL?
DML: Data Manipulation Language (SELECT, INSERT, UPDATE, DELETE).
DDL: Data Definition Language (CREATE, ALTER, DROP).
DCL: Data Control Language (GRANT, REVOKE).
10. What are aggregate functions?
Functions that return a single value: SUM(), AVG(), COUNT(), MIN(), MAX().
11. Which is faster: CTE or Subquery?
Performance depends on context, but subqueries are sometimes faster as CTEs may be materialized.
12. What are constraints and types?
Rules to maintain data integrity. Types: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT.
13. Types of Keys?
Primary Key
Foreign Key
Unique Key
Composite Key
Candidate Key
14. Different types of Operators?
Arithmetic: +, -, *, /
Comparison: =, <>, >, <, >=, <=
Logical: AND, OR, NOT
Bitwise, LIKE, IN, BETWEEN
15. Difference between GROUP BY and WHERE?
WHERE filters before aggregation. GROUP BY groups after filtering.
16. What are Views?
Virtual tables based on SQL queries. They store only query definition.
17. What are different types of constraints?
Same as Q12: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT.
18. What is difference between VARCHAR and NVARCHAR?
VARCHAR: ASCII, 1 byte per char.
NVARCHAR: Unicode, 2 bytes per char, supports multiple languages.
19. Similarity for CHAR and NCHAR?
CHAR: Fixed-length ASCII.
NCHAR: Fixed-length Unicode.
20. What are indexes and their types?
Used for faster retrieval.
Types:
- Clustered
- Non-clustered
- Unique
- Composite
- Full-text
21. What is an index? Explain its types.
Same as above. Indexes speed up queries by creating pointers to data.
22. List different types of relationships in SQL.
One-to-One
One-to-Many
Many-to-Many
23. Differentiate between UNION and UNION ALL.
UNION: Removes duplicates.
UNION ALL: Includes duplicates.
24. How many types of clauses in SQL?
Common clauses: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT, OFFSET, JOIN, ON.
25. What is the difference between UNION and UNION ALL in SQL?
Same as Q23.
26. What are various types of relationships in SQL?
Same as Q22.
27. Difference between Primary Key and Secondary Key?
Primary Key: Uniquely identifies rows.
Secondary Key: May not be unique, used for lookup.
28. What is the difference between WHERE and HAVING?
Already answered in Q2.
29. Find second highest salary of an employee?
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);
Post #3754
1.14K
- ❤ 1
- 👍 1