Top 10 SQL Interview Questions and Answers for Freshers (2026)
Question 1: What Is SQL?
Introduction
SQL (Structured Query Language) is a standard programming language used to communicate with relational databases. It allows users to store, retrieve, update, and delete data efficiently.
SQL is one of the most important skills for Java Developers, Full Stack Developers, Data Analysts, and Database Administrators.
Popular databases that use SQL include:
MySQL
PostgreSQL
Oracle
SQL Server
Why SQL Is Important
Organizations use SQL because it:
Stores Large Amounts of Data
Retrieves Information Quickly
Supports Data Analysis
Maintains Data Integrity
Powers Business Applications
Almost every enterprise application uses SQL databases.
SQL Architecture Diagram
Application
|
v
SQL Query
|
v
Database Server
|
v
Data Storage
Common SQL Operations
Insert Data
INSERT INTO students(id,name)
VALUES(1,'John');
Retrieve Data
SELECT * FROM students;
Update Data
UPDATE students
SET name='David'
WHERE id=1;
Delete Data
DELETE FROM students
WHERE id=1; Interview Answer
SQL (Structured Query Language) is a standard language used to communicate with relational databases. It allows users to create, retrieve, update, and delete data efficiently using commands such as SELECT, INSERT, UPDATE, and DELETE.
Common Mistakes
❌ SQL is a database.
❌ SQL is a programming language like Java.
Correct:
✅ SQL is a database query language.
✅ SQL is used to manage relational databases.
Interview Tip
Whenever an interviewer asks:
"What is SQL?"
Always mention:
Structured Query Language
Relational Database
CRUD Operations
Data Management
Question 2: What Are the Different Types of SQL Commands?
Introduction
SQL (Structured Query Language) provides various commands to interact with relational databases. These commands help users create databases, retrieve data, modify records, control permissions, and manage transactions.
To make SQL easier to understand, commands are divided into different categories based on their functionality.
Understanding SQL command types is one of the most frequently asked SQL interview questions.
Why SQL Commands Are Important
SQL commands help developers:
Create Database Objects
Store Data
Retrieve Information
Modify Records
Manage User Access
Control Transactions
Without SQL commands, database operations would not be possible.
Types of SQL Commands
SQL commands are classified into:
1. DDL (Data Definition Language)
Used to define database structures.
Commands:
CREATE
ALTER
DROP
TRUNCATE
2. DML (Data Manipulation Language)
Used to manipulate data inside tables.
Commands:
INSERT
UPDATE
DELETE
3. DQL (Data Query Language)
Used to retrieve data.
Command:
SELECT
4. DCL (Data Control Language)
Used to control user permissions.
Commands:
GRANT
REVOKE
5. TCL (Transaction Control Language)
Used to manage transactions.
Commands:
COMMIT
ROLLBACK
SAVEPOINT
SQL Commands Classification Diagram
SQL Commands
|
-------------------------------------------------
| | | | |
v v v v v
DDL DML DQL DCL TCL
CREATE INSERT SELECT GRANT COMMIT
ALTER UPDATE REVOKE ROLLBACK
DROP DELETE SAVEPOINT
TRUNCATE
1. DDL Commands
CREATE
Used to create database objects.
Example:
CREATE TABLE students(
id INT,
name VARCHAR(50)
);
ALTER
Used to modify table structure.
Example:
ALTER TABLE students
ADD email VARCHAR(100);
DROP
Used to delete a table permanently.
Example:
DROP TABLE students;
TRUNCATE
Removes all records from a table.
Example:
TRUNCATE TABLE students;
2. DML Command
INSERTs
Adds records into a table.
Example:
INSERT INTO students
VALUES(1,'John');
UPDATE
Modifies existing records.
Example:
UPDATE students
SET name='David'
WHERE id=1;
DELETE
Removes records from a table.
Example:
DELETE FROM students
WHERE id=1;
3. DQL Command
SELECT
Retrieves data from a table.
Example:
SELECT * FROM students;
4. DCL Commands
GRANT
Provides permissions.
Example:
GRANT SELECT
ON students
TO user1;
REVOKE
Removes permissions.
Example:
REVOKE SELECT
ON students
FROM user1;
5. TCL Commands
COMMIT
Saves changes permanently.
Example:
COMMIT;
ROLLBACK
Undoes changes.
Example:
ROLLBACK;
SAVEPOINT
Creates a transaction checkpoint.
Example:
SAVEPOINT sp1;
Real-World Example
Consider an Education Management System.
CREATE TABLE Student
|
v
INSERT Student Data
|
v
SELECT Student Data
|
v
UPDATE Student Details
|
v
DELETE Student Record
Every operation uses different SQL command categories.
Advantages of SQL Commands
Organized Database Management
Different commands serve different purposes.
Better Security
Permission control using DCL.
Transaction Management
Data consistency through TCL.
Easy Data Manipulation
DML commands simplify record handling.
Efficient Data Retrieval
DQL commands provide quick access to data.
Interview Answer
SQL commands are categorized into five types: DDL (Data Definition Language), DML (Data Manipulation Language), DQL (Data Query Language), DCL (Data Control Language), and TCL (Transaction Control Language). These command groups are used to define, manipulate, retrieve, secure, and manage database transactions.
Common Mistakes
Avoid these answers:
❌ SQL has only SELECT and INSERT.
❌ DDL and DML are the same.
Correct:
✅ DDL defines database structures.
✅ DML manipulates data.
✅ DQL retrieves data.
✅ DCL controls permissions.
✅ TCL manages transactions.
Interview Tip
Whenever an interviewer asks:
"What are the different types of SQL commands?"
Always mention:
DDL
DML
DQL
DCL
TCL
This is the answer recruiters expect.
Quick Revision
DDL → Structure
DML → Data Modification
DQL → Data Retrieval
DCL → Permissions
TCL → Transactions
One-Line Summary
SQL commands are classified into DDL, DML, DQL, DCL, and TCL based on their functionality in database management.
Question 3: What Is the Difference Between DELETE, TRUNCATE, and DROP?
Introduction
DELETE, TRUNCATE, and DROP are SQL commands used to remove data from a database. Although they appear similar, they serve different purposes and behave differently.
Understanding the difference between these commands is one of the most frequently asked SQL interview questions because improper use can result in accidental data loss.
Why This Question Is Important
In real-world applications, developers often need to:
Remove Specific Records
Clear Entire Tables
Delete Database Objects
Choosing the wrong command can lead to serious issues in production environments.
Overview of DELETE, TRUNCATE, and DROP
DELETE
|
+--> Removes Specific Records
TRUNCATE
|
+--> Removes All Records
DROP
|
+--> Removes Entire Table
1. DELETE Command
DELETE removes selected rows from a table.
The table structure remains intact.
Syntax
DELETE FROM students
WHERE id = 1;
Example
Before:
| ID | Name |
|---|---|
| 1 | John |
| 2 | David |
Query:
DELETE FROM students
WHERE id = 1;
After:
| ID | Name |
|---|---|
| 2 | David |
Only the specified record is removed.
DELETE Diagram
Students Table
|
v
DELETE id=1
|
v
Remaining Records Stay
2. TRUNCATE Command
TRUNCATE removes all records from a table.
However, the table structure remains available.
Syntax
TRUNCATE TABLE students;
Example
Before:
| ID | Name |
|---|---|
| 1 | John |
| 2 | David |
Query:
TRUNCATE TABLE students;
After:
Table Exists
No Records
TRUNCATE Diagram
Students Table
|
v
TRUNCATE
|
v
All Records Removed
|
v
Table Still Exists
3. DROP Command
DROP completely removes the table from the database.
Both:
Data
Table Structure
are deleted permanently.
Syntax
DROP TABLE students;
Example
Before:
Students Table Exists
Query:
DROP TABLE students;
After:
Students Table Does Not Exist
DROP Diagram
Students Table
|
v
DROP
|
v
Table Removed
|
v
Data Removed
Comparison Table
| Feature | DELETE | TRUNCATE | DROP |
|---|---|---|---|
| Removes Data | Yes | Yes | Yes |
| Removes Table Structure | No | No | Yes |
| WHERE Clause Supported | Yes | No | No |
| Rollback Possible | Yes | Usually No | No |
| Deletes Specific Rows | Yes | No | No |
| Deletes Entire Table | No | No | Yes |
| Speed | Slow | Fast | Fastest |
Real-World Example
Consider an Education Management System.
DELETE
Remove a single student record:
DELETE FROM students
WHERE id = 101;
TRUNCATE
Remove all student records before a new academic year:
TRUNCATE TABLE students;
DROP
Delete the entire students table:
DROP TABLE students;
Advantages
DELETE
✔ Removes specific records
✔ Supports WHERE condition
TRUNCATE
✔ Faster than DELETE
✔ Clears large tables quickly
DROP
✔ Completely removes unused tables
✔ Frees database resources
Interview Answer
DELETE removes selected rows from a table and supports the WHERE clause. TRUNCATE removes all rows from a table but keeps the table structure. DROP removes both the table structure and all data permanently from the database.
Common Mistakes
Avoid these answers:
❌ DELETE and TRUNCATE are the same.
❌ DROP only removes data.
Correct:
✅ DELETE removes specific records.
✅ TRUNCATE removes all records.
✅ DROP removes both table and data.
Interview Tip
Whenever an interviewer asks:
"What is the difference between DELETE, TRUNCATE, and DROP?"
Always explain:
Data Removal
Table Structure
WHERE Clause Support
Performance Differences
These are the points recruiters expect.
Quick Revision
DELETE → Removes Selected Rows
TRUNCATE → Removes All Rows
DROP → Removes Table and Data
One-Line Summary
DELETE removes specific records, TRUNCATE removes all records while keeping the table structure, and DROP permanently removes both the table structure and its data.
Question 4: What Is a Primary Key?
Introduction
A Primary Key is a column or a combination of columns that uniquely identifies each record in a database table.
It ensures that every row in a table is unique and prevents duplicate records.
Primary Keys are one of the most important concepts in database design and are frequently used in SQL interviews.
Why Primary Key Is Important
Primary Keys help:
Uniquely Identify Records
Prevent Duplicate Data
Improve Data Integrity
Establish Relationships Between Tables
Enhance Database Performance
Without a Primary Key, managing and identifying records becomes difficult.
Primary Key Diagram
Students Table
+------+--------+
| ID | Name |
+------+--------+
| 101 | John |
| 102 | David |
| 103 | Smith |
+------+--------+
Primary Key = ID
Each student has a unique ID.
Characteristics of a Primary Key
Unique
No duplicate values are allowed.
Example:
101
102
103
Valid
101
101
103
Invalid
Not Null
A Primary Key cannot contain NULL values.
Example:
101
NULL
103
Invalid
One Primary Key Per Table
A table can have only one Primary Key.
However, the Primary Key can consist of multiple columns (Composite Key).
Creating a Primary Key
During Table Creation
CREATE TABLE students(
id INT PRIMARY KEY,
name VARCHAR(50),
course VARCHAR(50)
);
Adding Primary Key Later
ALTER TABLE students
ADD PRIMARY KEY(id);
How Primary Key Works
New Record
|
v
Check Primary Key
|
+---- Unique?
|
Yes --------> Insert
|
No ---------> Reject
Real-World Example
Consider a Banking Application.
Customer Table:
+------------+----------+
| CustomerID | Name |
+------------+----------+
| 1001 | Rahul |
| 1002 | Priya |
| 1003 | Arjun |
+------------+----------+
CustomerID acts as the Primary Key because every customer must have a unique ID.
Advantages of Primary Key
Prevents Duplicate Records
Ensures data uniqueness.
Improves Data Integrity
Maintains database accuracy.
Supports Relationships
Used with Foreign Keys.
Faster Searching
Database indexing improves performance.
Interview Answer
A Primary Key is a column or set of columns that uniquely identifies each record in a database table. It cannot contain duplicate or NULL values and is used to maintain data integrity and establish relationships between tables.
Common Mistakes
Avoid these answers:
❌ Primary Key allows duplicates.
❌ Primary Key can contain NULL values.
Correct:
✅ Primary Key must be unique.
✅ Primary Key cannot contain NULL values.
Interview Tip
Whenever an interviewer asks:
"What is a Primary Key?"
Always mention:
Unique Identifier
No Duplicates
No NULL Values
Data Integrity
These are the key points recruiters expect.
Quick Revision
Primary Key = Unique + Not Null + Record Identifier
One-Line Summary
A Primary Key is a database column that uniquely identifies each record in a table and prevents duplicate or NULL values.
Question 5: What Is a Foreign Key?
Introduction
A Foreign Key is a column or group of columns in one table that refers to the Primary Key of another table.
It is used to establish a relationship between two database tables and maintain data consistency.
Foreign Keys are essential in relational databases because they help connect related data across multiple tables.
Why Foreign Key Is Important
Foreign Keys help:
Establish Relationships Between Tables
Maintain Data Integrity
Prevent Invalid Data Entry
Reduce Data Redundancy
Support Database Normalization
Without Foreign Keys, tables would become isolated and data consistency could be compromised.
Understanding Foreign Key
Consider two tables:
Students Table
+------+--------+
| ID | Name |
+------+--------+
| 101 | John |
| 102 | David |
| 103 | Smith |
+------+--------+
Primary Key = ID
Courses Table
+-----------+------------+
| CourseID | StudentID |
+-----------+------------+
| 1 | 101 |
| 2 | 102 |
| 3 | 103 |
+-----------+------------+
Foreign Key = StudentID
StudentID refers to the Primary Key (ID) in the Students table.
Foreign Key Relationship Diagram
Students Table
+------+--------+
| ID | Name |
+------+--------+
| 101 | John |
| 102 | David |
| 103 | Smith |
+------+--------+
|
|
v
Courses Table
+-----------+------------+
| CourseID | StudentID |
+-----------+------------+
| 1 | 101 |
| 2 | 102 |
| 3 | 103 |
+-----------+------------+
Foreign Key = StudentID
Creating a Foreign Key
Parent Table
CREATE TABLE students(
id INT PRIMARY KEY,
name VARCHAR(50)
);
Child Table
CREATE TABLE courses(
course_id INT PRIMARY KEY,
student_id INT,
FOREIGN KEY(student_id)
REFERENCES students(id)
);
How Foreign Key Works
Insert Record
|
v
Check Parent Table
|
+---- Exists?
|
Yes ------------> Insert Allowed
|
No -------------> Insert Rejected
This ensures only valid references are stored.
Real-World Example
Consider a Banking System.
Customers Table
CustomerID
1001
1002
1003
Accounts Table
AccountID CustomerID
5001 1001
5002 1002
5003 1003
CustomerID in Accounts table is a Foreign Key.
This links each account to a valid customer.
Advantages of Foreign Key
Maintains Data Integrity
Prevents invalid references.
Establishes Relationships
Connects multiple tables.
Prevents Orphan Records
Every reference must exist.
Supports Database Normalization
Reduces duplicate data.
Improves Database Design
Creates structured and organized databases.
Primary Key vs Foreign Key
| Feature | Primary Key | Foreign Key |
|---|---|---|
| Purpose | Uniquely identifies records | Creates relationship between tables |
| Duplicate Values | Not Allowed | Allowed |
| NULL Values | Not Allowed | Allowed (depending on design) |
| Number Per Table | One | Multiple |
| References Another Table | No | Yes |
Interview Answer
A Foreign Key is a column or set of columns in one table that references the Primary Key of another table. It is used to establish relationships between tables and maintain referential integrity by ensuring that only valid data is stored.
Common Mistakes
Avoid these answers:
❌ Foreign Key must be unique.
❌ Foreign Key is the same as Primary Key.
Correct:
✅ Foreign Key references a Primary Key.
✅ Foreign Key creates relationships between tables.
✅ Foreign Key can contain duplicate values.
Interview Tip
Whenever an interviewer asks:
"What is a Foreign Key?"
Always mention:
Relationship Between Tables
Primary Key Reference
Data Integrity
Referential Integrity
These are the key points interviewers expect.
Quick Revision
Primary Key → Unique Identifier
Foreign Key → Table Relationship
One-Line Summary
A Foreign Key is a column that references the Primary Key of another table and is used to establish relationships while maintaining data integrity.
Question 6: What Is the Difference Between WHERE and HAVING Clause?
Introduction.
The WHERE and HAVING clauses are used in SQL to filter data. Although both are used for filtering, they operate at different stages of query execution.
The WHERE clause filters individual rows before grouping, while the HAVING clause filters grouped data after the GROUP BY operation.
Understanding the difference between WHERE and HAVING is one of the most frequently asked SQL interview questions.
Why WHERE and HAVING Are Important
These clauses help developers:
Filter Data Efficiently
Improve Query Performance
Analyze Large Datasets
Generate Business Reports
Work with Aggregate Functions
They are commonly used in real-world SQL applications.
WHERE Clause
The WHERE clause filters records before grouping or aggregation.
Syntax
SELECT *
FROM students
WHERE age > 18;
Example
Students Table:
| ID | Name | Age |
|---|---|---|
| 1 | John | 20 |
| 2 | David | 17 |
| 3 | Smith | 22 |
Query:
SELECT *
FROM students
WHERE age > 18;
Output:
| ID | Name | Age |
|---|---|---|
| 1 | John | 20 |
| 3 | Smith | 22 |
HAVING Clause
The HAVING clause filters grouped records after the GROUP BY operation.
Syntax
SELECT department,
COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;
Example
Employees Table:
| Department |
|---|
| IT |
| IT |
| IT |
| HR |
| HR |
Query:
SELECT department,
COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 2;
Output:
| Department | Count |
|---|---|
| IT | 3 |
WHERE vs HAVING Diagram
Database Table
|
v
WHERE
(Filter Rows)
|
v
GROUP BY
(Group Data)
|
v
HAVING
(Filter Groups)
|
v
Result
Key Differences
| Feature | WHERE | HAVING |
|---|---|---|
| Filters | Rows | Groups |
| Executes | Before GROUP BY | After GROUP BY |
| Aggregate Functions | Not Allowed | Allowed |
| Used With GROUP BY | Optional | Usually Used |
| Performance | Faster | Slower |
Real-World Example
Consider an Education Management System.
WHERE Example
Find students older than 18:
SELECT *
FROM students
WHERE age > 18;
HAVING Example
Find departments with more than 10 students:
SELECT department,
COUNT(*)
FROM students
GROUP BY department
HAVING COUNT(*) > 10;
Query Execution Flow
Table Data
|
v
WHERE Clause
|
v
Filtered Rows
|
v
GROUP BY
|
v
Grouped Data
|
v
HAVING Clause
|
v
Final Result
Advantages
WHERE
✔ Faster Filtering
✔ Reduces Data Early
✔ Improves Performance
HAVING
✔ Works with Aggregate Functions
✔ Filters Grouped Data
✔ Useful for Reporting
Interview Answer
The WHERE clause filters individual rows before grouping takes place, while the HAVING clause filters grouped data after the GROUP BY operation. WHERE cannot be used with aggregate functions, whereas HAVING is specifically designed to filter aggregated results.
Common Mistakes
Avoid these answers:
❌ WHERE and HAVING are the same.
❌ HAVING is faster than WHERE.
Correct:
✅ WHERE filters rows.
✅ HAVING filters groups.
✅ HAVING works with aggregate functions.
Interview Tip
Whenever an interviewer asks:
"What is the difference between WHERE and HAVING?"
Always mention:
WHERE → Before GROUP BY
HAVING → After GROUP BY
WHERE → Rows
HAVING → Groups
HAVING → Aggregate Functions
These are the key points interviewers expect.
Quick Revision
WHERE → Filters Rows
HAVING → Filters Groups
WHERE → Before GROUP BY
HAVING → After GROUP BY
One-Line Summary
The WHERE clause filters rows before grouping, while the HAVING clause filters grouped data after the GROUP BY operation.
Question 7: What Is a JOIN in SQL?
Introduction
A JOIN is an SQL operation used to combine data from two or more tables based on a related column.
In relational databases, data is often stored across multiple tables to avoid duplication. JOIN allows us to retrieve related information from these tables in a single query.
JOIN is one of the most important SQL concepts and is frequently asked in interviews.
Why JOIN Is Important
JOIN helps developers:
Combine Data from Multiple Tables
Reduce Data Redundancy
Generate Reports
Improve Database Design
Retrieve Related Information Efficiently
Almost every real-world application uses JOIN operations.
Understanding JOIN
Consider two tables:
Students Table
| StudentID | StudentName |
|---|---|
| 101 | John |
| 102 | David |
| 103 | Smith |
Courses Table
| CourseID | StudentID | CourseName |
|---|---|---|
| 1 | 101 | Java |
| 2 | 102 | SQL |
| 3 | 103 | Spring Boot |
StudentID is the common column.
JOIN Architecture Diagram
Students Table
|
| StudentID
|
v
Courses Table
|
v
Combined Result
SQL JOIN Syntax
SELECT s.StudentName,
c.CourseName
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID;
Result
| StudentName | CourseName |
|---|---|
| John | Java |
| David | SQL |
| Smith | Spring Boot |
The JOIN combines matching records from both tables.
How JOIN Works
Students Table
|
v
Matching Column
(StudentID)
|
v
Courses Table
|
v
Combined Output
Real-World Example
Consider an E-Commerce Application.
Customers Table
| CustomerID | CustomerName |
|---|---|
| 1 | Rahul |
| 2 | Priya |
Orders Table
| OrderID | CustomerID |
|---|---|
| 1001 | 1 |
| 1002 | 2 |
Using JOIN:
SELECT c.CustomerName,
o.OrderID
FROM Customers c
JOIN Orders o
ON c.CustomerID=o.CustomerID;
Output:
| CustomerName | OrderID |
|---|---|
| Rahul | 1001 |
| Priya | 1002 |
Advantages of JOIN
Data Retrieval
Combines related information.
Better Reporting
Useful for business reports.
Reduced Duplication
Supports database normalization.
Efficient Queries
Retrieves related data in one query.
Interview Answer
A JOIN is an SQL operation used to combine records from two or more tables based on a related column. It helps retrieve connected data efficiently and is commonly used in relational databases.
Common Mistakes
Avoid these answers:
❌ JOIN merges tables permanently.
❌ JOIN creates new tables.
Correct:
✅ JOIN combines data temporarily during query execution.
✅ JOIN retrieves related records using a common column.
Interview Tip
Whenever an interviewer asks:
"What is a JOIN?"
Always explain:
Multiple Tables
Common Column
Related Data
Data Retrieval
These are the key points interviewers expect.
Quick Revision
JOIN = Multiple Tables + Common Column + Combined Result
One-Line Summary
A JOIN is used to combine data from multiple tables based on a common column and retrieve related information efficiently.
Question 8: What Are the Different Types of JOINs in SQL?
JOINs are used in SQL to combine data from multiple tables based on a related column.
In real-world applications, information is usually stored across different tables. JOINs help retrieve related data efficiently by connecting those tables.
Understanding different types of JOINs is one of the most important SQL interview topics.
Why JOIN Types Are Important
JOINs help developers:
Retrieve Related Data
Generate Reports
Build Relationships Between Tables
Improve Database Design
Reduce Data Redundancy
Almost every enterprise application uses JOINs extensively.
Types of JOINs in SQL
There are four major types of JOINs:
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL OUTER JOIN
Sample Tables
Students Table
| StudentID | Name |
|---|---|
| 101 | John |
| 102 | David |
| 103 | Smith |
Courses Table
| StudentID | Course |
|---|---|
| 101 | Java |
| 102 | SQL |
| 104 | Python |
JOIN Types Diagram
JOINS
|
--------------------------------
| | | |
v v v v
INNER LEFT RIGHT FULL
JOIN JOIN JOIN JOIN
1. INNER JOIN
Returns only matching records from both tables.
Syntax
SELECT *
FROM Students s
INNER JOIN Courses c
ON s.StudentID = c.StudentID;
Result
| StudentID | Name | Course |
|---|---|---|
| 101 | John | Java |
| 102 | David | SQL |
StudentID 103 and 104 are excluded because they don't match.
INNER JOIN Diagram
Students Courses
101 John 101 Java
102 David 102 SQL
103 Smith 104 Python
Result
101 John Java
102 David SQL
2. LEFT JOIN
Returns all records from the left table and matching records from the right table.
Syntax
SELECT *
FROM Students s
LEFT JOIN Courses c
ON s.StudentID = c.StudentID;
Result
| StudentID | Name | Course |
|---|---|---|
| 101 | John | Java |
| 102 | David | SQL |
| 103 | Smith | NULL |
All student records appear.
LEFT JOIN Diagram
Students Table
|
v
All Records Included
Matching Course Records Added
103 Smith → NULL
3. RIGHT JOIN
Returns all records from the right table and matching records from the left table.
Syntax
SELECT *
FROM Students s
RIGHT JOIN Courses c
ON s.StudentID = c.StudentID;
Result
| StudentID | Name | Course |
|---|---|---|
| 101 | John | Java |
| 102 | David | SQL |
| 104 | NULL | Python |
All course records appear.
RIGHT JOIN Diagram
Courses Table
|
v
All Records Included
Matching Student Records Added
104 Python → NULL
4. FULL OUTER JOIN
Returns all records from both tables.
Matching records are combined.
Non-matching records show NULL values.
Syntax
SELECT *
FROM Students s
FULL OUTER JOIN Courses c
ON s.StudentID = c.StudentID;
Result
| StudentID | Name | Course |
|---|---|---|
| 101 | John | Java |
| 102 | David | SQL |
| 103 | Smith | NULL |
| 104 | NULL | Python |
FULL JOIN Diagram
Students Table
+
Courses Table
|
v
All Records Included
Matched + Unmatched Records
Real-World Example
Consider an Education Management System.
Students Table
Stores student details.
Courses Table
Stores enrolled courses.
Using JOINs:
INNER JOIN → Students enrolled in courses.
LEFT JOIN → All students including those without courses.
RIGHT JOIN → All courses including unassigned courses.
FULL JOIN → Complete report.
Comparison Table
| JOIN Type | Matching Records | Left Table | Right Table |
|---|---|---|---|
| INNER JOIN | Yes | No | No |
| LEFT JOIN | Yes | Yes | No |
| RIGHT JOIN | Yes | No | Yes |
| FULL JOIN | Yes | Yes | Yes |
Interview Answer
SQL supports four major JOIN types: INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. INNER JOIN returns matching records, LEFT JOIN returns all records from the left table, RIGHT JOIN returns all records from the right table, and FULL JOIN returns all records from both tables.
Common Mistakes
Avoid these answers:
❌ INNER JOIN returns all records.
❌ LEFT JOIN and RIGHT JOIN are the same.
Correct:
✅ INNER JOIN returns matching records only.
✅ LEFT JOIN returns all left table records.
✅ RIGHT JOIN returns all right table records.
✅ FULL JOIN returns all records from both tables.
Interview Tip
Whenever an interviewer asks:
"What are the different types of JOINs?"
Always mention:
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL OUTER JOIN
Also explain what each returns.
Quick Revision
INNER JOIN → Matching Records
LEFT JOIN → All Left Records
RIGHT JOIN → All Right Records
FULL JOIN → All Records
One-Line Summary
SQL JOINs combine data from multiple tables, and the main types are INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN.
Question 9: What Is Normalization in SQL?
Introduction
Normalization is the process of organizing data in a database to reduce data redundancy and improve data integrity.
It involves dividing large tables into smaller related tables and establishing relationships between them using Primary Keys and Foreign Keys.
Normalization helps create efficient, scalable, and maintainable databases.
It is one of the most frequently asked SQL interview questions.
Why Normalization Is Important
Normalization helps:
Eliminate Duplicate Data
Improve Data Consistency
Reduce Storage Space
Simplify Database Maintenance
Improve Database Design
Without normalization, databases may contain redundant and inconsistent data.
Database Without Normalization
Consider the following table:
| StudentID | StudentName | Course |
|---|---|---|
| 101 | John | Java |
| 101 | John | SQL |
| 102 | David | Spring Boot |
Problems:
❌ Duplicate Student Information
❌ Data Redundancy
❌ Difficult Updates
Normalization Diagram
Unnormalized Table
|
v
1NF
|
v
2NF
|
v
3NF
|
v
Optimized Database
First Normal Form (1NF)
Rule:
No Repeating Groups
Atomic Values Only
Before 1NF
| StudentID | Courses |
|---|---|
| 101 | Java, SQL |
After 1NF
| StudentID | Course |
|---|---|
| 101 | Java |
| 101 | SQL |
Each column contains a single value.
Second Normal Form (2NF)
Rule:
Must Be In 1NF
Remove Partial Dependencies
Before 2NF
| StudentID | CourseID | StudentName |
|---|---|---|
| 101 | 1 | John |
StudentName depends only on StudentID.
After 2NF
Students Table
| StudentID | StudentName |
|---|---|
| 101 | John |
Courses Table
| CourseID | StudentID |
|---|---|
| 1 | 101 |
Redundant data is removed.
Third Normal Form (3NF)
Rule:
Must Be In 2NF
Remove Transitive Dependencies
Before 3NF
| StudentID | DepartmentID | DepartmentName |
|---|---|---|
| 101 | 1 | IT |
DepartmentName depends on DepartmentID.
After 3NF
Students Table
| StudentID | DepartmentID |
|---|---|
| 101 | 1 |
Departments Table
| DepartmentID | DepartmentName |
|---|---|
| 1 | IT |
Database becomes more organized.
Normalization Flow Diagram
Student Table
|
v
Remove Duplicate Data
|
v
Create Related Tables
|
v
Use Primary Keys
|
v
Use Foreign Keys
|
v
Normalized Database
Real-World Example
Consider an Education Management System.
Without Normalization:
Student + Course + Faculty
Stored In One Table
Problems:
Duplicate Data
Large Table Size
Slow Queries
With Normalization:
Students Table
Courses Table
Faculty Table
Enrollments Table
Benefits:
Better Performance
Easy Maintenance
Improved Data Integrity
Advantages of Normalization
Reduces Data Redundancy
Duplicate information is removed.
Improves Data Integrity
Data remains accurate and consistent.
Easier Maintenance
Updates become simpler.
Better Database Design
Tables become more structured.
Efficient Storage
Less storage space is required.
Disadvantages of Normalization
More Tables
Database structure becomes more complex.
More JOIN Operations
Queries may require multiple JOINs.
Slightly Complex Queries
Fetching data may take additional effort.
Interview Answer
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity. It divides large tables into smaller related tables using Primary Keys and Foreign Keys. Common normalization forms include 1NF, 2NF, and 3NF.
Common Mistakes
Avoid these answers:
❌ Normalization increases duplicate data.
❌ Normalization means creating more records.
Correct:
✅ Normalization reduces redundancy.
✅ Normalization improves consistency.
✅ Normalization organizes data efficiently.
Interview Tip
Whenever an interviewer asks:
"What is Normalization?"
Always explain:
Data Redundancy
Data Integrity
1NF
2NF
3NF
These are the points recruiters expect.
Quick Revision
1NF → Atomic Values
2NF → Remove Partial Dependency
3NF → Remove Transitive Dependency
One-Line Summary
Normalization is a database design technique that reduces redundancy and improves data integrity by organizing data into related tables.
Question 10: What Is the Difference Between SQL and NoSQL?
Introduction
SQL and NoSQL are two different types of database management systems used to store, manage, and retrieve data.
SQL databases are relational databases that store data in tables with rows and columns, whereas NoSQL databases store data in flexible formats such as documents, key-value pairs, graphs, or wide-column structures.
Understanding the difference between SQL and NoSQL is one of the most frequently asked database interview questions.
Why SQL and NoSQL Are Important
Modern applications generate huge amounts of data.
Organizations choose SQL or NoSQL based on:
Data Structure
Scalability Requirements
Performance Needs
Business Requirements
Examples:
SQL Databases
MySQL
PostgreSQL
Oracle
SQL Server
NoSQL Databases
MongoDB
Cassandra
Redis
CouchDB
SQL vs NoSQL Architecture
DATABASES
|
----------------------------
| |
v v
SQL NoSQL
Relational Non-Relational
Tables Documents
Fixed Schema Flexible Schema
Vertical Scaling Horizontal Scaling
What Is SQL?
SQL (Structured Query Language) databases store data in tables.
Example:
Students Table
| StudentID | Name |
|---|---|
| 101 | John |
| 102 | David |
Data is organized into rows and columns.
SQL databases follow ACID properties:
Atomicity
Consistency
Isolation
Durability
What Is NoSQL?
NoSQL databases store data in flexible formats.
Example:
MongoDB Document
{
"studentId":101,
"name":"John",
"course":"Java"
}
NoSQL databases are designed for handling large-scale and unstructured data.
Key Differences
| Feature | SQL | NoSQL |
|---|---|---|
| Database Type | Relational | Non-Relational |
| Data Format | Tables | Documents, Key-Value, Graph |
| Schema | Fixed | Flexible |
| Scalability | Vertical | Horizontal |
| Query Language | SQL | Database Specific |
| Relationships | Strong | Limited |
| ACID Support | Strong | Varies |
| Performance | Complex Queries | High-Speed Large Data Processing |
SQL vs NoSQL Diagram
SQL
Application
|
v
Relational Database
|
v
Tables
|
v
Structured Data
NoSQL
Application
|
v
NoSQL Database
|
v
Documents
|
v
Flexible Data
Real-World Example
Banking Application
Uses SQL Database
Reason:
High Data Consistency
Transaction Support
Strong Relationships
Examples:
PostgreSQL
Oracle
Social Media Application
Uses NoSQL Database
Reason:
Large User Data
Rapid Scaling
Flexible Structure
Examples:
MongoDB
Cassandra
Advantages of SQL
Strong Data Integrity
Supports ACID transactions.
Structured Data
Well-organized tables.
Powerful Queries
Supports JOINs and complex operations.
Better Relationships
Ideal for relational data.
Advantages of NoSQL
High Scalability
Supports distributed systems.
Flexible Schema
Easy to modify data structure.
High Performance
Handles massive data volumes efficiently.
Cloud Friendly
Widely used in modern applications.
When Should You Use SQL?
Use SQL when:
✔ Data Relationships Are Important
✔ Transactions Are Required
✔ Structured Data Exists
✔ Data Consistency Is Critical
When Should You Use NoSQL?
Use NoSQL when:
✔ Large Data Volumes Exist
✔ Rapid Scaling Is Required
✔ Flexible Data Structure Is Needed
✔ Real-Time Applications Are Used
Interview Answer
SQL databases are relational databases that store data in tables with predefined schemas and support ACID transactions. NoSQL databases are non-relational databases that store data in flexible formats such as documents and key-value pairs, providing better scalability and flexibility for large-scale applications.
Common Mistakes
Avoid these answers:
❌ SQL is better than NoSQL.
❌ NoSQL replaces SQL.
Correct:
✅ SQL and NoSQL serve different purposes.
✅ The choice depends on application requirements.
Interview Tip
Whenever an interviewer asks:
"What is the difference between SQL and NoSQL?"
Always mention:
Relational vs Non-Relational
Fixed vs Flexible Schema
Vertical vs Horizontal Scaling
Structured vs Unstructured Data
These are the points recruiters expect.
Quick Revision
SQL
✔ Relational
✔ Tables
✔ Fixed Schema
✔ ACID
✔ Strong Relationships
NoSQL
✔ Non-Relational
✔ Documents
✔ Flexible Schema
✔ Scalable
✔ High Performance
One-Line Summary
SQL databases use structured tables with fixed schemas, while NoSQL databases use flexible data models designed for scalability and large-scale applications.
📚 Related Articles
Top 25 Java Interview Questions and Answers for Freshers (2026)
top-10-spring-boot-interview-questions-and-answers-2026
Top 10 React JS Interview Questions and Answers
introduction-to-java-complete-beginners-guide
Java OOP(Object Oriented Programming) Concepts
Exception Handling in Java: Complete Beginner's Guide
Click below to download the complete PDF.
📥 Download Complete PDF
Comments
Post a Comment