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.
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.
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.
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.
DBMS stands for Database Management System. It is software that allows users to create, store, manage, and control databases.
Examples:
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:
| 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 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 |
DDL stands for Data Definition Language. It is used to create and modify database objects.
Common DDL commands:
CREATE TABLE Student ( ID INT, Name VARCHAR(50) );
DML stands for Data Manipulation Language. It is used to modify existing data inside tables.
DML commands include:
INSERT INTO Student VALUES(1,'Rahul');
DQL stands for Data Query Language. It is used to retrieve data from databases.
The main DQL command is SELECT.
SELECT * FROM Student;
A table is a database object that stores data in rows and columns.
Each table contains:
Example:
| ID | Name | Age |
|---|---|---|
| 1 | Amit | 20 |
A primary key is a column or combination of columns that uniquely identifies every record in a table.
Properties of Primary Key:
CREATE TABLE Student ( Student_ID INT PRIMARY KEY, Name VARCHAR(50) );
A foreign key is a column that creates a relationship between two tables.
It references the primary key of another table.
CREATE TABLE Orders ( Order_ID INT, Customer_ID INT, FOREIGN KEY(Customer_ID) REFERENCES Customer(Customer_ID) );
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.
A super key is any combination of columns that uniquely identifies a record.
A candidate key is a minimal form of super key.
A composite key is a key created using two or more columns together to uniquely identify a record.
CREATE TABLE Enrollment ( Student_ID INT, Course_ID INT, PRIMARY KEY(Student_ID, Course_ID) );
Constraints are rules applied to table columns to maintain accuracy and reliability of data.
Common SQL constraints are:
NOT NULL prevents a column from storing empty values.
CREATE TABLE Employee ( Name VARCHAR(50) NOT NULL );
UNIQUE ensures that all values in a column are different.
Example:
Email VARCHAR(100) UNIQUE;
CHECK constraint limits the values allowed in a column.
Age INT CHECK(Age>=18);
DEFAULT assigns an automatic value when no value is provided.
Status VARCHAR(20) DEFAULT 'Active';
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.
Suppose we have two tables:
A JOIN can display student details along with their course information.
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. |
INNER JOIN returns only those records where a matching value exists in both tables.
SELECT column_name FROM table1 INNER JOIN table2 ON table1.column = table2.column;
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.
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.
A SELF JOIN is a join where a table is joined with itself.
It is useful when comparing records within the same table.
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.
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.
SELECT Name FROM Employee WHERE Salary > ( SELECT AVG(Salary) FROM Employee );
This query displays employees earning more than the average salary.
Subqueries are classified into different types:
A correlated subquery is a subquery that depends on the outer query. It executes once for every row processed by the outer query.
SELECT Employee_Name FROM Employee E WHERE Salary > ( SELECT AVG(Salary) FROM Employee WHERE Department_ID = E.Department_ID );
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.
| Normal Form | Description |
|---|---|
| 1NF | Removes repeating groups and stores atomic values. |
| 2NF | Removes partial dependency. |
| 3NF | Removes transitive dependency. |
| BCNF | Advanced version of 3NF. |
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.
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.
CREATE INDEX idx_name ON Employee(Name);
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.
CREATE VIEW Employee_View AS SELECT Name, Salary FROM Employee;
A Stored Procedure is a collection of SQL statements stored inside the database that can be executed whenever required.
CREATE PROCEDURE GetEmployees() BEGIN SELECT * FROM Employee; END;
| 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 |
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.
Bank money transfer is a common example:
| 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. |
SQL Injection is a security attack where malicious SQL commands are inserted into application input fields.
SQL query optimization improves database performance.
| 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 |
SELECT MAX(Salary) FROM Employee WHERE Salary < ( SELECT MAX(Salary) FROM Employee );
SELECT Name, COUNT(*) FROM Employee GROUP BY Name HAVING COUNT(*) > 1;
SELECT Employee.Name FROM Employee LEFT JOIN Department ON Employee.Department_ID = Department.ID WHERE Department.ID IS NULL;
| WHERE | HAVING |
|---|---|
| Filters rows before grouping | Filters groups after GROUP BY |
| Used with normal conditions | Used with aggregate functions |
| UNION | UNION ALL |
|---|---|
| Removes duplicate records | Keeps duplicate records |
| Slower | Faster |
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.