Dualo
SQL Essentials

What is SQL?

A declarative language for querying relational databases. You describe WHAT you want; the engine figures out HOW to get it.

1 min read

SQL is an ANSI/ISO-standardized declarative language for relational algebra operations over tables. Queries are parsed into a logical plan (relational operators: scan, filter, join, aggregate, sort), rewritten for equivalence (predicate pushdown, join reordering), and translated into a physical plan by the query optimizer using statistics on the data distribution.

foundations (Codd, 1970): data as relations (sets of tuples), each column with a domain (type), primary keys (unique, non-null), foreign keys (referential integrity). Normal forms (1NF through BCNF) guide schema design to reduce redundancy.

: parse → rewrite → optimize (cost-based using statistics) → execute (iterator pipelines, vectorized operators in modern engines like DuckDB, ClickHouse). Plans vary by data volume: 10 rows → nested loop; 10M rows → hash join. Optimizer chooses based on estimated cardinality.

Language categories: DML (SELECT, INSERT, UPDATE, DELETE), DDL (CREATE, ALTER, DROP, TRUNCATE), DCL (GRANT, REVOKE), TCL (BEGIN, COMMIT, ROLLBACK, SAVEPOINT). Each has distinct semantics around transactions (DDL in most engines auto-commits; use CTE or transactions carefully).

Dialect realities: though SQL-92/99/2003/2008 cover core features, 30% of real-world SQL is engine-specific. Examples: LIMIT (Postgres/MySQL) vs TOP (SQL Server) vs FETCH FIRST (standard, Oracle/DB2); RETURNING clause (Postgres) vs OUTPUT (SQL Server); window-function availability (modern everywhere; some legacy systems lack them).

Not 'just a query language': modern SQL engines include JSON operators (jsonb in Postgres, JSON in MySQL), array types, full-text search, geospatial (PostGIS), graph queries (SQL/PGQ, Neptune openCypher-in-SQL), UDFs in multiple languages. You can solve a huge class of problems without leaving the database.

Grounded on https://www.postgresql.org/docs/current/sql.html

Next up

SELECT basics — filter, sort, limit

The bread and butter: SELECT columns FROM table WHERE conditions ORDER BY sort LIMIT n. Learn this well; everything else builds on it.