SQL / Interview Preparation

Top 50 SQL Interview Questions & Answers (2026)

Prepare for SQL interviews with 50 questions and answers for freshers and experienced candidates. Learn SQL joins, queries, keys and more.

Top 50 SQL Interview Questions 2026
Top 50 SQL Interview Questions 2026
Top 50 SQL Interview Questions and Answers for Freshers and Experienced (2026)

Updated: October 2026 | By LogicByte Team | SQL Interview Preparation

Preparing for an SQL interview in 2026? Whether you're a fresher applying for your first IT job or an experienced professional preparing for a data analyst, software developer, or database-related role, SQL is an important technical skill.

Recruiters may evaluate your understanding of SQL commands, joins, subqueries, aggregate functions, window functions, and practical database problems.

In this guide, you'll discover 50 SQL interview questions and answers, ranging from beginner to advanced levels, with practical SQL query examples.

You'll learn:


  • Basic SQL interview questions for freshers
  • Important SQL commands and concepts
  • SQL JOIN interview questions
  • Frequently practiced SQL coding problems
  • Advanced SQL interview questions
  • Tips for preparing for an SQL technical assessment

Let's begin.

Part 1: Basic SQL Interview Questions for Freshers

1. What is SQL?

SQL stands for Structured Query Language. It is used to manage and query data in relational database management systems such as MySQL, PostgreSQL, SQL Server, and Oracle Database.

Common SQL operations include retrieving, inserting, updating, and deleting records.


2. What are the different types of SQL commands?

SQL commands are commonly grouped into:

TypeCommandsPurposeDDLCREATE, ALTER, DROPDefine database structuresDMLINSERT, UPDATE, DELETEModify recordsDQLSELECTRetrieve recordsDCLGRANT, REVOKEManage permissionsTCLCOMMIT, ROLLBACKManage transactions


3. What is a primary key?

A primary key uniquely identifies each record in a database table. Its values must be unique and cannot be NULL.


4. What is a foreign key?

A foreign key is a column or group of columns that references a key in another table, helping enforce relationships and referential integrity.


5. What is the difference between SQL and MySQL?

SQL is a language used to interact with relational databases, whereas MySQL is a database management system that supports SQL.


6. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping, while HAVING filters groups after aggregation.

Example:


SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

This query identifies departments with more than five employees.


7. What is NULL in SQL?

NULL represents a missing or unknown value. It is different from zero and an empty string.

Use IS NULL or IS NOT NULL to test for NULL values.


8. What is the DISTINCT keyword?

DISTINCT removes duplicate rows from the selected result.


SELECT DISTINCT department
FROM employees;

9. What is the ORDER BY clause?

ORDER BY sorts query results in ascending or descending order.


SELECT *
FROM employees
ORDER BY salary DESC;

10. What is the difference between DELETE, TRUNCATE, and DROP?

  • DELETE removes selected or all rows.
  • TRUNCATE removes all rows while retaining the table structure.
  • DROP removes the table itself.

Transaction behavior and rollback support vary by database system.

Part 2: Intermediate SQL Interview Questions

11. What is a JOIN in SQL?

A JOIN combines records from two or more tables using related columns.


12. What is an INNER JOIN?

An INNER JOIN returns records with matching join conditions in both tables.


SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.id;

13. What is a LEFT JOIN?

A LEFT JOIN returns all rows from the left table and matching rows from the right table. Unmatched right-side columns contain NULL.


14. What is a RIGHT JOIN?

A RIGHT JOIN returns all rows from the right table and matching rows from the left table.


15. What is a FULL OUTER JOIN?

A FULL OUTER JOIN returns matched and unmatched records from both tables, with NULL values where a matching row is absent.

Support and syntax depend on the database.


16. What is a CROSS JOIN?

A CROSS JOIN returns the Cartesian product of two tables, combining each row of the first table with every row of the second.


17. What is GROUP BY?

GROUP BY combines rows sharing the same grouping values so aggregate functions can calculate summaries.


SELECT department, AVG(salary)
FROM employees
GROUP BY department;

18. What are aggregate functions?

Common aggregate functions include:


  • COUNT()
  • SUM()
  • AVG()
  • MIN()
  • MAX()

They calculate values across sets of rows.


19. What is a subquery?

A subquery is a query nested inside another SQL statement.


SELECT name
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
);

This returns employees earning more than the overall average salary.


20. What is a SQL view?

A view is a named query that presents data as a virtual table. Standard views generally do not store their result rows independently.

Part 3: SQL Coding Interview Questions

For the following examples, assume an employees table with columns id, name, department, and salary, unless stated otherwise.


21. How do you find the second-highest salary?

SELECT MAX(salary)
FROM employees
WHERE salary < (
    SELECT MAX(salary)
    FROM employees
);

This returns the second-highest distinct salary.


22. How do you find duplicate values?

SELECT name, COUNT(*)
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;

This identifies repeated employee names, not necessarily duplicate employee records.


23. How do you find employees earning above-average salaries?

SELECT *
FROM employees
WHERE salary > (
    SELECT AVG(salary)
    FROM employees
);

24. How do you find the highest salary in each department?

SELECT department, MAX(salary)
FROM employees
GROUP BY department;

25. How do you count employees in each department?

SELECT department, COUNT(*)
FROM employees
GROUP BY department;

26. How do you find the top three distinct salaries?

SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

This example uses MySQL/PostgreSQL-compatible LIMIT syntax.


27. How do you find employees whose names start with A?

SELECT *
FROM employees
WHERE name LIKE 'A%';

Case sensitivity depends on the database and collation.


28. How do you find employees earning between 30,000 and 60,000?

SELECT *
FROM employees
WHERE salary BETWEEN 30000 AND 60000;

