Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views50 pages

Complete DBMS Interview Guide - Concepts, SQL, PL

Uploaded by

Cdr Rjt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views50 pages

Complete DBMS Interview Guide - Concepts, SQL, PL

Uploaded by

Cdr Rjt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

Complete DBMS Interview Guide: Concepts, SQL,

PL/SQL & More


1. Database Fundamentals 🟡 MODERATE IMPORTANCE

1.1 Data vs Information


Definition for Interview: Data represents raw, unprocessed facts and figures, while information
is processed data that provides meaningful insights for decision-making in a specific context.
Real-life Scenario: Consider Amazon's sales data:
Raw Data: "CustomerID: 12345, Product: Laptop, Price: $899, Date: 2024-01-15"
Information: "Customer 12345 purchased electronics worth $899 on January 15th,
contributing to a 20% increase in laptop sales this month"
The transformation happens when we analyze, filter, and contextualize data to extract business
value.

1.2 Database vs File System 🔴 HIGH IMPORTANCE


Definition for Interview: A Database Management System (DBMS) is a software that provides
systematic storage, retrieval, and management of data with built-in integrity, security, and
concurrent access controls, unlike traditional file systems.
File System Problems:
1. Data Redundancy: Same customer information stored in multiple files
2. Data Inconsistency: Updating one file but forgetting others
3. Data Isolation: Related data scattered across different files
4. Atomicity Problems: No guarantee that related operations complete together
5. Concurrent Access Issues: Multiple users can't safely modify same data
DBMS Advantages:
Centralized data management
ACID properties ensure consistency
Built-in security mechanisms
Efficient query processing
Concurrent access with locking

1.3 Three-Schema Architecture 🟡 MODERATE IMPORTANCE


Definition for Interview: A database architecture with three abstraction levels that provide data
independence and security through layered access control.
Levels:
1. Physical Level (Internal Schema): How data is physically stored on disk
2. Logical Level (Conceptual Schema): Overall database structure for entire organization
3. View Level (External Schema): User-specific views showing relevant data portions
Real-life Example: Banking system where:
Customers see account balances and transaction history (View level)
Bank managers see comprehensive customer analytics (Logical level)
Database administrators manage disk storage and indexing (Physical level)

1.4 OLTP vs OLAP 🟡 MODERATE IMPORTANCE


Aspect OLTP (Online Transaction Processing) OLAP (Online Analytical Processing)

Purpose Day-to-day operational transactions Historical data analysis and reporting

Response Time Milliseconds Minutes to hours acceptable

Query Type Simple, repetitive queries Complex analytical queries

Data Volume Current, small datasets Historical, large datasets

Example ATM transactions, e-commerce purchases Sales trend analysis, business intelligence

2. Entity-Relationship (ER) Model 🔴 HIGH IMPORTANCE

2.1 Entity
Definition for Interview: An entity is a distinguishable real-world object that can be uniquely
identified and about which we store information in the database.
Synthetic Example - E-commerce System:

Customer Entity:
- CustomerID: C001 (Primary identifier)
- Name: "John Smith"
- Email: "[email protected]"
- Phone: "+1-555-0123"
- Address: "123 Main St, City, State"
2.2 Attributes Types 🟡 MODERATE IMPORTANCE
Definition for Interview: Attributes are properties or characteristics that describe an entity.
Types:
1. Simple vs Composite:
Simple: Age (cannot be subdivided)
Composite: Address (Street, City, State, ZIP)
2. Single-valued vs Multi-valued:
Single: DateOfBirth
Multi-valued: PhoneNumbers (can have multiple)
3. Stored vs Derived:
Stored: DateOfBirth
Derived: Age (calculated from DateOfBirth)

2.3 Relationships 🔴 HIGH IMPORTANCE


Definition for Interview: A relationship represents an association between two or more entities.
Cardinality Types:
One-to-One (1:1): Person-Passport
One-to-Many (1:M): Department-Employee
Many-to-Many (M:N): Student-Course

2.4 Weak Entity 🟡 MODERATE IMPORTANCE


Definition for Interview: A weak entity cannot be uniquely identified by its own attributes and
depends on a strong entity for identification.
Example:

Strong Entity: Employee (EmpID, Name, Department)


Weak Entity: Dependent (DependentName, Age, Relationship)
Identification: EmpID + DependentName (Composite key)

3. Keys in DBMS 🔴 HIGH IMPORTANCE

3.1 Key Types and Definitions


Key Type Definition Example Use Case

Any set of attributes that uniquely {StudentID, Name,


Super Key Basis for all other keys
identifies a tuple Email}
Key Type Definition Example Use Case

Candidate Minimal super key (no redundant


{StudentID}, {Email} Potential primary keys
Key attributes)

Selected candidate key for main


Primary Key StudentID Main entity identifier
identification

Alternate Email (if StudentID is Alternative unique


Non-selected candidate keys
Key primary) identifiers

References primary key of another DepartmentID in Maintains referential


Foreign Key
table Employee table integrity

Composite Identifies relationship


Key made of multiple attributes {StudentID, CourseID}
Key records

3.2 Key Differences and Interview Points


Primary vs Unique: Primary key cannot be NULL, table can have only one; Unique key
allows one NULL, table can have multiple
Primary vs Foreign: Primary identifies records in current table; Foreign references records in
other tables
Candidate vs Super: Candidate is minimal (no extra attributes); Super can have redundant
attributes

4. Functional Dependencies 🔴 HIGH IMPORTANCE

4.1 Definition for Interview


Functional Dependency (FD): A constraint where one attribute uniquely determines another. If X
→ Y, then whenever two tuples have same X value, they must have same Y value.

4.2 Armstrong's Axioms 🟡 MODERATE IMPORTANCE


1. Reflexivity: If Y ⊆ X, then X → Y
2. Augmentation: If X → Y, then XZ → YZ
3. Transitivity: If X → Y and Y → Z, then X → Z

4.3 Synthetic Example

Employee Table:
EmpID | Name | DeptID | DeptName | Salary
101 | Alice | D001 | IT | 75000
102 | Bob | D002 | HR | 65000
103 | Carol | D001 | IT | 80000

Functional Dependencies:
- EmpID → Name, DeptID, Salary
- DeptID → DeptName
- EmpID → DeptName (transitive through DeptID)

5. Normalization 🔴 HIGH IMPORTANCE

5.1 Purpose and Definition


Definition for Interview: Normalization is the process of organizing database tables to minimize
data redundancy and prevent update, insertion, and deletion anomalies while preserving data
dependencies.

5.2 Normal Forms with Examples

First Normal Form (1NF) 🔴 HIGH IMPORTANCE


Definition: Each cell contains atomic values, no repeating groups.
Before 1NF (Violates atomicity):

StudentCourse Table:
StudentID | Name | Courses
S001 | Alice | CS101, CS102, CS103

After 1NF:

CREATE TABLE StudentCourse (


StudentID VARCHAR(10),
Name VARCHAR(50),
CourseID VARCHAR(10)
);

INSERT INTO StudentCourse VALUES


('S001', 'Alice', 'CS101'),
('S001', 'Alice', 'CS102'),
('S001', 'Alice', 'CS103');

Second Normal Form (2NF) 🔴 HIGH IMPORTANCE


Definition: 1NF + No partial dependencies (non-key attributes fully dependent on entire primary
key).
Before 2NF (Has partial dependency):

Enrollment Table:
StudentID | CourseID | StudentName | CourseName | Grade
S001 | CS101 | Alice | Database | A

Problem: StudentName depends only on StudentID, not full key {StudentID, CourseID}
After 2NF:

-- Student table
CREATE TABLE Student (
StudentID VARCHAR(10) PRIMARY KEY,
StudentName VARCHAR(50)
);

-- Course table
CREATE TABLE Course (
CourseID VARCHAR(10) PRIMARY KEY,
CourseName VARCHAR(50)
);

-- Enrollment table
CREATE TABLE Enrollment (
StudentID VARCHAR(10),
CourseID VARCHAR(10),
Grade CHAR(2),
PRIMARY KEY (StudentID, CourseID),
FOREIGN KEY (StudentID) REFERENCES Student(StudentID),
FOREIGN KEY (CourseID) REFERENCES Course(CourseID)
);

Third Normal Form (3NF) 🔴 HIGH IMPORTANCE


Definition: 2NF + No transitive dependencies (non-key attributes depend only on primary key).
Before 3NF (Has transitive dependency):

Employee Table:
EmpID | Name | DeptID | DeptName | DeptLocation
101 | Alice | D001 | IT | Building A

Problem: DeptName and DeptLocation depend on DeptID, not directly on EmpID


After 3NF:

-- Employee table
CREATE TABLE Employee (
EmpID INT PRIMARY KEY,
Name VARCHAR(50),
DeptID VARCHAR(10),
FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
);

-- Department table
CREATE TABLE Department (
DeptID VARCHAR(10) PRIMARY KEY,
DeptName VARCHAR(50),
DeptLocation VARCHAR(50)
);

Boyce-Codd Normal Form (BCNF) 🟡 MODERATE IMPORTANCE


