SQL Interview Questions and Answers Part 1 | Complete SQL Interview Preparation

SQL Interview Questions and Answers

SQL (Structured Query Language) is one of the most important skills required for database developers, software engineers, data analysts, and backend developers. Almost every organization that works with data uses SQL for storing, managing, retrieving, and analyzing information.

During technical interviews, companies ask SQL questions to check a candidate's understanding of database concepts, query writing ability, data manipulation skills, and problem-solving approach.

This SQL Interview Questions guide covers frequently asked questions from beginner to intermediate levels with simple explanations and practical examples. These questions are useful for B.Tech Computer Science students, freshers, experienced developers, and database professionals.


Why SQL is Important for Interviews?

SQL knowledge is required in almost every software-related role because databases are the foundation of modern applications. Websites, mobile applications, banking systems, e-commerce platforms, and enterprise software all depend on databases.


Basic SQL Interview Questions

1. What is SQL?

SQL stands for Structured Query Language. It is a standard programming language used to communicate with relational databases.

SQL allows users to create databases, create tables, insert records, update data, delete information, and retrieve required data using queries.

Examples of databases that support SQL are MySQL, Oracle Database, Microsoft SQL Server, PostgreSQL, and SQLite.


2. What is a Database?

A database is an organized collection of related information stored electronically. It allows users and applications to efficiently store, access, and manage data.

For example, a college database may store information about students, courses, teachers, and examination records.


3. What is DBMS?

DBMS stands for Database Management System. It is software that allows users to create, store, manage, and control databases.

Examples:


4. What is RDBMS?

RDBMS stands for Relational Database Management System. It stores data in the form of tables consisting of rows and columns.

RDBMS follows relationships between tables using keys such as primary keys and foreign keys.

Examples:


5. Difference Between DBMS and RDBMS

DBMS RDBMS
Stores data as files or simple structures Stores data in related tables
May not support relationships Supports relationships between tables
Less secure compared to RDBMS Provides better security and integrity

SQL Commands Interview Questions

6. What are SQL Commands?

SQL commands are instructions used to perform operations on databases.

SQL commands are divided into five main categories:

Category Purpose
DDL Used to define database structure
DML Used to modify data
DQL Used to retrieve data
DCL Used for permissions and security
TCL Used for transaction control

7. What is DDL in SQL?

DDL stands for Data Definition Language. It is used to create and modify database objects.

Common DDL commands:

Example:


CREATE TABLE Student
(
ID INT,
Name VARCHAR(50)
);


8. What is DML in SQL?

DML stands for Data Manipulation Language. It is used to modify existing data inside tables.

DML commands include:

Example:

INSERT INTO Student
VALUES(1,'Rahul');

9. What is DQL?

DQL stands for Data Query Language. It is used to retrieve data from databases.

The main DQL command is SELECT.

Example:

SELECT * FROM Student;

SQL Table Related Interview Questions

10. What is a Table?

A table is a database object that stores data in rows and columns.

Each table contains:

Example:

ID Name Age
1 Amit 20

11. What is a Primary Key?

A primary key is a column or combination of columns that uniquely identifies every record in a table.

Properties of Primary Key:

Example:

CREATE TABLE Student
(
Student_ID INT PRIMARY KEY,
Name VARCHAR(50)
);

12. What is a Foreign Key?

A foreign key is a column that creates a relationship between two tables.

It references the primary key of another table.

Example:

CREATE TABLE Orders
(
Order_ID INT,
Customer_ID INT,
FOREIGN KEY(Customer_ID)
REFERENCES Customer(Customer_ID)
);

13. What is a Candidate Key?

A candidate key is a column or set of columns that can uniquely identify records in a table.

A table can have multiple candidate keys, but only one is selected as the primary key.


14. What is a Super Key?

A super key is any combination of columns that uniquely identifies a record.

A candidate key is a minimal form of super key.


15. What is a Composite Key?

A composite key is a key created using two or more columns together to uniquely identify a record.

Example:

CREATE TABLE Enrollment
(
Student_ID INT,
Course_ID INT,
PRIMARY KEY(Student_ID, Course_ID)
);

SQL Constraints Interview Questions

16. What are SQL Constraints?

Constraints are rules applied to table columns to maintain accuracy and reliability of data.

Common SQL constraints are:


17. What is NOT NULL Constraint?

NOT NULL prevents a column from storing empty values.

CREATE TABLE Employee
(
Name VARCHAR(50) NOT NULL
);

18. What is UNIQUE Constraint?

UNIQUE ensures that all values in a column are different.

Example:

Email VARCHAR(100) UNIQUE;

19. What is CHECK Constraint?

CHECK constraint limits the values allowed in a column.

Age INT CHECK(Age>=18);

20. What is DEFAULT Constraint?

DEFAULT assigns an automatic value when no value is provided.

Status VARCHAR(20)
DEFAULT 'Active';

21. What are SQL Joins?

SQL Joins are used to combine data from two or more tables based on a related column between them.

In relational databases, information is usually divided into multiple tables to avoid data duplication. Joins help retrieve meaningful information by connecting these tables.

Example:

Suppose we have two tables:

A JOIN can display student details along with their course information.


22. What are the Different Types of SQL Joins?

The main types of SQL Joins are:

Join Type Description
INNER JOIN Returns matching records from both tables.
LEFT JOIN Returns all records from left table and matching records from right table.
RIGHT JOIN Returns all records from right table and matching records from left table.
FULL JOIN Returns all matching and non-matching records from both tables.
SELF JOIN Joins a table with itself.