BETWEEN includes both boundaries.


29. How do you update an employee's salary?

UPDATE employees
SET salary = 55000
WHERE id = 101;

30. How do you delete employees with salaries below 20,000?

DELETE FROM employees
WHERE salary < 20000;

Always verify the WHERE condition before executing destructive queries.

Part 4: Advanced SQL Interview Questions

31. What is a window function?

A window function performs calculations across related rows without collapsing those rows into one grouped result.


32. What is ROW_NUMBER()?

ROW_NUMBER() assigns a sequential number to rows within a specified ordering or partition.


33. What is RANK()?

RANK() assigns rankings while allowing tied values to share a rank. Ties can cause gaps in subsequent rankings.


34. What is DENSE_RANK()?

DENSE_RANK() assigns rankings without gaps after tied values.


35. What is the difference between RANK() and DENSE_RANK()?

For values 100, 100, and 90:


  • RANK(): 1, 1, 3
  • DENSE_RANK(): 1, 1, 2

36. What is a CTE?

A Common Table Expression is a named temporary result defined using WITH and referenced within a SQL statement.


WITH high_salary AS (
    SELECT *
    FROM employees
    WHERE salary > 50000
)
SELECT *
FROM high_salary;

37. What is database normalization?

Normalization organizes relational data to reduce unnecessary duplication and improve data integrity.


38. What is an index?

An index is a database structure that can accelerate data retrieval, although it introduces storage and maintenance overhead.


39. What is a stored procedure?

A stored procedure is a database-side routine containing SQL statements and, depending on the database, procedural logic.


40. What is a database transaction?

A transaction is a unit of database work that should preserve consistency and follow ACID principles: Atomicity, Consistency, Isolation, and Durability.

Part 5: Scenario-Based SQL Interview Questions

41. How do you find the third-highest distinct salary?

SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;

This uses MySQL/PostgreSQL syntax and returns no row if fewer than three distinct non-NULL salaries exist.


42. How do you find employees without an assigned department?

SELECT *
FROM employees
WHERE department IS NULL;

43. How do you calculate total salary by department?

SELECT department, SUM(salary)
FROM employees
GROUP BY department;

44. How do you find employees with the same salary?

SELECT *
FROM employees
WHERE salary IN (
    SELECT salary
    FROM employees
    WHERE salary IS NOT NULL
    GROUP BY salary
    HAVING COUNT(*) > 1
);

45. How do you find the number of unique departments?

SELECT COUNT(DISTINCT department)
FROM employees;

NULL department values are excluded.


46. How do you retrieve the five highest-paid employees?

SELECT *
FROM employees
ORDER BY salary DESC, id
LIMIT 5;

47. How do you calculate a running total?

SELECT id, salary,
SUM(salary) OVER (
    ORDER BY id
) AS running_total
FROM employees;

48. How do you identify employees earning more than their department's average salary?

SELECT e.*
FROM employees e
WHERE e.salary > (
    SELECT AVG(e2.salary)
    FROM employees e2
    WHERE e2.department = e.department
);

49. How do you find the highest-paid employee in each department?

SELECT id, name, department, salary
FROM (
    SELECT *,
        DENSE_RANK() OVER (
            PARTITION BY department
            ORDER BY salary DESC
        ) AS salary_rank
    FROM employees
) ranked
WHERE salary_rank = 1;

This returns all employees tied for the highest salary in their department.


50. How do you find departments with more than ten employees?

SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department
HAVING COUNT(*) > 10;

How to Prepare for an SQL Interview in 2026

Learning definitions alone is not enough. Interview preparation should include practical query-writing exercises and timed assessments.

Step 1: Master SQL fundamentals

Study SELECT, WHERE, ORDER BY, GROUP BY, HAVING, and aggregate functions.

Step 2: Practice SQL JOINs

Understand INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN using sample datasets.

Step 3: Solve SQL coding questions

Practice second-highest salary, duplicate detection, departmental ranking, and subqueries.

Step 4: Learn advanced SQL

Explore CTEs, window functions, indexes, and query optimization.

Step 5: Attempt an SQL mock test

Test your knowledge using timed SQL assessments and review incorrect answers.

Practice SQL Online with LogicByte

LogicByte is a learning and skill assessment platform designed to help learners improve technical skills through practice questions, coding challenges, mock tests, and skill assessments.

Instead of only reading SQL interview answers, strengthen your preparation by solving SQL problems independently.

Explore LogicByte's SQL practice questions and mock tests to evaluate your preparation.

Frequently Asked Questions About SQL Interviews

Is SQL difficult for freshers?

SQL fundamentals are approachable for beginners. More advanced concepts, such as window functions and query optimization, require additional practice.


Which SQL topics are most important for interviews?

Focus on SELECT queries, JOINs, GROUP BY, HAVING, subqueries, aggregate functions, NULL handling, and window functions.


Can I prepare for an SQL interview in seven days?

Seven days can be enough to review SQL fundamentals if you already have some experience. Beginners may need more time to build practical query-writing skills.


Is SQL important for data analyst jobs?

Yes. SQL is widely used to retrieve, combine, filter, and analyze data stored in relational databases.


How can I practice SQL interview questions online?

Use interactive SQL practice exercises, coding challenges, and mock assessments. Focus on understanding why each query works instead of memorizing answers.

Final Thoughts

SQL interview preparation becomes more effective when you combine conceptual knowledge with hands-on query practice.

Start with SQL fundamentals, progress to joins and subqueries, and then challenge yourself with window functions and real-world database scenarios.

Whether you're preparing for your first technical interview or improving your existing SQL skills, consistent practice can help you build confidence.

Start practicing SQL with LogicByte and take the next step in your technical interview preparation.