← Study Notes Databases


Databases

SQL

The declarative language for relational databases — you state what data you want with SELECT, JOIN and GROUP BY, and the query planner decides how to fetch it. Decades of tooling, strong consistency and ACID transactions make it the safe default. Real mastery is reading the query plan (EXPLAIN) and knowing why a missing index turns a query into a full table scan.


Purpose

SQL is the declarative language of relational databases: you state what data you want — SELECT, JOIN, GROUP BY, WHERE — and the query planner works out how to fetch it. Fifty years of refinement have made it the most durable interface in software.

When to Use It

It is the safe default for almost any application's system of record: strong consistency, ACID transactions, rich querying and an enormous tooling ecosystem. Analytics, reporting and ad-hoc exploration are where its declarative power is most obvious.

Trade-offs

The planner's freedom is a double-edged sword: the same query can be instant or catastrophic depending on indexes and statistics, and the database's cost decisions are invisible until you look. ORMs hide SQL but not its costs — N+1 query patterns are the classic leak.

Implementation

Learn joins properly (inner vs left), aggregate with GROUP BY/HAVING, and use parameterised queries always — never string concatenation. Real mastery is reading EXPLAIN output: seeing whether a query walks an index or scans the table, and fixing the schema or query accordingly.