Definition: 3NF + For every non-trivial FD X → Y, X must be a super key.
Example requiring BCNF:

CourseInstructor Table:
CourseID | Instructor | Room
CS101 | Dr. Smith | R101
CS102 | Dr. Jones | R102
CS101 | Dr. Brown | R103

If constraint: Instructor → Room (each instructor has fixed room)


This violates BCNF because Instructor is not a super key

6. B-Trees and B+ Trees 🔴 HIGH IMPORTANCE

6.1 What are B-Trees?


Definition for Interview: B-Trees are self-balancing, multi-way search trees designed for
systems that read and write large blocks of data, commonly used in database indexing for
efficient data retrieval.
Why B-Trees are Needed:
Database files are stored on disk (slower than memory)
Disk access is expensive - minimize number of disk reads
B-Trees reduce tree height, minimizing disk accesses
Each node can store multiple keys (higher branching factor)

6.2 B-Tree Properties 🔴 HIGH IMPORTANCE


All leaves at same level (perfectly balanced)
Each node has minimum m/2 - 1 keys and maximum m-1 keys (where m is order)
Internal nodes store keys and data
Keys within node are sorted
Time complexity: O(log n) for search, insert, delete
6.3 B+ Tree Improvements 🔴 HIGH IMPORTANCE
Definition: B+ Tree is an optimized version of B-Tree specifically designed for database
systems.
Key Differences:

Aspect B-Tree B+ Tree

Data Storage Keys and data in all nodes Data only in leaf nodes

Internal Nodes Store keys and data Store only keys (more keys per node)

Leaf Connections Not connected Linked as sequential list

Range Queries Requires tree traversal Efficient via leaf-level scanning

Disk Utilization Lower Higher (more keys per internal node)

6.4 Real-life Database Example


Scenario: Employee database with 1 million records, searching by EmployeeID.
Without indexing: Must scan all 1 million records = O(n)
With B+ Tree index: log₁₀₀(1,000,000) ≈ 3 disk accesses = O(log n)

-- Creating index uses B+ Tree internally


CREATE INDEX idx_employee_id ON Employee(EmployeeID);

-- This query now uses index for fast lookup


SELECT * FROM Employee WHERE EmployeeID = 12345;

7. Indexing Types 🟡 MODERATE IMPORTANCE

7.1 Primary Indexing


Definition: Index created on primary key of a sorted file.
Sparse index (one entry per block)
Faster access for ordered data
Used when data is physically sorted by primary key

7.2 Secondary Indexing


Definition: Index on non-primary key attributes.
Dense index (one entry per record)
Enables fast access on non-sorted attributes
Multiple secondary indexes possible per table
7.3 Clustered vs Non-Clustered
Clustered: Data rows stored in order of index key (one per table)
Non-Clustered: Index separate from data storage (multiple per table)

8. Comprehensive SQL Guide 🔴 HIGH IMPORTANCE

8.1 SQL Overview and Categories


Definition for Interview: SQL (Structured Query Language) is a standardized, declarative
language for managing relational databases, divided into five sublanguages for different
operations.
SQL Categories:
DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE
DML (Data Manipulation Language): INSERT, UPDATE, DELETE
DQL (Data Query Language): SELECT
TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT
DCL (Data Control Language): GRANT, REVOKE

8.2 DDL Commands - Detailed Examples

CREATE Command 🔴 HIGH IMPORTANCE


Definition: Creates new database objects like tables, indexes, or databases.

-- Creating a database
CREATE DATABASE ECommerceDB;
USE ECommerceDB;

-- Creating a comprehensive table with constraints


CREATE TABLE Customers (
CustomerID INT PRIMARY KEY AUTO_INCREMENT,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL,
Phone CHAR(10),
DateOfBirth DATE,
RegistrationDate DATETIME DEFAULT CURRENT_TIMESTAMP,
Status ENUM('Active', 'Inactive', 'Suspended') DEFAULT 'Active',
CreditLimit DECIMAL(10,2) CHECK (CreditLimit >= 0)
);

Interview Scenario: "Create a table for an online shopping system that tracks customer
information with appropriate constraints."
ALTER Command Variations 🔴 HIGH IMPORTANCE
Adding Column:

-- Scenario: Need to track customer loyalty points


ALTER TABLE Customers
ADD COLUMN LoyaltyPoints INT DEFAULT 0;

-- Add column with constraint


ALTER TABLE Customers
ADD COLUMN Address TEXT,
ADD CONSTRAINT chk_phone CHECK (Phone REGEXP '^[0-9]{10}$');

Modifying Column:

-- Scenario: Need to increase email field size


ALTER TABLE Customers
MODIFY COLUMN Email VARCHAR(200) NOT NULL;

-- Change data type


ALTER TABLE Customers
MODIFY COLUMN CreditLimit DECIMAL(12,2);

Renaming Column:

-- Scenario: Better column naming


ALTER TABLE Customers
RENAME COLUMN Phone TO MobilePhone;

Dropping Column:

-- Scenario: Column no longer needed


ALTER TABLE Customers
DROP COLUMN DateOfBirth;

Renaming Table:

-- Scenario: Better table naming convention


RENAME TABLE Customers TO Customer_Master;

TRUNCATE vs DROP 🟡 MODERATE IMPORTANCE


TRUNCATE:

-- Removes all rows, preserves structure, resets auto-increment


TRUNCATE TABLE Customers;
Faster than DELETE (deallocates pages)
Cannot be rolled back in most systems
Resets identity counters
Cannot use WHERE clause
DROP:

-- Removes entire table structure and data


DROP TABLE IF EXISTS Customers;

Permanently removes table definition


Removes all indexes, triggers, constraints
Cannot be undone without backup

8.3 DML Commands - Comprehensive Examples

INSERT Command Variations 🔴 HIGH IMPORTANCE


Basic Insert:

-- Single row insertion


INSERT INTO Customers (FirstName, LastName, Email, Phone)
VALUES ('John', 'Doe', '[email protected]', '5551234567');

Multiple Row Insert:

-- Batch insertion for efficiency


INSERT INTO Customers (FirstName, LastName, Email, Phone) VALUES
('Alice', 'Smith', '[email protected]', '5559876543'),
('Bob', 'Johnson', '[email protected]', '5556789012'),
('Carol', 'Williams', '[email protected]', '5553456789');

Insert with Subquery:

-- Scenario: Copying data from another table


INSERT INTO ArchivedCustomers (CustomerID, FirstName, LastName, Email)
SELECT CustomerID, FirstName, LastName, Email
FROM Customers
WHERE Status = 'Inactive';
UPDATE Command Examples 🔴 HIGH IMPORTANCE
Simple Update:

-- Scenario: Customer changed email address


UPDATE Customers
SET Email = '[email protected]'
WHERE CustomerID = 1;

Multiple Column Update:

-- Scenario: Updating customer status and loyalty points


UPDATE Customers
SET Status = 'Active',
LoyaltyPoints = LoyaltyPoints + 100,
LastModified = CURRENT_TIMESTAMP
WHERE CustomerID = 1;

Conditional Update with CASE:

-- Scenario: Bulk status update based on credit limit


UPDATE Customers
SET Status = CASE
WHEN CreditLimit >= 10000 THEN 'Premium'
WHEN CreditLimit >= 5000 THEN 'Gold'
ELSE 'Standard'
END;

DELETE Command Examples 🟡 MODERATE IMPORTANCE


Conditional Delete:

-- Scenario: Remove inactive customers older than 2 years


DELETE FROM Customers
WHERE Status = 'Inactive'
AND RegistrationDate < DATE_SUB(CURRENT_DATE, INTERVAL 2 YEAR);

Delete with Subquery:

-- Scenario: Delete customers with no orders


DELETE FROM Customers
WHERE CustomerID NOT IN (
SELECT DISTINCT CustomerID FROM Orders
);
8.4 Advanced SELECT Queries 🔴 HIGH IMPORTANCE

WHERE Clause with Complex Conditions

-- Scenario: Find active customers with high credit limits in specific date range
SELECT CustomerID, FirstName, LastName, Email, CreditLimit
FROM Customers
WHERE Status = 'Active'
AND CreditLimit BETWEEN 5000 AND 15000
AND RegistrationDate >= '2023-01-01'
AND (Email LIKE '%@gmail.com' OR Email LIKE '%@yahoo.com')
ORDER BY CreditLimit DESC;

Pattern Matching with LIKE 🟡 MODERATE IMPORTANCE


Definition: LIKE operator uses wildcards for pattern matching.
Wildcards:
%: Matches zero or more characters
_: Matches exactly one character

-- Scenario: Find customers with specific name patterns


-- Names starting with 'J'
SELECT * FROM Customers WHERE FirstName LIKE 'J%';

-- Names ending with 'son'


SELECT * FROM Customers WHERE LastName LIKE '%son';

-- Email addresses with exactly 3 characters before @


SELECT * FROM Customers WHERE Email LIKE '___@%';

-- Names with 'a' as second character


SELECT * FROM Customers WHERE FirstName LIKE '_a%';

-- Complex pattern: Gmail addresses starting with 'john'


