🎯 Free Interview Preparation

SQL Interview Questions & Answers

Complete SQL interview preparation guide.

SQL Interview Questions & Answers (2026)

Complete SQL interview preparation guide.

Junior Question #1: What is SQL?

Level: Junior | Category: Fundamentals | Time: 7 mins

Example

SQL (Structured Query Language) is a standard language used to communicate with relational databases. It is used to create, retrieve, update and delete data.

SELECT *

FROM Employees;

Common Uses

  • Insert Data
  • Update Data
  • Delete Data
  • Retrieve Data
  • Create Database Objects
Interviewer Note: 💡 Candidate should explain SQL is used to interact with relational databases.

Junior Question #2: What is the difference between SQL and MySQL?

Level: Junior | Category: Fundamentals | Time: 7 mins

Expected Answer

SQL MySQL
Language Database Management System
Standard Implementation

SQL is the language used to query databases. MySQL is a database server that understands SQL.

Junior Question #3: What are the different SQL commands?

Level: Junior | Category: Fundamentals | Time: 7 mins

Expected Answer

Category Commands
DDL CREATE, ALTER, DROP
DML INSERT, UPDATE, DELETE
DQL SELECT
DCL GRANT, REVOKE
TCL COMMIT, ROLLBACK

Junior Question #4: What is a Primary Key?

Level: Junior | Category: Fundamentals | Time: 7 mins

Characteristics

A Primary Key uniquely identifies each row in a table.

  • Unique
  • Cannot be NULL
  • One Primary Key per table
CREATE TABLE Employees (

  EmployeeId INT PRIMARY KEY,

  Name VARCHAR(100)

);
Interviewer Note: 💡 Can a table exist without a Primary Key? Yes, but it is not recommended.

Junior Question #5: What is a Foreign Key?

Level: Junior | Category: Fundamentals | Time: 7 mins

Benefits

A Foreign Key creates a relationship between two tables.

CREATE TABLE Orders (

  OrderId INT PRIMARY KEY,

  CustomerId INT,

  FOREIGN KEY(CustomerId)

  REFERENCES Customers(CustomerId)

);
  • Maintains Data Integrity
  • Prevents Invalid Data
  • Creates Relationships

Junior Question #6: Difference Between DELETE, TRUNCATE and DROP

Level: Junior | Category: Fundamentals | Time: 7 mins

Examples

Command Description
DELETE Removes rows
TRUNCATE Removes all rows
DROP Deletes table completely
DELETE FROM Employees;

TRUNCATE TABLE Employees;

DROP TABLE Employees;

Junior Question #7: What is the WHERE clause?

Level: Junior | Category: Fundamentals | Time: 7 mins

Common Operators

WHERE filters records based on conditions.

SELECT *

FROM Employees

WHERE Salary > 50000;
  • =
  • !=
  • >
  • << /li>
  • IN
  • LIKE

Junior Question #8: Difference Between WHERE and HAVING

Level: Junior | Category: Fundamentals | Time: 7 mins

Example

WHERE HAVING
Filters Rows Filters Groups
Before GROUP BY After GROUP BY
SELECT DepartmentId,
       COUNT(*)

FROM Employees

GROUP BY DepartmentId

HAVING COUNT(*) > 5;

Junior Question #9: What is GROUP BY?

Level: Junior | Category: Fundamentals | Time: 7 mins

Typical Usage

GROUP BY groups rows that have the same values.

SELECT

  DepartmentId,

  COUNT(*) AS EmployeeCount

FROM Employees

GROUP BY DepartmentId;
  • COUNT
  • SUM
  • AVG
  • MIN
  • MAX

Junior Question #10: What are Aggregate Functions?

Level: Junior | Category: Fundamentals | Time: 7 mins

Example

Aggregate functions perform calculations on multiple rows and return a single value.

Function Description
COUNT() Counts rows
SUM() Total value
AVG() Average value
MIN() Minimum value
MAX() Maximum value
SELECT

  AVG(Salary) AS AverageSalary,

  MAX(Salary) AS HighestSalary,

  MIN(Salary) AS LowestSalary

FROM Employees;
Interviewer Note: 💡 Very common fresher interview topic.

Mid-Level Question #11: What is an INNER JOIN?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Expected Answer

INNER JOIN returns only matching records from both tables.

SELECT

  e.EmployeeName,

  d.DepartmentName

FROM Employees e

INNER JOIN Departments d

ON e.DepartmentId = d.DepartmentId;
Interviewer Note: 💡 What happens when a matching record doesn't exist? Row is excluded from result.

