Quick learning notes
SQL Interview Questions
Top 20 SQL Interview Questions with Answers
1. Explain order of execution in SQL.
Answer:
SQL follows this execution order:
FROM → WHERE → GROUP BY → HAVING → SELECT → ORDER BY.
2. Difference between WHERE and HAVING?
Answer:
WHERE filters rows before grouping, while HAVING filters grouped data after aggregation.
3. What is the use of GROUP BY?
Answer:
GROUP BY groups similar rows together so aggregate functions like SUM(), COUNT(), AVG() can be applied.
4. Explain all types of joins in SQL.
Answer:
INNER JOIN returns matching rows.
LEFT JOIN returns all left table rows.
RIGHT JOIN returns all right table rows.
FULL JOIN returns all matching and non-matching rows.
5. What are triggers in SQL?
Answer:
Triggers are automatic database actions executed on INSERT, UPDATE, or DELETE events.
6. What is stored procedure in SQL?
Answer:
A stored procedure is a reusable SQL program stored inside the database.
7. Explain all types of window functions.
Answer:
Common window functions include:
ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), and LAG().
8. Difference between DELETE and TRUNCATE?
Answer:
DELETE removes rows one by one and supports WHERE conditions.
TRUNCATE removes all rows quickly and resets identity.
9. Difference between DML, DDL and DCL?
Answer:
DML manipulates data.
DDL defines database structure.
DCL controls permissions.
10. What are aggregate functions?
Answer:
Aggregate functions perform calculations on multiple rows.
Examples:
COUNT(), SUM(), AVG(), MIN(), MAX().
11. Which is faster between CTE and Subquery?
Answer:
Performance depends on the execution plan.
CTEs improve readability while subqueries are useful for nested operations.
12. What are constraints and types of constraints?
Answer:
Constraints enforce data integrity rules.
Examples:
PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK, NOT NULL.
13. Types of Keys?
Answer:
Primary Key, Foreign Key, Composite Key, Candidate Key, Alternate Key.
14. Different types of Operators?
Answer:
Arithmetic, Comparison, Logical, and Special operators.
15. Difference between GROUP BY and WHERE?
Answer:
WHERE filters rows before grouping.
GROUP BY organizes rows into groups.
16. What are Views?
Answer:
Views are virtual tables created using SQL queries.
17. What are different types of constraints?
Answer:
NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT.
18. What is difference between VARCHAR and NVARCHAR?
Answer:
VARCHAR stores regular text.
NVARCHAR supports Unicode characters.
19. Similar for CHAR and NCHAR?
Answer:
CHAR stores fixed-length regular text.
NCHAR stores fixed-length Unicode text.
20. What are indexes and their types?
Answer:
Indexes improve query performance.
Common types:
Clustered Index and Non-Clustered Index.
--------------------------------------------------
Seldom India Technologies
Website: https://www.seldomindia.com
WhatsApp: 9001827800