SELECT * FROM Customers WHERE Email LIKE 'john%@gmail.com';

What each query does:


First query finds all customers whose first name starts with letter 'J'
Second query finds customers with last names ending in 'son' (Johnson, Wilson, etc.)
Third query finds emails with exactly 3-character usernames
Fourth query finds first names with 'a' as the second letter
Fifth query finds Gmail users whose email starts with 'john'
8.5 Aggregate Functions 🔴 HIGH IMPORTANCE
Individual Aggregate Function Examples:
COUNT Function:

-- Scenario: Count total active customers


SELECT COUNT(*) AS TotalActiveCustomers
FROM Customers
WHERE Status = 'Active';

-- Count customers with email addresses (non-NULL emails)


SELECT COUNT(Email) AS CustomersWithEmail
FROM Customers;

-- Count unique domains in email addresses


SELECT COUNT(DISTINCT SUBSTRING_INDEX(Email, '@', -1)) AS UniqueDomains
FROM Customers;

SUM Function:

-- Scenario: Calculate total credit limit across all customers


SELECT SUM(CreditLimit) AS TotalCreditExtended
FROM Customers
WHERE Status = 'Active';

AVG Function:

-- Scenario: Find average loyalty points


SELECT AVG(LoyaltyPoints) AS AverageLoyaltyPoints
FROM Customers
WHERE Status = 'Active';

MIN and MAX Functions:

-- Scenario: Find credit limit range


SELECT
MIN(CreditLimit) AS LowestCreditLimit,
MAX(CreditLimit) AS HighestCreditLimit
FROM Customers;

GROUP BY with Aggregates:

-- Scenario: Customer statistics by status


SELECT
Status,
COUNT(*) AS CustomerCount,
AVG(CreditLimit) AS AvgCreditLimit,
SUM(LoyaltyPoints) AS TotalPoints
FROM Customers
GROUP BY Status
ORDER BY CustomerCount DESC;

8.6 JOIN Operations 🔴 HIGH IMPORTANCE


Let's create additional tables for JOIN examples:

-- Orders table
CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
TotalAmount DECIMAL(10,2),
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
);

-- OrderDetails table
CREATE TABLE OrderDetails (
OrderDetailID INT PRIMARY KEY,
OrderID INT,
ProductName VARCHAR(100),
Quantity INT,
UnitPrice DECIMAL(8,2),
FOREIGN KEY (OrderID) REFERENCES Orders(OrderID)
);

INNER JOIN 🔴 HIGH IMPORTANCE

-- Scenario: Get customer names with their order information


SELECT
c.FirstName,
c.LastName,
o.OrderID,
o.OrderDate,
o.TotalAmount
FROM Customers c
INNER JOIN Orders o ON c.CustomerID = o.CustomerID
WHERE o.OrderDate >= '2024-01-01'
ORDER BY o.OrderDate DESC;

What it does: Returns only customers who have placed orders, along with their order details.

LEFT JOIN 🔴 HIGH IMPORTANCE

-- Scenario: Get all customers and their orders (including customers with no orders)
SELECT
c.CustomerID,
c.FirstName,
c.LastName,
COUNT(o.OrderID) AS TotalOrders,
COALESCE(SUM(o.TotalAmount), 0) AS TotalSpent
FROM Customers c
LEFT JOIN Orders o ON c.CustomerID = o.CustomerID
GROUP BY c.CustomerID, c.FirstName, c.LastName
ORDER BY TotalSpent DESC;

What it does: Returns all customers, showing 0 orders and $0 spent for customers who haven't
placed orders.

RIGHT JOIN 🟡 MODERATE IMPORTANCE

-- Scenario: Show all orders and customer info (including orphaned orders)
SELECT
o.OrderID,
o.OrderDate,
c.FirstName,
c.LastName,
c.Email
FROM Customers c
RIGHT JOIN Orders o ON c.CustomerID = o.CustomerID;

What it does: Returns all orders, even those without valid customer records (data integrity
issues).

FULL OUTER JOIN 🟡 MODERATE IMPORTANCE

-- Scenario: Complete view of customers and orders relationship


SELECT
c.CustomerID,
c.FirstName,
o.OrderID,
o.OrderDate
FROM Customers c
FULL OUTER JOIN Orders o ON c.CustomerID = o.CustomerID;

What it does: Returns all customers and all orders, whether they match or not.

8.7 Set Operations 🟡 MODERATE IMPORTANCE


Let's create a Prospects table for examples:

CREATE TABLE Prospects (


ProspectID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Email VARCHAR(100)
);

UNION Operation:
-- Scenario: Combine customer and prospect email lists for marketing
SELECT Email, 'Customer' AS Source FROM Customers
UNION
SELECT Email, 'Prospect' AS Source FROM Prospects;

What it does: Combines emails from both tables, removes duplicates, labels source.
UNION ALL Operation:

-- Scenario: Get complete mailing list including duplicates


SELECT Email FROM Customers
UNION ALL
SELECT Email FROM Prospects;

What it does: Combines all emails, keeps duplicates for frequency analysis.
INTERSECT Operation:

-- Scenario: Find emails that exist in both customers and prospects


SELECT Email FROM Customers
INTERSECT
SELECT Email FROM Prospects;

What it does: Returns only emails present in both tables.


EXCEPT/MINUS Operation:

-- Scenario: Find prospect emails not yet converted to customers


SELECT Email FROM Prospects
EXCEPT
SELECT Email FROM Customers;

What it does: Returns prospect emails that don't exist in customer table.

8.8 Advanced Query Features 🟡 MODERATE IMPORTANCE

LIMIT and OFFSET 🟡 MODERATE IMPORTANCE


Definition: LIMIT restricts number of rows returned; OFFSET specifies number of rows to skip.
Pagination Scenario: E-commerce site showing 10 customers per page.

-- Page 1 (first 10 customers)


SELECT CustomerID, FirstName, LastName, Email
FROM Customers
ORDER BY CustomerID
LIMIT 10 OFFSET 0;

-- Page 2 (customers 11-20)


SELECT CustomerID, FirstName, LastName, Email
FROM Customers
ORDER BY CustomerID
LIMIT 10 OFFSET 10;

-- Page 3 (customers 21-30)


SELECT CustomerID, FirstName, LastName, Email
FROM Customers
ORDER BY CustomerID
LIMIT 10 OFFSET 20;

Detailed Explanation:
LIMIT 10: Returns maximum 10 rows
OFFSET 10: Skips first 10 rows before returning results
ORDER BY CustomerID: Ensures consistent pagination order
Page calculation: Page N shows rows from OFFSET = (N-1) × PageSize
Top N Queries:

-- Scenario: Top 5 customers by credit limit


SELECT CustomerID, FirstName, LastName, CreditLimit
FROM Customers
ORDER BY CreditLimit DESC
LIMIT 5;

-- Scenario: 10 most recent customers


SELECT CustomerID, FirstName, LastName, RegistrationDate
FROM Customers
ORDER BY RegistrationDate DESC
LIMIT 10;

Window Functions 🟡 MODERATE IMPORTANCE

-- Scenario: Rank customers by credit limit within each status group


SELECT
CustomerID,
FirstName,
LastName,
Status,
CreditLimit,
RANK() OVER (PARTITION BY Status ORDER BY CreditLimit DESC) as CreditRank,
ROW_NUMBER() OVER (ORDER BY CreditLimit DESC) as OverallRank
FROM Customers;
9. SQL Constraints 🔴 HIGH IMPORTANCE

9.1 Constraint Types and Definitions

PRIMARY KEY Constraint 🔴 HIGH IMPORTANCE


Definition: Uniquely identifies each row in a table; cannot contain NULL values; only one per
table.

-- During table creation


CREATE TABLE Products (
ProductID INT PRIMARY KEY,
ProductName VARCHAR(100) NOT NULL,
Price DECIMAL(8,2)
);

-- Adding after table creation


ALTER TABLE Products
ADD CONSTRAINT pk_products PRIMARY KEY (ProductID);

-- Composite primary key


CREATE TABLE OrderDetails (
OrderID INT,
ProductID INT,
Quantity INT,
PRIMARY KEY (OrderID, ProductID)
);

Use Cases:
Entity identification (CustomerID for customers)
Foreign key references (other tables reference this key)
Indexing optimization (automatically creates clustered index)

FOREIGN KEY Constraint 🔴 HIGH IMPORTANCE


Definition: References primary key of another table to maintain referential integrity.

-- Creating table with foreign key


CREATE TABLE Orders (
OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE,
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
ON DELETE CASCADE
ON UPDATE RESTRICT
);

-- Adding foreign key to existing table


ALTER TABLE Orders
ADD CONSTRAINT fk_orders_customer
FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID);

Referential Actions:
CASCADE: Delete/update child records when parent changes
RESTRICT: Prevent parent deletion/update if child records exist
SET NULL: Set child foreign key to NULL when parent deleted

UNIQUE Constraint 🟡 MODERATE IMPORTANCE


Definition: Ensures column values are unique; allows one NULL value per column.

-- Single column unique