Mid-Level Question #12: What is a LEFT JOIN?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Example Use Case

LEFT JOIN returns all records from the left table and matching records from the right table.

SELECT

  e.EmployeeName,

  d.DepartmentName

FROM Employees e

LEFT JOIN Departments d

ON e.DepartmentId = d.DepartmentId;

Find employees who are not assigned to any department.

Mid-Level Question #13: Difference Between INNER JOIN and LEFT JOIN

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Expected Answer

INNER JOIN LEFT JOIN
Only matching rows All rows from left table
Unmatched rows excluded Unmatched rows included

Mid-Level Question #14: What is a Self Join?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Employee Manager Example

A Self Join joins a table with itself.

SELECT

  e.EmployeeName,

  m.EmployeeName AS ManagerName

FROM Employees e

LEFT JOIN Employees m

ON e.ManagerId = m.EmployeeId;

Mid-Level Question #15: What is a Subquery?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Expected Answer

A Subquery is a query nested inside another query.

SELECT *

FROM Employees

WHERE Salary >

(

  SELECT AVG(Salary)

  FROM Employees

);
Interviewer Note: 💡 Common Difference between Subquery and Join?

Mid-Level Question #16: What is a View?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Benefits

A View is a virtual table created from a SQL query.

CREATE VIEW ActiveEmployees AS

SELECT *

FROM Employees

WHERE IsActive = 1;
  • Reusable Queries
  • Security
  • Simplified Access

Mid-Level Question #17: What is Normalization?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Expected Answer

Normalization reduces data redundancy and improves data consistency.

Normal Form Purpose
1NF Atomic values
2NF Remove partial dependency
3NF Remove transitive dependency

Mid-Level Question #18: What is Denormalization?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Example

Denormalization intentionally adds redundancy to improve read performance.

Storing customer name directly in Orders table to avoid frequent joins.

Interviewer Note: 💡 Expected discussion: Performance vs Data Consistency trade-off.

Mid-Level Question #19: What is a Stored Procedure?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Advantages

A Stored Procedure is a precompiled SQL program stored in the database.

CREATE PROCEDURE GetEmployees

AS

BEGIN

  SELECT *

  FROM Employees

END
  • Reusability
  • Performance
  • Security

Mid-Level Question #20: What are SQL Constraints?

Level: Mid-Level | Category: Joins & Objects | Time: 10 mins

Expected Answer

Constraints enforce rules on table data.

Constraint Purpose
PRIMARY KEY Unique Identifier
FOREIGN KEY Relationship
UNIQUE Unique Values
NOT NULL Mandatory Value
CHECK Validation Rule
DEFAULT Default Value
CREATE TABLE Users (

  UserId INT PRIMARY KEY,

  Email VARCHAR(100) UNIQUE,

  Age INT CHECK (Age >= 18)

);
Interviewer Note: 💡 Mid-level candidates should explain when and why each constraint is used.

Senior Question #21: What is an Index?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Example

An Index is a database object that improves query performance by reducing the amount of data scanned.

CREATE INDEX IX_Employee_Email

ON Employees(Email);

Benefits

  • Faster Search Operations
  • Reduced Table Scans
  • Improved Query Performance
Interviewer Note: 💡 Why shouldn't every column be indexed? More indexes increase INSERT, UPDATE and DELETE costs.

Senior Question #22: Difference Between Clustered and Non-Clustered Index

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Expected Answer

Clustered Index Non-Clustered Index
Stores actual data Stores references to data
Only one per table Multiple allowed
Physically sorts rows Separate structure
Interviewer Note: 💡 Frequently asked senior-level interview question.

Senior Question #23: What is a Transaction?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Real World Example

A Transaction is a group of operations executed as a single unit.

BEGIN TRANSACTION;

UPDATE Accounts
SET Balance = Balance - 1000
WHERE Id = 1;

UPDATE Accounts
SET Balance = Balance + 1000
WHERE Id = 2;

COMMIT;

Bank money transfer between accounts.

Senior Question #24: What are ACID Properties?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Expected Answer

Property Description
Atomicity All or Nothing
Consistency Valid State
Isolation No Interference
Durability Permanent Storage
Interviewer Note: 💡 Banking systems commonly use ACID guarantees.

Senior Question #25: What is a Deadlock?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Example

Deadlock occurs when two transactions wait for each other indefinitely.

  • Transaction A locks Table 1
  • Transaction B locks Table 2
  • A waits for Table 2
  • B waits for Table 1

Prevention

  • Access tables in same order
  • Keep transactions short
  • Proper indexing

Senior Question #26: What is a Cursor?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Expected Answer

