thecodex.expert · The Codex Family of Knowledge
Language Reference

SQL

Describe what data you want — the database figures out how to retrieve it. The universal language for every relational database on Earth.

ISO/IEC 9075:2023 Declarative Last verified:
New to SQL? This is the reference encyclopedia — dip in anytime. To learn step by step, start the SQL Course →
🗃 Try SQL now

Run real SQL queries in your browser. No install needed.

Open SQL Playground →

📑 SQL Reference — All Topics

Querying

SELECT, WHERE, ORDER BY, LIMIT, LIKE, IN — the basics.

JOINs & Aggregates

INNER/LEFT/RIGHT JOIN, GROUP BY, HAVING, COUNT, SUM.

Advanced SQL

Window functions, CTEs, subqueries, indexes, EXPLAIN.

What is SQL

Declarative: you describe WHAT data you want, the engine decides HOW.

Setup and Clients

No SQL runtime exists — you pick an engine (Postgres, MySQL, SQLite) first.

SELECT Basics

SELECT and FROM — the two words every query starts with.

Filtering with WHERE

Comparisons, BETWEEN, IN, and LIKE — decide which rows even qualify.

Sorting and Limiting

Without ORDER BY, row order isn't guaranteed at all.

Inner and Outer Joins

INNER JOIN drops unmatched rows; LEFT JOIN keeps them, as NULLs.

Subqueries

Correlated subqueries re-run per outer row — fast queries can quietly slow down.

GROUP BY and HAVING

WHERE can't filter on COUNT(*) — that's exactly what HAVING is for.

Window Functions

Aggregates that don't collapse rows — OVER, PARTITION BY, running totals.

Common Table Expressions (CTEs)

WITH names a subquery for clarity, and can even reference itself.

Data Types

SQLite lets you insert text into an INTEGER column — type affinity, not strict typing.

Creating Tables

DROP TABLE has no undo, no trash can, and no confirmation dialog.

Primary and Foreign Keys

Foreign keys make an orphaned reference literally impossible at the DB level.

Constraints

Rules enforced by the database itself, no matter which app is writing to it.

Indexing

Every index speeds up reads and slows down writes — always both, never just one.

Transactions

A crashed bank transfer that debits but never credits — exactly what this prevents.

ACID Properties

Four separate promises — miss one, and you don't really have transactions.

Normalization

Editing a customer's address in five different rows means the schema isn't normalized.

Views

A saved query, not a snapshot — re-run fresh, live, every single time.

Stored Procedures

The one place SQL stops being one language — PL/pgSQL, T-SQL, and MySQL differ.

Triggers

Fires with no app code calling it — powerful, and easy to hide bugs inside.

Query Optimization

Wrapping an indexed column in a function often defeats the index entirely.

Execution Plans

EXPLAIN ANALYZE turns "this feels slow" into a specific, fixable line.

NULL Handling

WHERE column = NULL never matches a single row, ever — by design.

Set Operations

UNION dedupes; UNION ALL skips that check and is almost always faster.

Database Design

One-to-many, many-to-many, and a design checklist before writing any app code.

SQL Injection and Security

A single quote in a login form has bypassed authentication — real breaches, not theory.

Canonical Definition

SQL (Structured Query Language) is a declarative domain-specific language for managing and querying data in relational database management systems (RDBMS), based on Edgar Codd's relational model, standardised as ISO/IEC 9075, and used by all major databases including PostgreSQL, MySQL, SQLite, SQL Server, and Oracle.

SELECT — the core statement

SQLselect.sql
SELECT name, salary, department
FROM employees
WHERE salary > 50000
ORDER BY salary DESC
LIMIT 10;

-- Aggregates
SELECT department, COUNT(*) AS headcount, AVG(salary) AS avg_sal
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

JOINs

SQLjoins.sql
-- INNER JOIN: only rows that match in both tables
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;

-- LEFT JOIN: all employees, even those with no department
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;

CREATE, INSERT, UPDATE, DELETE

SQLddl_dml.sql
CREATE TABLE employees (
    id        SERIAL PRIMARY KEY,
    name      VARCHAR(100) NOT NULL,
    salary    NUMERIC(10,2) CHECK (salary > 0),
    dept_id   INTEGER REFERENCES departments(id)
);

INSERT INTO employees (name, salary) VALUES ('Priya', 85000);
UPDATE employees SET salary = salary * 1.10 WHERE dept_id = 3;
DELETE FROM employees WHERE active = FALSE;

CTEs and Window Functions

SQLadvanced.sql
-- CTE
WITH high_earners AS (
    SELECT * FROM employees WHERE salary > 80000
)
SELECT department, COUNT(*) FROM high_earners GROUP BY department;

-- Window function
SELECT name, salary,
    RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rank_in_dept,
    SUM(salary) OVER (ORDER BY hire_date) AS running_total
FROM employees;
Commonly confused
WHERE filters rows; HAVING filters groups. WHERE runs before GROUP BY and cannot reference aggregates. HAVING runs after GROUP BY.
NULL = NULL is NULL (unknown), not TRUE. Use IS NULL / IS NOT NULL to test for null.
JOIN = INNER JOIN. JOIN without a qualifier is always INNER JOIN.

Sources

1
ISO/IEC 9075-2:2023 — SQL: Foundation. iso.org/standard/76583.html.
2
Codd, E. F. (1970). A Relational Model of Data for Large Shared Data Banks. CACM 13(6).
3
PostgreSQL 16 Documentation. postgresql.org/docs/current/.
Source confidence: High Last verified:
🗄️ SQL Hub
Everything SQL in one place — learning tracks, all reference pages, snippets with Try it, and SQL Playground.
SQL Hub →