🗃️ Database Topics
1. 🔑 Various Types of Keys
- What is a primary key?
A primary key is a field (or a set of fields) that uniquely identifies each record in a table.
It cannot contain NULL values, and each table can have only one primary key.- What is a unique key?
A unique key ensures that all values in a column are different from one another.
Unlike a primary key, it can allow NULL values (usually one or more, depending on the database).- What is a candidate key?
A candidate key is any column (or combination of columns) that can uniquely identify a row.
A table can have multiple candidate keys, but only one is selected as the primary key.- What is a foreign key?
A foreign key is a field in one table that points to the primary key in another table.
It is used to create relationships between tables and maintain referential integrity.- What is a composite key?
A composite key is a primary or candidate key made up of two or more columns.
The values in these columns together must be unique, even if the individual values are not.- What are the differences between these keys?
| Key Type | Uniqueness | NULL Allowed | Main Purpose | Can Be Composite |
|---|---|---|---|---|
| Primary Key | Yes | No | Uniquely identifies each record | Yes |
| Unique Key | Yes | Yes (usually one) | Enforces uniqueness on a column | Yes |
| Candidate Key | Yes | Sometimes | A possible choice for primary key | Yes |
| Foreign Key | No (references another key) | Yes | Links to primary key in another table | Yes |
| Composite Key | Yes | Sometimes | Ensures uniqueness using multiple columns | Always |
2. 🔗 Join Types (With Examples)
- What is an INNER JOIN?
An INNER JOIN returns only the rows that have matching values in both tables. If there is no match, the row is excluded from the result.- What is a LEFT JOIN?
A LEFT JOIN returns all rows from the left table, and the matched rows from the right table. If there is no match, NULLs are returned for columns from the right table.- What is a RIGHT JOIN?
A RIGHT JOIN returns all rows from the right table, and the matched rows from the left table. If there is no match, NULLs are returned for columns from the left table.- What is a FULL OUTER JOIN?
A FULL OUTER JOIN returns all rows when there is a match in either the left or right table. Rows with no match in one of the tables will have NULLs for the missing side.- When should each type of join be used?
- Use **INNER JOIN** when you only want records with matches in both tables.
- Use **LEFT JOIN** when you need all records from the left table, even if there's no match.
- Use **RIGHT JOIN** when you need all records from the right table, even if there's no match.
- Use **FULL OUTER JOIN** when you want all records from both tables, matched where possible.- Provide examples for each join type:
Assume two tables: Employees
| id | name | dept_id |
|---|---|---|
| 1 | Alice | 10 |
| 2 | Bob | 20 |
| 3 | Charlie | NULL |
Departments
| id | dept_name |
|---|---|
| 10 | HR |
| 20 | Engineering |
| 30 | Marketing |
✅ INNER JOIN Example:
SELECT Employees.name, Departments.dept_name
FROM Employees
INNER JOIN Departments ON Employees.dept_id = Departments.id;Result:
| name | dept_name |
|---|---|
| Alice | HR |
| Bob | Engineering |
✅ LEFT JOIN Example:
SELECT Employees.name, Departments.dept_name
FROM Employees
LEFT JOIN Departments ON Employees.dept_id = Departments.id;Result:
| name | dept_name |
|---|---|
| Alice | HR |
| Bob | Engineering |
| Charlie | NULL |
✅ RIGHT JOIN Example:
SELECT Employees.name, Departments.dept_name
FROM Employees
RIGHT JOIN Departments ON Employees.dept_id = Departments.id;Result:
| name | dept_name |
|---|---|
| Alice | HR |
| Bob | Engineering |
| NULL | Marketing |
✅ FULL OUTER JOIN Example:
SELECT Employees.name, Departments.dept_name
FROM Employees
FULL OUTER JOIN Departments ON Employees.dept_id = Departments.id;Result:
| name | dept_name |
|---|---|
| Alice | HR |
| Bob | Engineering |
| Charlie | NULL |
| NULL | Marketing |
3. 🔁 Relation Types (With Examples)
- What is a one-to-one relationship?
A one-to-one relationship means each row in Table A is linked to one and only one row in Table B, and vice versa. This type is used when two tables share the same primary key but store different types of data.- What is a one-to-many relationship?
A one-to-many relationship means one row in Table A can be associated with many rows in Table B, but each row in Table B relates to only one row in Table A. This is the most common relationship in relational databases.- What is a many-to-many relationship?
A many-to-many relationship occurs when multiple records in Table A relate to multiple records in Table B. To implement it, an intermediate (junction) table is used containing foreign keys referencing both tables.- Give real-world examples of each relationship type.
| Relationship Type | Real-World Example | Tables Involved |
|---|---|---|
| One-to-One | A person and their passport | Person(id) ↔ Passport(person_id) |
| One-to-Many | A customer placing multiple orders | Customer(id) → Order(customer_id) |
| Many-to-Many | Students enrolled in multiple courses | Student(id) ↔ Enrollment(student_id, course_id) ↔ Course(id) |
📌 Example SQL Schema
✅ One-to-One Example:
CREATE TABLE Person (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Passport (
id INT PRIMARY KEY,
person_id INT UNIQUE,
passport_number VARCHAR(50),
FOREIGN KEY (person_id) REFERENCES Person(id)
);✅ One-to-Many Example:
CREATE TABLE Customer (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Orders (
id INT PRIMARY KEY,
order_date DATE,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES Customer(id)
);✅ Many-to-Many Example:
CREATE TABLE Student (
id INT PRIMARY KEY,
name VARCHAR(100)
);
CREATE TABLE Course (
id INT PRIMARY KEY,
course_name VARCHAR(100)
);
CREATE TABLE Enrollment (
student_id INT,
course_id INT,
PRIMARY KEY (student_id, course_id),
FOREIGN KEY (student_id) REFERENCES Student(id),
FOREIGN KEY (course_id) REFERENCES Course(id)
);4. ✏️ CRUD Queries
- How to write a CREATE query?
Example: Creating a Users table
CREATE TABLE Users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);- How to write a READ (SELECT) query?
Example: Fetching all users
SELECT * FROM Users;Example: Fetching specific columns with conditions
SELECT name, email FROM Users WHERE id = 1;- How to write an UPDATE query?
Example: Updating a user’s name
UPDATE Users
SET name = 'John Doe'
WHERE id = 1;- How to write a DELETE query?
Example: Deleting a user
DELETE FROM Users
WHERE id = 1;5. 🔐 Transactions
- What is a transaction in a database?
A transaction is a sequence of one or more SQL operations executed as a single unit of work.
Either all the operations are committed (saved) together, or none are applied (rolled back).- Why are transactions important?
Transactions ensure data integrity, especially in multi-user environments.
They allow multiple related operations to be treated as one atomic action, avoiding partial updates.- What are the properties of a transaction?
ACID Properties:
- Atomicity: All operations succeed or none are applied.
- Consistency: Database state remains valid after transaction.
- Isolation: Transactions are isolated from each other.
- Durability: Changes are permanently saved.Example of Transaction:
BEGIN;
UPDATE Accounts SET balance = balance - 100 WHERE account_id = 1;
UPDATE Accounts SET balance = balance + 100 WHERE account_id = 2;
COMMIT;6. 📇 Indexing (With Types & Examples)
- What is indexing in databases?
Indexing is a data structure technique used to quickly locate and access data in a database table.
It works like an index in a book — speeding up lookups without scanning the entire table.- What are the types of indexes (e.g., single-column, composite, unique, full-text)
| Type | Description | Example Use Case |
|---|---|---|
| Single-column | Index on one column. | Search by username |
| Composite | Index on multiple columns. | Search by (first_name, last_name) |
| Unique | Ensures no duplicate values for indexed columns. | Email addresses |
| Full-text | Optimized for text searching with relevance ranking. | Article content |
- When and why should indexes be used?
Indexes should be used when:
- Frequent lookups are required.
- Data is frequently sorted or filtered.
- Performance is critical for large datasets.
Indexes improve SELECT query performance but slow down INSERT, UPDATE, and DELETE operations due to maintenance overhead. They should be used on columns frequently queried in WHERE, JOIN, or ORDER BY clauses.- Provide examples of indexing. Example:
-- Single-column index
CREATE INDEX idx_username ON Users(username);
-- Composite index
CREATE INDEX idx_fullname ON Users(first_name, last_name);
-- Unique index
CREATE UNIQUE INDEX idx_email_unique ON Users(email);7. 🧱 ACID Properties
- What does ACID stand for?
ACID is an acronym for Atomicity, Consistency, Isolation, and Durability — the four key properties of a reliable transaction.- What is Atomicity?
Atomicity ensures that a transaction is completed as a single unit of work. If any part fails, the entire transaction is rolled back.- What is Consistency?
Consistency ensures that a transaction leaves the database in a valid state, maintaining data integrity.- What is Isolation?
Isolation ensures that concurrent transactions do not interfere with each other, maintaining data consistency.- What is Durability?
Durability ensures that once a transaction is committed, its changes are permanently saved, even in the event of a system failure.8. ⚖️ Normalization
- What is normalization in relational databases?
Normalization is the process of organizing data in a database to reduce redundancy and improve data integrity.- What are different normal forms (1NF, 2NF, 3NF, BCNF)?
| Normal Form | Rule | Example |
|---|---|---|
| 1NF | No repeating groups or arrays; each cell contains atomic (indivisible) values. | A table with multiple phone numbers in one column should be split into rows. |
| 2NF | 1NF + no partial dependency on part of a composite key. | Order details table with product info split into separate product table. |
| 3NF | 2NF + no transitive dependency (non-key columns depending on other non-key columns). | Customer city stored separately in City table instead of Customer. |
| BCNF | Stronger form of 3NF where every determinant is a candidate key. | Rare cases of overlapping candidate keys. |
- Why is normalization important?
Normalization is important because it helps prevent data redundancy, reduces data duplication, and ensures data consistency. It also makes it easier to maintain and update the database.- Provide examples of normalized vs non-normalized tables.
Example:
-- Non-normalized table
CREATE TABLE Orders (
id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
product_name VARCHAR(100),
quantity INT,
price DECIMAL(10, 2)
);
-- Normalized tables
CREATE TABLE Customers (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100)
);
CREATE TABLE Orders (
id INT PRIMARY KEY,
customer_id INT,
order_date DATE,
product_name VARCHAR(100),
quantity INT,
price DECIMAL(10, 2)
);- What is the difference between SQL and NoSQL databases?
| Feature | SQL (Relational) | NoSQL (Non-Relational) |
|---|---|---|
| Data Model | Tables with rows & columns | Document, key-value, graph, column-family |
| Schema | Fixed, predefined | Flexible, dynamic |
| Scaling | Vertical scaling (bigger servers) | Horizontal scaling (more servers) |
| Transactions | Strong ACID compliance | Often eventual consistency (some support ACID) |
| Query Language | SQL | API-specific or query languages (e.g., MongoDB) |
| Best For | Complex queries, structured data | Unstructured or semi-structured data |
| Examples | MySQL, PostgreSQL, Oracle | MongoDB, Cassandra, Redis, Neo4j |
9. 📊 Data Modeling (With Examples)
- What is data modeling in databases?
Data modeling is the process of structuring and organizing data in a database to optimize performance, maintain data integrity, and support specific business requirements.- What are the different data models (e.g., relational, document, graph, column-family)?
Data models define the structure and organization of data in a database.