The Ultimate List of Basic to Advanced SQL Interview Questions (With Answers)
Preparing for an SQL interview? Whether you’re just starting out or have years of experience, this guide covers the most important basic to advanced SQL interview questions to help you crack your next technical round confidently.
SQL is the backbone of data management in IT companies, and mastering it is essential for roles like database developer, analyst, data engineer, or backend developer.
✅ Basic SQL Interview Questions
Let’s begin with the fundamentals that every candidate should know.
1. What is SQL?
Answer:
SQL (Structured Query Language) is used to interact with relational databases. It allows you to retrieve, insert, update, and delete data using queries.
2. What are the different types of SQL commands?
Answer:
- DDL: CREATE, DROP, ALTER
- DML: INSERT, UPDATE, DELETE
- DCL: GRANT, REVOKE
- TCL: COMMIT, ROLLBACK
- DQL: SELECT
3. What is the difference between WHERE and HAVING?
- WHERE filters rows before grouping.
- HAVING filters after the
GROUP BY
clause.
4. How do you retrieve unique values from a table?
SELECT DISTINCT ColumnName FROM TableName;
5. Explain the use of NULL in SQL.
Answer:
NULL means a missing or undefined value. It’s not the same as zero or an empty string.
🧠 Intermediate SQL Interview Questions
Once the basics are strong, interviewers test your ability to solve business scenarios.
6. What is a JOIN? Explain its types.
- INNER JOIN: Returns common records in both tables
- LEFT JOIN: Returns all from left table and matched from right
- RIGHT JOIN: All from right table and matched from left
- FULL JOIN: All records from both sides
7. How to find the second highest salary?
SELECT MAX(Salary)
FROM Employee
WHERE Salary < (SELECT MAX(Salary) FROM Employee);
8. Difference between UNION and UNION ALL?
- UNION removes duplicates.
- UNION ALL includes duplicates.
9. What are subqueries and correlated subqueries?
Subqueries are nested SELECT queries.
Correlated subqueries reference columns from the outer query.
10. Explain GROUP BY with example.
SELECT Department, COUNT(*)
FROM Employee
GROUP BY Department;
It groups records based on a column and performs aggregate functions.
🔍 Advanced SQL Interview Questions
Now, let’s explore advanced SQL interview questions often asked in product-based companies or senior roles.
11. What is a CTE? How is it different from a subquery?
CTE (Common Table Expression) is a temporary result set that can be referenced multiple times.
WITH HighSalaries AS (
SELECT * FROM Employee WHERE Salary > 100000
)
SELECT * FROM HighSalaries WHERE Department = 'IT';
12. What are window functions?
Window functions perform calculations across a set of rows related to the current row.
Example:
SELECT Name, Salary, RANK() OVER (ORDER BY Salary DESC) AS Rank
FROM Employee;
13. How would you handle performance issues in SQL queries?
- Indexing
- Avoiding SELECT *
- Using joins effectively
- Query profiling and execution plans
14. Explain ACID properties.
- Atomicity – All or nothing
- Consistency – Data must be valid before and after
- Isolation – Transactions don’t affect each other
- Durability – Once committed, changes persist
15. Write a query to fetch employees who earn more than the average salary of their department.
SELECT * FROM Employee e
WHERE Salary > (
SELECT AVG(Salary)
FROM Employee
WHERE Department = e.Department
);
❓ FAQs – Basic to Advanced SQL Interview Questions
Q1: Who should prepare basic to advanced SQL interview questions?
Anyone applying for database roles, backend developer, business analyst, or data scientist should prepare these questions.
Q2: Are these suitable for freshers?
Yes, the questions start from basic and gradually progress to advanced. Freshers can skip or review advanced ones based on the job role.
Q3: How do I practice these queries?
You can use platforms like LeetCode, HackerRank, or install MySQL/PostgreSQL locally to practice.
🔚 Final Words
Mastering basic to advanced SQL interview questions is your gateway to acing interviews at top tech companies. Focus on understanding the concepts, practicing queries daily, and explaining your logic clearly.
👉 Save this post, and practice these queries until you can write them without looking. Want more job-specific SQL question sets (e.g., TCS, Infosys, Wipro)? Let me know and I’ll create one for you.