CREATE TABLE Users (
UserID INT PRIMARY KEY,
Username VARCHAR(50) UNIQUE,
Email VARCHAR(100) UNIQUE
);

-- Multiple column unique (combination must be unique)


ALTER TABLE Users
ADD CONSTRAINT uk_user_email UNIQUE (Username, Email);

Difference from PRIMARY KEY:


UNIQUE allows NULL values
Table can have multiple UNIQUE constraints
UNIQUE doesn't automatically become clustered index

NOT NULL Constraint 🟡 MODERATE IMPORTANCE


Definition: Prevents NULL values in specified columns.

-- During table creation


CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
Email VARCHAR(100) NOT NULL,
Salary DECIMAL(10,2) NOT NULL
);

-- Adding to existing column


ALTER TABLE Employees
MODIFY COLUMN PhoneNumber VARCHAR(15) NOT NULL;
CHECK Constraint 🟡 MODERATE IMPORTANCE
Definition: Validates data against specified conditions before insertion/update.

-- Age validation
CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
Name VARCHAR(100),
Age INT CHECK (Age >= 18 AND Age <= 65),
Salary DECIMAL(10,2) CHECK (Salary > 0),
Status VARCHAR(20) CHECK (Status IN ('Active', 'Inactive', 'Terminated')),
Email VARCHAR(100) CHECK (Email LIKE '%@%.%')
);

-- Adding check constraint


ALTER TABLE Employees
ADD CONSTRAINT chk_salary_range CHECK (Salary BETWEEN 30000 AND 200000);

Common CHECK Examples:

-- Date range validation


CHECK (StartDate <= EndDate)

-- Positive values only


CHECK (Quantity > 0)

-- Gender validation
CHECK (Gender IN ('M', 'F', 'Other'))

-- Email format validation


CHECK (Email REGEXP '^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$')

DEFAULT Constraint 🟡 MODERATE IMPORTANCE


Definition: Provides default value when no value specified during insertion.

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,
CustomerID INT,
OrderDate DATE DEFAULT CURRENT_DATE,
Status VARCHAR(20) DEFAULT 'Pending',
Priority INT DEFAULT 1,
CreatedTimestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Adding default to existing column


ALTER TABLE Orders
ALTER COLUMN Status SET DEFAULT 'Processing';
10. Transaction Management 🔴 HIGH IMPORTANCE

10.1 ACID Properties 🔴 HIGH IMPORTANCE

Atomicity
Definition: Transaction executes completely or not at all - no partial execution.
Real-life Example: Bank transfer between accounts

BEGIN TRANSACTION;

-- Debit from source account


UPDATE Accounts SET Balance = Balance - 1000 WHERE AccountID = 'A001';

-- Credit to destination account


UPDATE Accounts SET Balance = Balance + 1000 WHERE AccountID = 'A002';

-- If any operation fails, entire transaction rolls back


COMMIT; -- Only if both operations succeed

Consistency
Definition: Database moves from one valid state to another valid state.
Example: After bank transfer, total money in system remains same

-- Before transaction: Account A = $5000, Account B = $3000, Total = $8000


-- After transaction: Account A = $4000, Account B = $4000, Total = $8000

Isolation
Definition: Concurrent transactions don't interfere with each other.
Example: Two people booking last airline seat

-- Transaction T1 and T2 both check seat availability


-- Only one can successfully book - isolation prevents double booking

Durability
Definition: Committed changes persist even after system failure.
Example: ATM withdrawal recorded permanently

-- After COMMIT, even if ATM crashes, transaction is permanent


10.2 Transaction Commands 🟡 MODERATE IMPORTANCE

-- Starting transaction
BEGIN TRANSACTION;
-- or START TRANSACTION;

-- Making changes permanent


COMMIT;

-- Undoing changes
ROLLBACK;

-- Creating checkpoint for partial rollback


SAVEPOINT sp1;

-- Rolling back to savepoint


ROLLBACK TO SAVEPOINT sp1;

10.3 Concurrency Control 🟡 MODERATE IMPORTANCE

Two-Phase Locking (2PL)


Growing Phase: Acquire locks, cannot release
Shrinking Phase: Release locks, cannot acquire

-- Example of locking in transaction


BEGIN TRANSACTION;
SELECT * FROM Accounts WHERE AccountID = 'A001' FOR UPDATE; -- Acquire lock
UPDATE Accounts SET Balance = Balance - 100 WHERE AccountID = 'A001';
COMMIT; -- Release all locks

11. PL/SQL Comprehensive Guide 🟡 MODERATE IMPORTANCE

11.1 PL/SQL Overview


Definition for Interview: PL/SQL (Procedural Language extension to SQL) is Oracle's block-
structured programming language that combines SQL with procedural constructs like loops,
conditions, and exception handling.

11.2 PL/SQL Block Structure 🟡 MODERATE IMPORTANCE

DECLARE
-- Variable declarations
v_customer_name VARCHAR2(100);
v_order_total NUMBER := 0;
BEGIN
-- Executable statements
SELECT FirstName || ' ' || LastName INTO v_customer_name
FROM Customers WHERE CustomerID = 101;
DBMS_OUTPUT.PUT_LINE('Customer: ' || v_customer_name);
EXCEPTION
-- Exception handling
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/

11.3 Stored Procedures 🟡 MODERATE IMPORTANCE

Creating Procedures
Definition: Stored procedures are reusable PL/SQL blocks that can accept parameters and
perform database operations.

-- Simple procedure without parameters


CREATE OR REPLACE PROCEDURE display_customer_count
AS
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count FROM Customers;
DBMS_OUTPUT.PUT_LINE('Total customers: ' || v_count);
END;
/

-- Procedure with IN parameters


CREATE OR REPLACE PROCEDURE get_customer_info(
p_customer_id IN NUMBER
)
AS
v_name VARCHAR2(100);
v_email VARCHAR2(100);
BEGIN
SELECT FirstName || ' ' || LastName, Email
INTO v_name, v_email
FROM Customers
WHERE CustomerID = p_customer_id;

DBMS_OUTPUT.PUT_LINE('Name: ' || v_name);


DBMS_OUTPUT.PUT_LINE('Email: ' || v_email);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found');
END;
/

-- Procedure with OUT parameters


CREATE OR REPLACE PROCEDURE calculate_order_total(
p_customer_id IN NUMBER,
p_total_orders OUT NUMBER,
p_total_amount OUT NUMBER
)
AS
BEGIN
SELECT COUNT(*), NVL(SUM(TotalAmount), 0)
INTO p_total_orders, p_total_amount
FROM Orders
WHERE CustomerID = p_customer_id;
END;
/

-- Calling procedures
BEGIN
display_customer_count;
get_customer_info(101);

DECLARE
v_order_count NUMBER;
v_total_amt NUMBER;
BEGIN
calculate_order_total(101, v_order_count, v_total_amt);
DBMS_OUTPUT.PUT_LINE('Orders: ' || v_order_count ||
', Total: $' || v_total_amt);
END;
END;
/

11.4 Functions 🟡 MODERATE IMPORTANCE


Definition: Functions are similar to procedures but must return a value and can be used in SQL
expressions.

-- Function to calculate customer discount


CREATE OR REPLACE FUNCTION get_customer_discount(
p_customer_id IN NUMBER
)
RETURN NUMBER
AS
v_total_orders NUMBER;
v_discount NUMBER := 0;
BEGIN
SELECT COUNT(*) INTO v_total_orders
FROM Orders
WHERE CustomerID = p_customer_id;

-- Discount based on order count


IF v_total_orders > 100 THEN
v_discount := 15; -- 15% for VIP customers
ELSIF v_total_orders > 50 THEN
v_discount := 10; -- 10% for frequent customers
ELSIF v_total_orders > 10 THEN
v_discount := 5; -- 5% for regular customers
END IF;
RETURN v_discount;
EXCEPTION
WHEN OTHERS THEN
RETURN 0;
END;
/

-- Using function in SQL query


SELECT
CustomerID,
FirstName,
LastName,
get_customer_discount(CustomerID) AS DiscountPercent
FROM Customers
WHERE Status = 'Active';

11.5 Cursors 🟡 MODERATE IMPORTANCE

Definition for Interview:


Cursors are pointers to SQL result sets that allow row-by-row processing of query results in
PL/SQL.

Implicit Cursors 🟡 MODERATE IMPORTANCE


Definition: Automatically created by Oracle for DML operations.

DECLARE
v_rows_updated NUMBER;
BEGIN
UPDATE Customers SET Status = 'Inactive'
WHERE LastOrderDate < ADD_MONTHS(SYSDATE, -12);

-- Check implicit cursor attributes


IF SQL%FOUND THEN
v_rows_updated := SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(v_rows_updated || ' customers marked inactive');
ELSE
DBMS_OUTPUT.PUT_LINE('No customers to update');
END IF;
END;
/

Implicit Cursor Attributes:


SQL%FOUND: TRUE if last SQL statement affected rows
SQL%NOTFOUND: TRUE if last SQL statement affected no rows
SQL%ROWCOUNT: Number of rows affected by last SQL statement
SQL%ISOPEN: Always FALSE (implicitly closed)
Explicit Cursors 🟡 MODERATE IMPORTANCE
Definition: Programmer-defined cursors for SELECT statements returning multiple rows.

