Databases & SQL for NSCT: Normalization, Joins, Indexing & ACID Secrets
Database knowledge is vital for every modern developer. In the NSCT, the Databases & SQL domain (10%) rigorously tests relational schema design, query evaluation order, transaction isolation, and indexing mechanics.
1. Normalization Breakdown (1NF to BCNF)
Normalization questions ask you to identify the highest normal form of a given table or select the decomposition that eliminates anomalies without losing dependencies.
- 1NF (First Normal Form):
- Each attribute must contain atomic (indivisible) values.
- No repeating groups or multivalued attributes (e.g., storing comma-separated phone numbers violates 1NF).
- 2NF (Second Normal Form):
- Must be in 1NF.
- No partial dependency: No non-prime attribute may depend on a proper subset of any candidate key. (Applies only when the candidate key is composite).
- 3NF (Third Normal Form):
- Must be in 2NF.
- No transitive dependency: Non-prime attributes must not depend on other non-prime attributes ($X ightarrow Y$ requires $X$ to be a super key or $Y$ to be a prime attribute).
- BCNF (Boyce-Codd Normal Form):
- A stricter version of 3NF.
- For every functional dependency $X ightarrow Y$, $X$ must be a super key.
2. SQL Logical Query Processing Order
A classic trap in NSCT is testing the order in which SQL evaluates clauses versus the order they are written:
FROM&JOIN(Generates Cartesian product / join sets)WHERE(Filters rows before aggregation)GROUP BY(Collapses rows into groups)HAVING(Filters aggregated groups)SELECT(Computes projected expressions)DISTINCT(Removes duplicate tuples)ORDER BY(Sorts output rows)LIMIT/OFFSET(Restricts final row count)
Key Rule: You cannot use an alias declared in
SELECTwithin theWHEREclause becauseWHEREexecutes beforeSELECT!
3. SQL JOIN Types & NULL Handling
- INNER JOIN: Returns only matching rows from both tables.
- LEFT (OUTER) JOIN: Returns all rows from left table; non-matching right columns become
NULL. - RIGHT (OUTER) JOIN: Returns all rows from right table.
- FULL OUTER JOIN: Returns all rows from both tables, filling
NULLwhere match is missing. - CROSS JOIN: Cartesian product ($M imes N$ rows).
The Three-Valued Logic Trap
In SQL, comparison with NULL yields UNKNOWN, not TRUE or FALSE:
SELECT * FROM users WHERE salary = NULL;returns 0 rows (Always useIS NULL).NOT (UNKNOWN)evaluates toUNKNOWN.
4. ACID Properties & Transaction Isolation Levels
- Atomicity: All operations succeed, or the entire transaction is rolled back (Undo log).
- Consistency: Database transitions from one valid state to another satisfying all integrity constraints.
- Isolation: Concurrent transactions execute without cross-interference.
- Durability: Committed changes persist even after server power failure (Write-Ahead Logging / Redo log).
The 4 ANSI SQL Isolation Levels
| Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read |
|---|---|---|---|
| Read Uncommitted | Possible | Possible | Possible |
| Read Committed | Prevented | Possible | Possible |
| Repeatable Read | Prevented | Prevented | Possible (MySQL InnoDB prevents via MVCC) |
| Serializable | Prevented | Prevented | Prevented |
5. Indexing: B-Trees vs Hash Indexes
- B+ Tree Index: Supports equality (
=) and range queries (BETWEEN,<,>). Keys stored in balanced leaves linked sequentially. - Hash Index: $O(1)$ point lookups; cannot perform range scans or prefix searches (
LIKE 'abc%').
Content structured in accordance with Higher Education Commission (HEC) Computing National Curriculum, PSEB TechLift competency standards, and the 120-minute Virtual University assessment model.