A Cursor processes rows one at a time.

DECLARE EmployeeCursor CURSOR

FOR

SELECT EmployeeId

FROM Employees;
Interviewer Note: 💡 Strong candidates explain that set-based operations are generally preferred over cursors.

Senior Question #27: What is Query Optimization?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Common Techniques

Query Optimization improves performance and reduces execution time.

  • Proper Indexing
  • Avoid SELECT *
  • Optimize Joins
  • Reduce Subqueries
  • Partition Large Tables
Interviewer Note: 💡 Senior candidates should discuss execution plans.

Senior Question #28: What is an Execution Plan?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Things to Look For

Execution Plan shows how SQL Server executes a query.

  • Table Scans
  • Index Scans
  • Index Seeks
  • Missing Indexes
  • High Cost Operators
Interviewer Note: 💡 Expected answer includes Index Seek vs Table Scan discussion.

Senior Question #29: What are SQL Window Functions?

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Common Window Functions

Window Functions perform calculations across rows without collapsing results.

SELECT

  EmployeeName,

  Salary,

  ROW_NUMBER() OVER(

    ORDER BY Salary DESC

  ) AS RankNo

FROM Employees;
  • ROW_NUMBER()
  • RANK()
  • DENSE_RANK()
  • LAG()
  • LEAD()

Senior Question #30: Difference Between ROW_NUMBER(), RANK() and DENSE_RANK()

Level: Senior | Category: Advanced & Internals | Time: 12 mins

Example

Function Behavior
ROW_NUMBER() Always unique sequence
RANK() Skips numbers after ties
DENSE_RANK() No gaps after ties
Salary ROW_NUMBER RANK DENSE_RANK
100 1 1 1
100 2 1 1
90 3 3 2
Interviewer Note: 💡 Very common SQL Server, Oracle and PostgreSQL interview question.

Practical Question #31: Find the Second Highest Salary

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Expected Answer

One of the most frequently asked SQL interview questions.

SELECT MAX(Salary)

FROM Employees

WHERE Salary < (

  SELECT MAX(Salary)

  FROM Employees

);
Interviewer Note: 💡 How would you find the 3rd or Nth highest salary?

Practical Question #32: Find Duplicate Records

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Expected Answer

Find duplicate email addresses.

SELECT

  Email,

  COUNT(*) AS Total

FROM Users

GROUP BY Email

HAVING COUNT(*) > 1;
Interviewer Note: 💡 Common question for testing GROUP BY and HAVING knowledge.

Practical Question #33: Delete Duplicate Records

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Expected Answer

WITH DuplicateUsers AS (

  SELECT

    Id,

    ROW_NUMBER() OVER (

      PARTITION BY Email

      ORDER BY Id

    ) AS RowNum

  FROM Users

)

DELETE

FROM DuplicateUsers

WHERE RowNum > 1;
Interviewer Note: 💡 Strong candidates understand ROW_NUMBER() usage.

Practical Question #34: Find Employees With Highest Salary In Each Department

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Advanced Version

SELECT

  DepartmentId,

  MAX(Salary) AS HighestSalary

FROM Employees

GROUP BY DepartmentId;
WITH RankedEmployees AS (

  SELECT

    EmployeeName,

    DepartmentId,

    Salary,

    RANK() OVER (

      PARTITION BY DepartmentId

      ORDER BY Salary DESC

    ) AS RankNo

  FROM Employees

)

SELECT *

FROM RankedEmployees

WHERE RankNo = 1;

Practical Question #35: Find Top 3 Highest Salaries

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

MySQL Version

SELECT TOP 3 *

FROM Employees

ORDER BY Salary DESC;
SELECT *

FROM Employees

ORDER BY Salary DESC

LIMIT 3;

Practical Question #36: Find Employees Who Joined In Last 30 Days

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Expected Answer

SELECT *

FROM Employees

WHERE JoiningDate >=

DATEADD(DAY, -30, GETDATE());
Interviewer Note: 💡 Tests Date Functions knowledge.

Practical Question #37: Find Missing IDs

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Expected Answer

IDs: 1,2,3,5,6,7 Missing = 4

SELECT

  t1.Id + 1 AS MissingId

FROM Employees t1

LEFT JOIN Employees t2

ON t1.Id + 1 = t2.Id

WHERE t2.Id IS NULL;
Interviewer Note: 💡 Frequently asked in product companies.

Practical Question #38: Find Department Wise Employee Count

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Expected Output

SELECT

  DepartmentId,

  COUNT(*) AS EmployeeCount

FROM Employees

GROUP BY DepartmentId;
Department Total Employees
IT 25
HR 8