DECLARE
-- Cursor declaration
CURSOR c_customers IS
SELECT CustomerID, FirstName, LastName, CreditLimit
FROM Customers
WHERE Status = 'Active'
ORDER BY CreditLimit DESC;

-- Variable to hold cursor data


v_customer c_customers%ROWTYPE;
BEGIN
-- Open cursor
OPEN c_customers;

LOOP
-- Fetch data
FETCH c_customers INTO v_customer;

-- Exit when no more data


EXIT WHEN c_customers%NOTFOUND;

-- Process each row


DBMS_OUTPUT.PUT_LINE('Customer: ' || v_customer.FirstName ||
' ' || v_customer.LastName ||
', Credit: $' || v_customer.CreditLimit);
END LOOP;

-- Close cursor
CLOSE c_customers;

DBMS_OUTPUT.PUT_LINE('Total customers processed: ' || c_customers%ROWCOUNT);


END;
/

Cursor with Parameters

DECLARE
CURSOR c_orders(p_customer_id NUMBER, p_min_amount NUMBER) IS
SELECT OrderID, OrderDate, TotalAmount
FROM Orders
WHERE CustomerID = p_customer_id
AND TotalAmount >= p_min_amount
ORDER BY OrderDate DESC;

v_order c_orders%ROWTYPE;
BEGIN
OPEN c_orders(101, 1000); -- Customer 101, orders >= $1000

LOOP
FETCH c_orders INTO v_order;
EXIT WHEN c_orders%NOTFOUND;

DBMS_OUTPUT.PUT_LINE('Order: ' || v_order.OrderID ||


', Date: ' || v_order.OrderDate ||
', Amount: $' || v_order.TotalAmount);
END LOOP;

CLOSE c_orders;
END;
/

FOR Loop Cursor (Simplified syntax)

BEGIN
FOR customer_rec IN (
SELECT CustomerID, FirstName, LastName
FROM Customers
WHERE Status = 'Active'
) LOOP
DBMS_OUTPUT.PUT_LINE('Processing customer: ' ||
customer_rec.FirstName || ' ' ||
customer_rec.LastName);
END LOOP;
END;
/

11.6 Exception Handling 🟡 MODERATE IMPORTANCE

Definition for Interview:


Exception handling in PL/SQL provides a way to handle runtime errors gracefully without
terminating the program.

Predefined Exceptions

DECLARE
v_customer_name VARCHAR2(100);
v_order_count NUMBER;
BEGIN
-- This might raise NO_DATA_FOUND
SELECT FirstName || ' ' || LastName INTO v_customer_name
FROM Customers WHERE CustomerID = 999;

-- This might raise ZERO_DIVIDE


v_order_count := 100 / 0;

EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Customer not found');
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Cannot divide by zero');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Query returned multiple rows');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Unexpected error: ' || SQLERRM);
END;
/

User-Defined Exceptions

DECLARE
-- Define custom exception
insufficient_balance EXCEPTION;

v_account_balance NUMBER;
v_withdrawal_amount NUMBER := 5000;
BEGIN
-- Get current balance
SELECT Balance INTO v_account_balance
FROM Accounts WHERE AccountID = 'A001';

-- Check business rule


IF v_account_balance < v_withdrawal_amount THEN
RAISE insufficient_balance; -- Raise custom exception
END IF;

-- Process withdrawal
UPDATE Accounts
SET Balance = Balance - v_withdrawal_amount
WHERE AccountID = 'A001';

DBMS_OUTPUT.PUT_LINE('Withdrawal successful');

EXCEPTION
WHEN insufficient_balance THEN
DBMS_OUTPUT.PUT_LINE('Error: Insufficient funds');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Error: Account not found');
END;
/

Exception Propagation in Nested Blocks 🟡 MODERATE IMPORTANCE

DECLARE
v_outer_var NUMBER := 100;
BEGIN
DBMS_OUTPUT.PUT_LINE('Outer block started');

-- Nested block
DECLARE
v_inner_var NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('Inner block started');
v_inner_var := v_outer_var / 0; -- This will raise ZERO_DIVIDE
DBMS_OUTPUT.PUT_LINE('This line will not execute');
EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Handled in inner block');
RAISE; -- Re-raise exception to outer block
END;

DBMS_OUTPUT.PUT_LINE('This line will not execute');

EXCEPTION
WHEN ZERO_DIVIDE THEN
DBMS_OUTPUT.PUT_LINE('Handled in outer block');
END;
/

11.7 Triggers 🟡 MODERATE IMPORTANCE

Definition for Interview:


Triggers are special PL/SQL blocks that execute automatically in response to database events
like INSERT, UPDATE, DELETE, or DDL operations.

DML Triggers

-- BEFORE INSERT trigger for data validation


CREATE OR REPLACE TRIGGER trg_customer_validation
BEFORE INSERT OR UPDATE ON Customers
FOR EACH ROW
BEGIN
-- Validate email format
IF :NEW.Email NOT LIKE '%@%.%' THEN
RAISE_APPLICATION_ERROR(-20001, 'Invalid email format');
END IF;

-- Auto-generate customer code


IF :NEW.CustomerCode IS NULL THEN
:NEW.CustomerCode := 'CUST' || TO_CHAR(SYSDATE, 'YYYYMMDD') ||
LPAD(:NEW.CustomerID, 4, '0');
END IF;

-- Set registration date if null


IF :NEW.RegistrationDate IS NULL THEN
:NEW.RegistrationDate := SYSDATE;
END IF;
END;
/

-- AFTER INSERT trigger for audit logging


CREATE OR REPLACE TRIGGER trg_customer_audit
AFTER INSERT OR UPDATE OR DELETE ON Customers
FOR EACH ROW
DECLARE
v_operation VARCHAR2(10);
BEGIN
-- Determine operation type
IF INSERTING THEN
v_operation := 'INSERT';
ELSIF UPDATING THEN
v_operation := 'UPDATE';
ELSIF DELETING THEN
v_operation := 'DELETE';
END IF;

-- Log the operation


INSERT INTO Customer_Audit_Log (
CustomerID,
Operation,
OperationDate,
UserId,
OldValues,
NewValues
) VALUES (
COALESCE(:NEW.CustomerID, :OLD.CustomerID),
v_operation,
SYSDATE,
USER,
CASE WHEN UPDATING THEN
'Name: ' || :OLD.FirstName || ' ' || :OLD.LastName ||
', Email: ' || :OLD.Email
END,
CASE WHEN NOT DELETING THEN
'Name: ' || :NEW.FirstName || ' ' || :NEW.LastName ||
', Email: ' || :NEW.Email
END
);
END;
/

11.8 Packages 🟡 MODERATE IMPORTANCE

Definition for Interview:


Packages are schema objects that group related PL/SQL types, variables, constants,
subprograms, cursors, and exceptions together.

Package Specification

-- Package specification (public interface)


CREATE OR REPLACE PACKAGE pkg_customer_management AS
-- Public constants
c_vip_threshold CONSTANT NUMBER := 100000;
c_default_credit_limit CONSTANT NUMBER := 5000;

-- Public procedures and functions


PROCEDURE add_new_customer(
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2,
p_customer_id OUT NUMBER
);

FUNCTION get_customer_status(p_customer_id IN NUMBER) RETURN VARCHAR2;

PROCEDURE update_credit_limit(
p_customer_id IN NUMBER,
p_new_limit IN NUMBER
);

END pkg_customer_management;
/

Package Body

-- Package body (implementation)


CREATE OR REPLACE PACKAGE BODY pkg_customer_management AS

-- Private variables (not accessible outside package)


g_last_customer_id NUMBER := 0;

-- Private function
FUNCTION generate_customer_code(p_customer_id IN NUMBER)
RETURN VARCHAR2
AS
BEGIN
RETURN 'CUST' || LPAD(p_customer_id, 6, '0');
END;

-- Public procedure implementation


PROCEDURE add_new_customer(
p_first_name IN VARCHAR2,
p_last_name IN VARCHAR2,
p_email IN VARCHAR2,
p_customer_id OUT NUMBER
)
AS
BEGIN
-- Generate new customer ID
SELECT customer_seq.NEXTVAL INTO p_customer_id FROM DUAL;

-- Insert new customer


INSERT INTO Customers (
CustomerID,
FirstName,
LastName,
Email,
CustomerCode,
CreditLimit,
Status
) VALUES (
p_customer_id,
p_first_name,
p_last_name,
p_email,
generate_customer_code(p_customer_id),
c_default_credit_limit,
'Active'
);

g_last_customer_id := p_customer_id;

COMMIT;
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
RAISE_APPLICATION_ERROR(-20002, 'Email already exists');
END add_new_customer;

-- Public function implementation


FUNCTION get_customer_status(p_customer_id IN NUMBER)
RETURN VARCHAR2
AS
v_total_orders NUMBER;
v_total_amount NUMBER;
v_status VARCHAR2(20);
BEGIN
SELECT COUNT(*), NVL(SUM(TotalAmount), 0)
INTO v_total_orders, v_total_amount
FROM Orders
WHERE CustomerID = p_customer_id;

