SQL Tutorial · Revision notes
SQL Views, Indexes & Practice
A view is a saved query that works like a virtual table. An index makes searching faster. A transaction groups changes so they are saved together (COMMIT) or cancelled together (ROLLBACK). This chapter ends with popular interview queries.
Quick revision
- View = saved SELECT query. It stores no data. DROP VIEW does not delete table data.
- Views simplify queries and hide private columns.
- Index = fast lookup, like a book index. It speeds reads and slows writes a little.
- Index columns used in WHERE, JOIN and ORDER BY. Primary key and unique already have indexes.
- Transaction = group of statements. COMMIT saves. ROLLBACK cancels. SAVEPOINT marks a point.
- ACID: Atomicity, Consistency, Isolation, Durability.
- Duplicates: GROUP BY + HAVING COUNT(*) > 1.
- Nth highest salary: DISTINCT + ORDER BY DESC + LIMIT 1 OFFSET N-1.