Practical Question #39: Find Employees Without Manager

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Real Scenario

SELECT *

FROM Employees

WHERE ManagerId IS NULL;

Find CEO or top-level managers.

Practical Question #40: Find Running Total Of Sales

Level: Coding Challenge | Category: Practical Queries | Time: 15 mins

Sample Output

Frequently asked Window Function problem.

SELECT

  OrderDate,

  Amount,

  SUM(Amount) OVER (

    ORDER BY OrderDate

  ) AS RunningTotal

FROM Orders;
Date Amount Running Total
01-Jan 100 100
02-Jan 200 300
03-Jan 150 450
Interviewer Note: 💡 Difference between SUM() and SUM() OVER()? SUM() groups rows. SUM() OVER() preserves individual rows while calculating aggregates.

Architect Question #41: How Would You Optimize a Slow Query?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Investigation Steps

  • Check Execution Plan
  • Identify Table Scans
  • Review Missing Indexes
  • Analyze Join Conditions
  • Review Statistics
  • Check Blocking Sessions

Example

SELECT *

FROM Orders

WHERE CustomerId = 100;

If CustomerId is frequently searched, create an index.

CREATE INDEX IX_Orders_CustomerId

ON Orders(CustomerId);
Interviewer Note: 💡 Strong candidates discuss execution plans before adding indexes.

Architect Question #42: How Would You Design an E-Commerce Database?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Core Tables

  • Users
  • Products
  • Categories
  • Orders
  • OrderItems
  • Payments
  • Coupons
  • Inventory

Relationships

  • User → Orders
  • Order → OrderItems
  • Product → Category
  • Order → Payment
Interviewer Note: 💡 Strong candidates discuss indexing, inventory locking and scaling.

Architect Question #43: How Would You Handle 100 Million Records?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Strategies

  • Table Partitioning
  • Read Replicas
  • Sharding
  • Redis Cache
  • Archiving
  • Optimized Indexes

Common Mistake

Adding more CPU without fixing bad queries.

Architect Question #44: What is Database Sharding?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Example

Sharding distributes data across multiple databases.

Shard Customers
Shard 1 1 - 1,000,000
Shard 2 1,000,001 - 2,000,000
Interviewer Note: 💡 What challenges come with sharding?

Architect Question #45: What is Database Replication?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Benefits

Replication copies data from a primary database to secondary databases.

  • Read Scaling
  • High Availability
  • Disaster Recovery

Architecture

  • Primary Database → Writes
  • Replica Databases → Reads

Architect Question #46: How Would You Prevent Database Deadlocks?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Real Example

  • Access Tables In Same Order
  • Keep Transactions Short
  • Use Proper Indexes
  • Avoid Long Running Transactions
  • Reduce Lock Duration

Payment systems often experience deadlocks during concurrent updates.

Architect Question #47: How Would You Design an Audit Logging System?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Audit Table

AuditLogs

(
  Id,
  UserId,
  Action,
  TableName,
  OldValue,
  NewValue,
  CreatedDate
)

Requirements

  • Immutable Records
  • Searchable
  • Retention Policy
  • Security Compliance

Architect Question #48: How Would You Design a Multi-Tenant Database?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Approaches

Approach Description
Shared Database TenantId Column
Separate Schema Schema Per Tenant
Separate Database Database Per Tenant
Interviewer Note: 💡 Which approach would you choose for SaaS products?

Architect Question #49: How Would You Design a High-Traffic Banking Database?

Level: Architect | Category: Architecture & Design | Time: 15 mins

Critical Requirement

  • Strict ACID Transactions
  • Optimistic/Pessimistic Locking
  • Read Replicas
  • Audit Logs
  • Encryption
  • Disaster Recovery

Data consistency is more important than raw performance.

Architect Question #50: Design a Database for a Social Media Platform

Level: Architect | Category: Architecture & Design | Time: 15 mins

Core Tables

  • Users
  • Posts
  • Comments
  • Likes
  • Followers
  • Messages
  • Notifications

Scaling Considerations

  • Read Replicas
  • Caching
  • Partitioning
  • Search Engine Integration
  • Event Driven Processing
Interviewer Note: 💡 This question evaluates database design, scalability, indexing, caching and architecture knowledge.

Frequently Asked Questions

Are SQL coding questions included?

Yes. Real-world SQL queries, joins, window functions and optimization questions are covered.

Is this suitable for experienced developers?

Yes. Junior, Mid-Level, Senior and Architect level SQL questions are included.

Does it cover performance tuning?

Yes. Indexing, execution plans and optimization discussions are included.