SQL commands
The 29 SQL commands you need for queries, changing data, building tables, transactions and permissions, grouped as DQL, DML, DDL, TCL and DCL. Every example runs on the same free sample database, and each row links to a lesson where your query is checked.
Every example runs on Kestrel, a fictional online store. Download the database to run them yourself.
Query data (DQL)
| Name | What it does | Example | Practice |
|---|---|---|---|
| SELECT | Reads columns from a table. SELECT * returns every column. | | SELECT: choose the columns you need |
| WHERE | Keeps only the rows that meet a condition. Combine conditions with AND and OR. | | WHERE: keep only the rows you want |
| ORDER BY, LIMIT | Sorts the result, DESC for largest first, and LIMIT keeps the first rows. SQL Server writes SELECT TOP 5 instead. | | ORDER BY and LIMIT: sort and take the top rows |
| DISTINCT | Removes duplicate rows from the result. | | DISTINCT: remove duplicate rows |
| LIKE | Matches text patterns: % stands for any run of characters and _ for exactly one. | | LIKE: match text patterns |
| IN, BETWEEN | IN matches any value in a list. BETWEEN matches a range and includes both ends. | | AND, OR, IN and BETWEEN: combine conditions |
| IS NULL | Finds missing values. = NULL never matches anything; use IS NULL or IS NOT NULL. | | NULL, IS NULL and COALESCE |
| GROUP BY | Returns one row per group, with aggregates such as COUNT, SUM and AVG. | | GROUP BY: totals for each group |
| HAVING | Filters groups after GROUP BY. WHERE filters rows before the grouping. | | HAVING: filter groups |
| JOIN | Combines rows from two tables where the keys match. JOIN means INNER JOIN. | | INNER JOIN: combine matching rows |
| LEFT JOIN | Keeps every row of the left table, with NULLs where nothing matches, so customers with no orders still appear. | | LEFT JOIN: keep rows without a match |
| UNION | Stacks two results with the same columns and removes duplicates. UNION ALL keeps them. | | UNION and UNION ALL |
| CASE WHEN | If-then logic inside a query. | | CASE WHEN: if-then logic |
| Subquery | A query inside another query, here to compare each price with the average. | | Subqueries: a query inside a query |
| WITH (CTE) | Names a step so the next query can read it like a table. | | CTEs: name a step with WITH |
| OVER (window) | Calculates across related rows without collapsing them, here a price rank within each category. | | Window functions: ROW_NUMBER, RANK and DENSE_RANK |
Change data (DML)
| Name | What it does | Example | Practice |
|---|---|---|---|
| INSERT | Adds rows to a table. List the columns so the values go where you expect. | | INSERT: add rows |
| UPDATE | Changes values in existing rows. Without WHERE it changes every row. | | UPDATE: change rows |
| DELETE | Removes rows. Run the same WHERE in a SELECT first to see what will go. | | DELETE: remove rows |
Define tables (DDL)
| Name | What it does | Example | Practice |
|---|---|---|---|
| CREATE TABLE | Creates a table with its columns, data types and keys. | | CREATE TABLE and data types |
| ALTER TABLE | Changes a table's structure: add, rename or drop a column. | | ALTER TABLE |
| CREATE VIEW | Saves a query under a name you can select from like a table. | | Views |
| CREATE INDEX | Builds an index so filters and joins on that column can find rows faster. | | Indexes |
| TRUNCATE | Removes every row at once and keeps the empty table. | | DELETE: remove rows |
| DROP TABLE | Deletes a table and all its rows. IF EXISTS avoids an error when it is not there. | | CREATE TABLE and data types |
Transactions (TCL)
| Name | What it does | Example | Practice |
|---|---|---|---|
| BEGIN, COMMIT | Groups changes into a transaction: COMMIT keeps them all, together. | | Transactions: BEGIN, COMMIT, ROLLBACK |
| ROLLBACK | Undoes every change since BEGIN. | | Transactions: BEGIN, COMMIT, ROLLBACK |
Permissions (DCL)
| Name | What it does | Example | Practice |
|---|---|---|---|
| GRANT | Gives a user or role a permission on a table (PostgreSQL syntax). | PostgreSQL syntax | |
| REVOKE | Takes a permission back. | PostgreSQL syntax |
Checked against
Every row was last checked on Nov 20, 2026.
Questions
What is the difference between DDL and DML?
DDL (data definition language) commands change the structure: CREATE, ALTER, DROP and TRUNCATE. DML (data manipulation language) commands change the rows: INSERT, UPDATE and DELETE. SELECT is often grouped on its own as DQL.
Which database do the examples run on?
They run on Kestrel, a fictional online store you can download for free as a SQL script, CSV files or an Excel workbook. The syntax is standard SQL and PostgreSQL runs every example as written. SQL Server writes TOP instead of LIMIT, and SQLite has no TRUNCATE or GRANT.