IF v_total_amount >= c_vip_threshold THEN


v_status := 'VIP';
ELSIF v_total_orders >= 50 THEN
v_status := 'Premium';
ELSIF v_total_orders >= 10 THEN
v_status := 'Regular';
ELSE
v_status := 'New';
END IF;

RETURN v_status;
END get_customer_status;

-- Public procedure implementation


PROCEDURE update_credit_limit(
p_customer_id IN NUMBER,
p_new_limit IN NUMBER
)
AS
BEGIN
IF p_new_limit < 0 THEN
RAISE_APPLICATION_ERROR(-20003, 'Credit limit cannot be negative');
END IF;

UPDATE Customers
SET CreditLimit = p_new_limit,
LastModified = SYSDATE
WHERE CustomerID = p_customer_id;
IF SQL%NOTFOUND THEN
RAISE_APPLICATION_ERROR(-20004, 'Customer not found');
END IF;

COMMIT;
END update_credit_limit;

END pkg_customer_management;
/

Using the Package

DECLARE
v_new_customer_id NUMBER;
v_customer_status VARCHAR2(20);
BEGIN
-- Add new customer
pkg_customer_management.add_new_customer(
'John',
'Smith',
'[email protected]',
v_new_customer_id
);

DBMS_OUTPUT.PUT_LINE('New Customer ID: ' || v_new_customer_id);

-- Get customer status


v_customer_status := pkg_customer_management.get_customer_status(v_new_customer_id);
DBMS_OUTPUT.PUT_LINE('Customer Status: ' || v_customer_status);

-- Update credit limit


pkg_customer_management.update_credit_limit(v_new_customer_id, 10000);
DBMS_OUTPUT.PUT_LINE('Credit limit updated successfully');
END;
/

12. SQL vs NoSQL Database Comparison 🔴 HIGH IMPORTANCE

12.1 Definition Comparison

SQL Databases
Definition for Interview: SQL databases are relational databases that store data in structured
tables with predefined schemas, use SQL for querying, and follow ACID properties for
transaction consistency.
Characteristics:
Fixed schema with tables, rows, and columns
Relationships defined through primary and foreign keys
Strong consistency and ACID compliance
Vertically scalable (scale up)
Mature ecosystem with standardized query language

NoSQL Databases
Definition for Interview: NoSQL databases are non-relational databases designed for flexible
schema, horizontal scalability, and handling of unstructured or semi-structured data types.
Characteristics:
Schema-less or dynamic schema
Various data models (document, key-value, graph, column-family)
Eventually consistent (BASE properties)
Horizontally scalable (scale out)
Optimized for specific use cases

12.2 ACID vs BASE Properties 🔴 HIGH IMPORTANCE

ACID Properties (SQL Databases)


Property Definition Real-life Example

Bank transfer: both debit and credit occur, or


Atomicity All operations succeed or all fail
neither

Database moves from one valid state to


Consistency Account balances follow business rules
another

Two people booking same flight seat - only one


Isolation Concurrent transactions don't interfere
succeeds

Committed changes persist despite ATM withdrawal remains recorded after power
Durability
failures outage

BASE Properties (NoSQL Databases)


Property Definition Real-life Example

System remains operational even with Social media platform works even if some
Basically Available
node failures servers down

System state may change over time


Soft State User feed updates as new data propagates
without input

Eventually System becomes consistent after some Facebook post appears on all friends' feeds
Consistent time eventually
12.3 Detailed Comparison Table
Aspect SQL Databases NoSQL Databases

Documents, key-value pairs, graphs, wide


Data Structure Tables with rows and columns
columns

Schema Fixed, predefined schema Flexible, schema-less or dynamic

Query
Standardized SQL Varies by database type
Language

Strong relationships via foreign


Relationships Weak or no enforced relationships
keys

Scalability Vertical (more powerful hardware) Horizontal (more servers)

Consistency Strong consistency (ACID) Eventual consistency (BASE)

Transactions Full ACID support Limited or no ACID support

Performance Optimized for complex queries Optimized for simple, high-volume operations

Use Cases Financial systems, ERP, CRM Big data, real-time analytics, content management

12.4 When to Use Each 🟡 MODERATE IMPORTANCE

Use SQL When:


Complex relationships: Multi-table joins and complex queries
ACID requirements: Financial transactions, inventory management
Data integrity critical: Banking, healthcare, accounting systems
Standard reporting: Business intelligence, analytics
Team expertise: Existing SQL knowledge and tools
Example Scenarios:
Banking system handling money transfers
E-commerce platform managing orders, inventory, customers
Hospital system tracking patient records and treatments
ERP system integrating finance, HR, and operations

Use NoSQL When:


Flexible data models: Varying document structures
High scalability: Millions of users, terabytes of data
Real-time performance: Low-latency requirements
Unstructured data: JSON documents, multimedia content
Rapid development: Frequent schema changes
Example Scenarios:
Social media platform storing user posts, comments, media
IoT system collecting sensor data from millions of devices
Content management system with varying article structures
Real-time analytics dashboard processing streaming data

12.5 Popular Database Examples

SQL Databases:
MySQL: Open-source, web applications
PostgreSQL: Advanced features, JSON support
Oracle: Enterprise-grade, complex transactions
SQL Server: Microsoft ecosystem integration
SQLite: Embedded, mobile applications

NoSQL Databases:
MongoDB: Document store, flexible schemas
Cassandra: Column-family, high availability
Redis: Key-value store, caching
Neo4j: Graph database, relationships
DynamoDB: AWS managed, serverless

12.6 Interview Questions and Answers


Q: Why would you choose NoSQL over SQL?
A: "I'd choose NoSQL when dealing with unstructured data, need horizontal scalability, require
flexible schemas for rapid development, or when eventual consistency is acceptable. For
example, a social media platform handling millions of posts with varying formats benefits from
MongoDB's document model and horizontal scaling."
Q: What are the trade-offs of using NoSQL?
A: "NoSQL trades consistency for availability and partition tolerance (CAP theorem). You lose
standardized query language, complex joins, and ACID guarantees. However, you gain
horizontal scalability, schema flexibility, and better performance for specific use cases."
Q: Can you use SQL and NoSQL together?
A: "Yes, polyglot persistence is common. For example, an e-commerce platform might use
PostgreSQL for transactional data (orders, payments) and MongoDB for product catalogs, with
Redis for caching. Each database serves its optimal use case within the same application."
Summary
This comprehensive guide covers all essential DBMS concepts for technical interviews:

🔴 High Priority Topics (Most Frequently Asked):


Database fundamentals and ER modeling
Keys and functional dependencies
Normalization (1NF, 2NF, 3NF, BCNF)
B-Trees and indexing
SQL commands and complex queries
JOIN operations and constraints
Transaction management and ACID properties
SQL vs NoSQL comparison

🟡 Moderate Priority Topics (Sometimes Asked):

Advanced normalization (4NF, 5NF)


PL/SQL programming constructs
Concurrency control mechanisms
Window functions and advanced SQL
OLTP vs OLAP systems

🟢 Lower Priority Topics (Rarely Asked):

Specific database vendor features


Advanced PL/SQL packages
Database administration tasks
Key Interview Success Tips:
1. Always provide definitions first when asked about concepts
2. Use real-life examples to demonstrate understanding
3. Practice SQL queries with increasing complexity
4. Understand trade-offs between different approaches
5. Know when to use each database type and normalization level
This guide provides the theoretical foundation and practical examples needed to excel in DBMS
technical interviews, covering everything from basic concepts to advanced database design
principles.

40 Database Interview Questions for Freshers -
TCS Internal Client Team
Designed for fresh graduates preparing for database roles in TCS internal projects

🟢 BASIC LEVEL QUESTIONS (1-15)

1. What is DBMS and why do we need it?


Answer: DBMS (Database Management System) is software that helps us store, organize, and
manage data efficiently. We need it because traditional file systems have problems like data
redundancy, inconsistency, and difficulty in concurrent access. For example, instead of
maintaining separate Excel files for customers, orders, and products which can get inconsistent,
DBMS keeps everything organized and connected.

2. Explain the difference between Data and Information.


Answer: Data is raw facts and figures like "John, 25, Mumbai" while Information is processed
data that gives meaning like "John is a 25-year-old customer from Mumbai who made 5
purchases this month." Data becomes information when we analyze it in context.

3. What are the main types of database keys?


Answer:
Primary Key: Uniquely identifies each record (EmployeeID)
Foreign Key: Links two tables (DepartmentID in Employee table referencing Department
table)
Unique Key: Ensures uniqueness but allows one NULL (Email address)
Composite Key: Made of multiple columns ({StudentID, SubjectID})

4. What is normalization and why is it important?


