Examples
SQL examples
Query and organize relational data.
SQL SELECT
Read rows from a table.
SQL Aggregates and GROUP BY
Summarize many rows into one row per group.
SQL Joins
Combine rows from two tables that share a key.
SQL INSERT, UPDATE, and DELETE
Add, change, and remove rows, then check the result.
SQL: constraints keep the data valid
Use PRIMARY KEY, NOT NULL, UNIQUE, CHECK, and REFERENCES so the database itself rejects bad rows.
SQL: transactions with COMMIT and ROLLBACK
Move money between two accounts so that both updates happen or neither does.
SQL: normalization removes repeated facts
Split a table that repeats a customer's city on every row into two tables and see why one update is enough.
SQL: subqueries and common table expressions
Compare each row with a group average, and name an intermediate result with WITH.
SQL: window functions for running totals and ranks
Compute a running total and a rank within each group without collapsing the rows.
SQL: views and triggers
Name a reusable query as a view and record price changes automatically with a trigger.
SQL: a unique index and the system catalog
Enforce uniqueness with an index and look up the index in the database's own catalog.