23. Explain INNER JOIN with Example

INNER JOIN returns only those records where a matching value exists in both tables.

Syntax:

SELECT column_name
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Example:

SELECT Student.Name, Course.Course_Name
FROM Student
INNER JOIN Course
ON Student.Course_ID = Course.Course_ID;

This query displays only students who have assigned courses.


24. Explain LEFT JOIN

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

SELECT Employee.Name, Department.Department_Name
FROM Employee
LEFT JOIN Department
ON Employee.Department_ID = Department.ID;

If an employee does not have a department, NULL values are displayed.


25. What is a SELF JOIN?

A SELF JOIN is a join where a table is joined with itself.

It is useful when comparing records within the same table.

Example:

SELECT A.Employee_Name, B.Manager_Name
FROM Employee A
JOIN Employee B
ON A.Manager_ID = B.Employee_ID;

This can be used to display employee-manager relationships.


26. What is a Subquery in SQL?

A subquery is a query written inside another query. It is also called a nested query.

The inner query executes first, and its result is used by the outer query.

Example:

SELECT Name
FROM Employee
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employee
);

This query displays employees earning more than the average salary.


27. What are the Types of Subqueries?

Subqueries are classified into different types:


28. What is a Correlated Subquery?

A correlated subquery is a subquery that depends on the outer query. It executes once for every row processed by the outer query.

Example:

SELECT Employee_Name
FROM Employee E
WHERE Salary >
(
SELECT AVG(Salary)
FROM Employee
WHERE Department_ID = E.Department_ID
);

29. What is Normalization in SQL?

Normalization is a database design technique used to organize data efficiently and reduce data redundancy.

The main goal of normalization is to eliminate duplicate data and improve database consistency.

Advantages of Normalization:


30. Explain Different Normal Forms

Normal Form Description
1NF Removes repeating groups and stores atomic values.
2NF Removes partial dependency.
3NF Removes transitive dependency.
BCNF Advanced version of 3NF.

31. What is Denormalization?

Denormalization is the process of intentionally adding duplicate data to improve query performance.

It is commonly used in reporting systems where faster data retrieval is required.


32. What is an Index in SQL?

An index is a database object that improves the speed of data retrieval operations.

Indexes work similar to an index in a book, helping the database quickly find required information.

Example:

CREATE INDEX idx_name
ON Employee(Name);

Advantages:

Disadvantages:


33. What are SQL Views?

A View is a virtual table created from the result of an SQL query.

Views do not store actual data; they display data from existing tables.

Syntax:

CREATE VIEW Employee_View AS
SELECT Name, Salary
FROM Employee;

Advantages:


34. What are Stored Procedures?

A Stored Procedure is a collection of SQL statements stored inside the database that can be executed whenever required.

Advantages:

Example:

CREATE PROCEDURE GetEmployees()
BEGIN
SELECT * FROM Employee;
END;

35. Difference Between Function and Stored Procedure

Function Stored Procedure
Must return a value May or may not return value
Used inside SQL expressions Executed independently
Cannot modify database in some systems Can perform database operations

36. What are SQL Transactions?

A transaction is a sequence of SQL operations performed as a single unit of work.

A transaction is completed only when all operations are successfully executed.

Example:

Bank money transfer is a common example:


37. Explain ACID Properties

Property Meaning
Atomicity Transaction completes fully or fails completely.
Consistency Database remains valid before and after transaction.
Isolation Transactions execute independently.
Durability Committed data remains permanently stored.

38. What is SQL Injection?

SQL Injection is a security attack where malicious SQL commands are inserted into application input fields.

Prevention Methods:


39. How to Optimize SQL Queries?

SQL query optimization improves database performance.


40. Difference Between DELETE, DROP, and TRUNCATE

DELETE TRUNCATE DROP
Removes selected rows Removes all rows Removes complete table
Can use WHERE condition No WHERE condition Table structure deleted
Can rollback in some systems Limited rollback support Cannot recover easily

41. Real-Time SQL Interview Scenario

Find the second highest salary from Employee table.

SELECT MAX(Salary)
FROM Employee
WHERE Salary <
(
SELECT MAX(Salary)
FROM Employee
);

42. Find Duplicate Records in SQL

SELECT Name, COUNT(*)
FROM Employee
GROUP BY Name
HAVING COUNT(*) > 1;

43. Find Employees Without Department

SELECT Employee.Name
FROM Employee
LEFT JOIN Department
ON Employee.Department_ID = Department.ID
WHERE Department.ID IS NULL;

44. What is the Difference Between WHERE and HAVING?

WHERE HAVING
Filters rows before grouping Filters groups after GROUP BY
Used with normal conditions Used with aggregate functions

45. What is the Difference Between UNION and UNION ALL?

UNION UNION ALL
Removes duplicate records Keeps duplicate records
Slower Faster

Conclusion

SQL interview preparation requires strong understanding of database concepts, query writing skills, and practical problem-solving ability. Advanced SQL topics like joins, indexes, normalization, transactions, and optimization are frequently asked in technical interviews.

By practicing these questions regularly, students and developers can improve their confidence and perform better in SQL-based interviews.

This complete SQL Interview Questions series covers important concepts required for fresher and experienced-level interviews.

← Previous: SQL Triggers Next: SQL Notes →
Home Visit Our YouTube Channel