Answer: Normalization is organizing database tables to reduce redundancy and prevent
anomalies. It's important because without it, we face update anomalies (changing data in
multiple places), insertion anomalies (can't add data without other data), and deletion anomalies
(losing important data when deleting records).

5. Explain 1NF, 2NF, and 3NF with a simple example.


Answer:
1NF: Each cell has single value (no "Math, Science" in one cell)
2NF: 1NF + no partial dependencies (all non-key attributes depend on complete primary
key)
3NF: 2NF + no transitive dependencies (non-key attributes don't depend on other non-key
attributes)

6. What are ACID properties?


Answer:
Atomicity: All or nothing execution (bank transfer: both debit and credit happen, or neither)
Consistency: Database moves from valid state to valid state
Isolation: Concurrent transactions don't interfere
Durability: Committed changes persist even after system failure

7. What's the difference between DELETE and TRUNCATE?


Answer:
DELETE: Removes specific rows, can use WHERE clause, slower, can be rolled back, logs
each deletion
TRUNCATE: Removes all rows, faster, cannot be rolled back, minimal logging, resets auto-
increment counters

8. What are constraints in SQL? Name different types.


Answer: Constraints enforce rules on data to maintain integrity:
NOT NULL: Column cannot be empty
PRIMARY KEY: Unique identifier
FOREIGN KEY: Maintains referential integrity
UNIQUE: Ensures uniqueness (allows one NULL)
CHECK: Validates data against conditions
DEFAULT: Provides default value

9. Explain different types of SQL JOINs.


Answer:
INNER JOIN: Returns matching records from both tables
LEFT JOIN: All records from left table, matching from right
RIGHT JOIN: All records from right table, matching from left
FULL OUTER JOIN: All records from both tables
10. What is the difference between UNION and UNION ALL?
Answer:
UNION: Combines results and removes duplicates
UNION ALL: Combines results keeping all duplicates (faster performance)

11. What are aggregate functions in SQL?


Answer: Functions that perform calculations on multiple rows:
COUNT(): Counts rows
SUM(): Adds numeric values
AVG(): Calculates average
MIN()/MAX(): Finds minimum/maximum values

12. What's the difference between WHERE and HAVING?


Answer:
WHERE: Filters rows before grouping, cannot use aggregate functions
HAVING: Filters groups after GROUP BY, can use aggregate functions

13. What is a subquery?


Answer: A query nested inside another query. Used when you need data from one query to filter
or calculate in another query. For example, finding employees with salary greater than average
salary.

14. What are indexes and why are they used?


Answer: Indexes are database objects that improve query performance by creating shortcuts
to data. Like a book index, they help find information quickly without scanning the entire table.
Trade-off: faster SELECT but slower INSERT/UPDATE/DELETE.

15. What's the difference between clustered and non-clustered index?


Answer:
Clustered: Data rows stored in order of index key, only one per table
Non-Clustered: Separate structure pointing to data rows, multiple allowed per table
🟡 INTERMEDIATE LEVEL QUESTIONS (16-30)

16. Explain the concept of transaction and its states.


Answer: A transaction is a unit of work that must be completed entirely or not at all. States:
Active (executing), Partially Committed (final statement executed), Committed (successful),
Failed (cannot proceed), Aborted (rolled back to initial state).

17. What is deadlock and how can it be prevented?


Answer: Deadlock occurs when two transactions wait for each other's resources indefinitely.
Prevention methods: timeout mechanisms, ordered resource acquisition, deadlock detection
algorithms that abort one transaction to break the cycle.

18. What are the different isolation levels in databases?


Answer:
READ UNCOMMITTED: Allows dirty reads
READ COMMITTED: Prevents dirty reads
REPEATABLE READ: Prevents dirty and non-repeatable reads
SERIALIZABLE: Highest isolation, prevents all anomalies

19. What is the difference between SQL and NoSQL databases?


Answer:
SQL: Structured, ACID compliant, fixed schema, vertical scaling (MySQL, PostgreSQL)
NoSQL: Flexible schema, eventually consistent, horizontal scaling (MongoDB, Cassandra)

20. Query: Write a query to find the second highest salary from Employee table.
Scenario: You have an Employee table with columns: EmpID, Name, Salary, DepartmentID
Table Structure:

Employee Table:
EmpID | Name | Salary | DepartmentID
1 | Alice | 50000 | 101
2 | Bob | 60000 | 102
3 | Carol | 70000 | 101
4 | David | 60000 | 103
5 | Eve | 80000 | 102

Answer:

SELECT MAX(Salary) as SecondHighestSalary


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

Explanation: This query first finds the maximum salary in the subquery, then finds the maximum
salary that is less than the overall maximum, giving us the second highest.

21. Query: Find employees who earn more than the average salary in their
department.
Scenario: Using the same Employee table above, find employees earning above their
department's average.
Answer:

SELECT e1.Name, e1.Salary, e1.DepartmentID


FROM Employee e1
WHERE e1.Salary > (
SELECT AVG(e2.Salary)
FROM Employee e2
WHERE e2.DepartmentID = e1.DepartmentID
);

Explanation: For each employee, the subquery calculates the average salary of their
department, and we compare their salary against this average.

22. Query: Write a query using INNER JOIN to get employee names with their
department names.
Scenario: You have Employee and Department tables:
Tables:

Employee Table:
EmpID | Name | DepartmentID
1 | Alice | 101
2 | Bob | 102
3 | Carol | 101

Department Table:
DeptID | DeptName
101 | IT
102 | HR
103 | Finance

Answer:

SELECT e.Name, d.DeptName


FROM Employee e
INNER JOIN Department d ON e.DepartmentID = d.DeptID;
Explanation: INNER JOIN combines rows from both tables where the DepartmentID matches
DeptID, giving us employee names with their corresponding department names.

23. Query: Find departments with more than 2 employees using GROUP BY and
HAVING.
Answer:

SELECT DepartmentID, COUNT(*) as EmployeeCount


FROM Employee
GROUP BY DepartmentID
HAVING COUNT(*) > 2;

Explanation: GROUP BY groups employees by department, COUNT(*) counts employees per


department, and HAVING filters groups with more than 2 employees.

24. What are stored procedures and their advantages?


Answer: Stored procedures are precompiled SQL code stored in database. Advantages: better
performance (precompiled), security (parameterized), code reusability, centralized business
logic, reduced network traffic.

25. What is a trigger in database?


Answer: A trigger is special stored procedure that automatically executes in response to
database events (INSERT, UPDATE, DELETE). Used for auditing, data validation, maintaining
derived data. Example: automatically updating inventory when a sale is made.

26. Explain the concept of cursor in PL/SQL.


Answer: Cursor is a pointer to SQL result set that allows row-by-row processing. Types: Implicit
(automatic for DML) and Explicit (programmer-defined). Used when you need to process query
results one row at a time instead of all at once.

27. What are the different types of backup strategies?


Answer:
Full Backup: Complete database copy
Incremental: Only changes since last backup
Differential: Changes since last full backup
Hot Backup: While database is running
Cold Backup: When database is shut down
28. Query: Write a query to find employees with no dependents using LEFT JOIN.
Scenario: You have Employee and Dependent tables:
Tables:

Employee Table:
EmpID | Name
1 | Alice
2 | Bob
3 | Carol

Dependent Table:
DepID | EmpID | DepName
1 | 1 | Son
2 | 1 | Daughter
3 | 3 | Son

Answer:

SELECT e.Name
FROM Employee e
LEFT JOIN Dependent d ON e.EmpID = d.EmpID
WHERE d.EmpID IS NULL;

Explanation: LEFT JOIN includes all employees. WHERE d.EmpID IS NULL filters only those
employees who don't have matching records in Dependent table.

29. Query: Use UNION to combine active and inactive customers.


Scenario: You have Customer and InactiveCustomer tables with same structure.
Answer:

SELECT CustomerID, Name, 'Active' as Status FROM Customer


UNION
SELECT CustomerID, Name, 'Inactive' as Status FROM InactiveCustomer
ORDER BY Name;

Explanation: UNION combines both result sets, removes duplicates, and adds a status label to
identify the source.

30. What is database partitioning and when is it used?


Answer: Partitioning divides large tables into smaller, manageable pieces while appearing as
single table. Types: horizontal (by rows), vertical (by columns), functional (by feature). Used for
improving performance, manageability, and availability of large databases.
🔴 MODERATE-HARD LEVEL QUESTIONS (31-40)

31. Explain MVCC (Multi-Version Concurrency Control).


Answer: MVCC allows multiple transactions to access database concurrently by maintaining
multiple versions of data. Each transaction sees consistent snapshot of data. Readers don't
block writers and writers don't block readers, improving concurrency compared to traditional
locking.

32. What are database sharding and its challenges?


Answer: Sharding distributes data across multiple database instances. Benefits: horizontal
scaling, improved performance. Challenges: complex queries across shards, rebalancing data,
maintaining referential integrity, increased application complexity.

33. Query: Write a query to find nth highest salary without using LIMIT or TOP.
Answer:

SELECT DISTINCT Salary


FROM Employee e1
WHERE (
SELECT COUNT(DISTINCT Salary)
FROM Employee e2
WHERE e2.Salary > e1.Salary
) = n-1;

Explanation: For nth highest salary, there should be exactly (n-1) distinct salaries greater than it.
This correlated subquery counts higher salaries for each salary value.

34. Query: Write a complex query using window functions to rank employees by
salary within departments.
Answer:

SELECT
Name,
Salary,
DepartmentID,
RANK() OVER (PARTITION BY DepartmentID ORDER BY Salary DESC) as SalaryRank,
ROW_NUMBER() OVER (PARTITION BY DepartmentID ORDER BY Salary DESC) as RowNum
FROM Employee;

Explanation: PARTITION BY creates separate ranking within each department. RANK() gives
same rank for ties, ROW_NUMBER() gives unique sequential numbers.
35. What is CAP theorem and how does it apply to distributed databases?
Answer: CAP theorem states distributed systems can guarantee only 2 of 3: Consistency (all
nodes see same data), Availability (system remains operational), Partition tolerance (system
continues despite network failures). Example: MongoDB chooses CP, Cassandra chooses AP.

36. Query: Write a recursive query to find all subordinates of a manager.


Scenario: Employee table with ManagerID column showing hierarchical relationships.
Answer:

WITH RECURSIVE EmployeeHierarchy AS (


-- Base case: direct reports
SELECT EmpID, Name, ManagerID, 1 as Level
FROM Employee
WHERE ManagerID = @ManagerID

UNION ALL

-- Recursive case: subordinates of subordinates


SELECT e.EmpID, e.Name, e.ManagerID, eh.Level + 1
FROM Employee e
INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmpID
)
SELECT * FROM EmployeeHierarchy;

Explanation: Recursive CTE first finds direct reports, then recursively finds their subordinates,
building complete hierarchy tree.

37. What are materialized views and when would you use them?
Answer: Materialized views are precomputed result sets stored physically. Unlike regular views,
data is actually stored. Used for: complex aggregations, data warehousing, improving query
performance on expensive operations. Trade-off: storage space vs query speed.

38. Query: Write a query to find gaps in sequential data.


Scenario: Find missing order numbers in Orders table.
Answer:

SELECT (t1.OrderID + 1) as MissingStart,


(MIN(t2.OrderID) - 1) as MissingEnd
FROM Orders t1
LEFT JOIN Orders t2 ON t2.OrderID > t1.OrderID
WHERE t2.OrderID IS NOT NULL
GROUP BY t1.OrderID
HAVING MIN(t2.OrderID) > t1.OrderID + 1;
Explanation: Finds gaps by comparing each order ID with the next higher order ID, identifying
where sequence breaks.

39. Explain database replication types and their use cases.


Answer:
Master-Slave: One writable master, multiple readable slaves (read scaling)
Master-Master: Multiple writable nodes (high availability)
Synchronous: Strong consistency, slower writes
Asynchronous: Better performance, eventual consistency

40. Query: Write a query to calculate running totals using window functions.
Scenario: Calculate running sum of sales by date.
Tables:

Sales Table:
SaleDate | Amount
2024-01-01 | 1000
2024-01-02 | 1500
2024-01-03 | 800

Answer:

SELECT
SaleDate,
Amount,
SUM(Amount) OVER (ORDER BY SaleDate ROWS UNBOUNDED PRECEDING) as RunningTotal
FROM Sales
ORDER BY SaleDate;

Explanation: Window function with ROWS UNBOUNDED PRECEDING calculates cumulative sum
from first row to current row, ordered by date.

🎯 Quick Interview Tips for TCS Freshers:


1. Always start with definition when explaining concepts
2. Use real-world examples from banking, e-commerce, or social media
3. Practice writing queries on paper/whiteboard
4. Understand trade-offs (performance vs consistency, normalization vs denormalization)
5. Know basics thoroughly - 80% questions will be fundamental concepts
6. Be honest about what you don't know, but show willingness to learn
7. Relate to project scenarios - think how these concepts apply in client projects
Good luck with your TCS interview preparation! Focus on understanding concepts rather
than memorizing answers.

1. https://www.youtube.com/watch?v=YRnjGeQbsHQ
2. https://www.sqlshack.com/commonly-used-sql-server-constraints-not-null-unique-primary-key/
3. https://www.youtube.com/watch?v=7_hJsWXKIx4
4. https://www.pingcap.com/article/understanding-b-tree-and-hash-indexing-in-databases/
5. https://www.programiz.com/sql/constraints
6. https://www.tutorialspoint.com/plsql/plsql_cursors.htm
7. https://stackoverflow.com/questions/870218/what-are-the-differences-between-b-trees-and-b-trees
8. https://www.w3schools.com/sql/sql_constraints.asp
9. https://enlear.academy/plsql-cursors-procedures-functions-and-packages-81b7280f3c08?gi=47e2919
82d7c
10. https://www.sqlpipe.com/blog/b-tree-vs-hash-index-and-when-to-use-them
11. https://www.sqlshack.com/commonly-used-sql-server-constraints-foreign-key-check-default/
12. https://www.youtube.com/watch?v=jzuzxEFoiss
13. https://www.slideshare.net/ShalabhChaudhary1/plsql-cursors-triggers
14. https://www.youtube.com/watch?v=aZjYr87r1b8
15. https://www.pingcap.com/article/understanding-different-types-of-database-constraints/
16. https://www.student-notes.net/nosql-databases-types-acid-vs-base-and-use-cases/
17. https://litux.nl/Books/books/www.leothreads.com/e-book/oreillybookself/oracle/prog2/ch08_04.htm
18. https://airbyte.com/data-engineering-resources/sql-vs-nosql
19. https://www.cs.utahtech.edu/it/4310/plSQL/Exception-4.pdf
20. https://www.talend.com/resources/sql-vs-nosql/
21. https://groups.google.com/g/oracle-plsql/c/EJ9ftLnvsw8
22. https://rivery.io/data-learning-center/relational-vs-nosql-databases/
23. https://builtin.com/data-science/b-tree-index
24. https://stackoverflow.com/questions/20218009/oracle-nested-blocks-and-exception-handling
25. https://www.meegle.com/en_us/topics/nosql/acid-vs-base-in-nosql
26. https://stackoverflow.com/questions/77659127/exception-handling-for-inserts-in-plsql-block
27. https://www.integrate.io/blog/the-sql-vs-nosql-difference/
28. https://stackoverflow.com/questions/26912141/pl-sql-nested-procedure-exception-handling
29. https://pub.towardsai.net/sql-vs-nosql-choose-the-most-convenient-technology-4506d831b6e4?gi=e1
d6c301c29f
30. https://docs.oracle.com/cd/A97630_01/appdev.920/a96590/adg10pck.htm
31. https://www.youtube.com/watch?v=arnFhklHsuE
32. https://eugenekj.github.io/Linkedin_Articles/articles/PLSQL/05_Exception_Handling.html
33. https://memgraph.com/blog/sql-vs-nosql-databases
34. https://codedamn.com/news/databases/sql-constraints-primary-key-foreign-key-not-null-unique-chec
k
35. https://www.tutorialspoint.com/plsql/plsql_exceptions.htm
36. https://www.dbms-notes.com/2011/10/exception-handling-nested-blocks.html
37. https://www.scribd.com/document/856937866/Unit-2-Plsql-Trigger-Cursor
38. https://byjus.com/gate/b-plus-tree-in-dbms-notes/
39. https://learn.microsoft.com/en-us/sql/relational-databases/tables/unique-constraints-and-check-constr
aints?view=sql-server-2017
40. https://www.youtube.com/watch?v=5uT2gjX13dk
41. https://planetscale.com/blog/btrees-and-database-indexes
42. https://www.complexsql.com/interview-questions-for-tcs/
43. https://www.upgrad.com/blog/tcs-interview-questions/
44. https://www.simplilearn.com/top-sql-interview-questions-and-answers-article
45. https://prepinsta.com/interview-preparation/technical-interview-questions/dbms/
46. https://www.youtube.com/watch?v=yqqnKfms3Pg
47. https://mindmajix.com/sql-server-dba-interview-questions
48. https://www.edureka.co/blog/interview-questions/dbms-interview-questions
49. https://www.youtube.com/watch?v=7wpHHTmrbwI
50. https://www.simplilearn.com/dbms-interview-questions-and-answers-article
51. https://learningdaily.dev/top-20-database-interview-questions-and-answers-daee464734ea?gi=c7fdaf
af04ec
52. https://www.youtube.com/watch?v=WJeRXwt8w9I
53. https://resources.workable.com/database-administrator-dba-interview-questions
54. https://www.sqlshack.com/sql-server-dba-interview-questions-and-answers/
55. https://www.almabetter.com/bytes/articles/dbms-interview-questions-and-answers
56. https://www.simplilearn.com/tutorials/programming-tutorial/tcs-nqt-interview-questions
57. https://codesignal.com/blog/interview-prep/28-sql-interview-questions-and-answers-from-beginner-t
o-senior-level/
58. https://www.wscubetech.com/blog/dbms-interview-questions/
59. https://prepinsta.com/tcs/interview-questions/
60. https://www.stratascratch.com/blog/top-15-sql-server-dba-interview-questions/
61. https://flexiple.com/dbms/interview-questions

You might also like