Show 25 50 100
PDF
5 result(s) in Database with 1+ ★ Clear

Index

Showing 5 topics
  1. Keys in DBMS
  2. Normalization (1NF, 2NF, 3NF, BCNF)
  3. SQL Joins
  4. Aggregate Functions, GROUP BY and HAVING
  5. Transactions and ACID
1 Index
KeyMeaning
Super keyAny set of columns that uniquely identifies a row
Candidate keyMinimal super key (no extra column)
Primary keyThe chosen candidate key: unique and NOT NULL, one per table
Alternate keyCandidate keys that were not chosen as primary
Foreign keyColumn that refers to the primary key of another table
Composite keyPrimary key made of two or more columns
Unique keyUnique values but NULL is allowed
SQL 19 lines
CREATE TABLE departments (
    id   INT PRIMARY KEY,
    name VARCHAR(50) NOT NULL UNIQUE
);

CREATE TABLE students (
    roll    INT PRIMARY KEY,
    email   VARCHAR(100) UNIQUE,
    name    VARCHAR(100) NOT NULL,
    dept_id INT,
    FOREIGN KEY (dept_id) REFERENCES departments(id)
);

CREATE TABLE enrollments (
    roll      INT,
    course_id INT,
    grade     CHAR(2),
    PRIMARY KEY (roll, course_id)       -- composite key
);
2 Index

Normalization splits tables to remove redundancy and anomalies (insert, update, delete). It is based on functional dependency: A → B means A decides B.

Each normal form builds on the previous one

Each normal form builds on the previous one

FormRuleExample fix
1NFEvery cell holds one atomic value; no repeating groupsPhone = "017.., 018.." → one row per phone
2NF1NF and no non-key column depends on only part of a composite key(Roll, CourseID) → CourseName depends only on CourseID → move to Course table
3NF2NF and no non-key column depends on another non-key columnRoll → DeptID → DeptName → move DeptName to Department table
BCNFFor every A → B, A must be a super keyStricter version of 3NF

Q & A

  1. What is denormalization?
    Adding some redundancy back on purpose (for example, storing a total) to make reads faster, at the cost of harder updates.
3 Index

A JOIN combines rows from two tables using a related column.

Shaded area = rows returned

Shaded area = rows returned

SQL 20 lines
-- INNER JOIN: only students that have a department
SELECT s.name, d.name AS dept
FROM students s
INNER JOIN departments d ON s.dept_id = d.id;

-- LEFT JOIN: every student, dept is NULL when missing
SELECT s.name, d.name AS dept
FROM students s
LEFT JOIN departments d ON s.dept_id = d.id;

-- Students without a department
SELECT s.name
FROM students s
LEFT JOIN departments d ON s.dept_id = d.id
WHERE d.id IS NULL;

-- SELF JOIN: employee with their manager
SELECT e.name, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;

CROSS JOIN returns every combination (rows A × rows B). MySQL has no FULL OUTER JOIN: use LEFT JOIN UNION RIGHT JOIN.

4 Index

Aggregate functions: COUNT, SUM, AVG, MIN, MAX. GROUP BY makes one row per group. WHERE filters rows before grouping; HAVING filters groups after.

SQL 6 lines
SELECT dept_id, COUNT(*) AS total, AVG(cgpa) AS avg_cgpa
FROM students
WHERE cgpa IS NOT NULL
GROUP BY dept_id
HAVING COUNT(*) > 10
ORDER BY avg_cgpa DESC;
Most asked SQL queries SQL 16 lines
-- 2nd highest salary
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

-- Nth highest (N = 3)
SELECT DISTINCT salary FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;

-- Duplicate emails
SELECT email, COUNT(*) FROM users
GROUP BY email HAVING COUNT(*) > 1;

-- Highest paid employee in each department
SELECT * FROM employees e
WHERE salary = (SELECT MAX(salary) FROM employees WHERE dept_id = e.dept_id);

Order of execution: FROM → JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT.

5 Index

A transaction is a group of operations that must succeed or fail together, e.g. a money transfer.

PropertyMeaning
AtomicityAll or nothing: if one step fails, everything is rolled back
ConsistencyThe database moves from one valid state to another (rules and constraints hold)
IsolationConcurrent transactions do not see each other's half-done work
DurabilityOnce committed, data survives crashes (written to disk / log)
SQL 4 lines
START TRANSACTION;
UPDATE accounts SET balance = balance - 500 WHERE id = 1;
UPDATE accounts SET balance = balance + 500 WHERE id = 2;
COMMIT;          -- or ROLLBACK; if anything went wrong

Isolation levels

Isolation levelDirty readNon-repeatable readPhantom read
Read UncommittedPossiblePossiblePossible
Read CommittedNoPossiblePossible
Repeatable Read (MySQL default)NoNoPossible
SerializableNoNoNo