Menu

SQL Commands: A List of Common SQL Statements With Examples

The SQL commands you use every day, grouped by what they do (DDL, DML, DQL and TCL), each with an example you can run in SQLite.

This page includes runnable editors - edit, run, and see output instantly.

The SQL Commands at a Glance

SQL commands fall into a few groups by what they do. The table lists the ones you will use most, and every section below runs one of them.

CommandGroupWhat it does
CREATE TABLEDDLDefines a new table and its columns
ALTER TABLEDDLAdds or renames a column, or renames a table
DROP TABLEDDLDeletes a table and all its rows
INSERTDMLAdds new rows
UPDATEDMLChanges values in existing rows
DELETEDMLRemoves rows
SELECTDQLReads rows, filtered with WHERE and sorted with ORDER BY
BEGIN, COMMIT, ROLLBACKTCLGroups changes so they succeed or fail together

DDL is data definition language, DML is data manipulation language, DQL is data query language and TCL is transaction control language. The names matter less than the split: DDL changes the shape of the database, DML changes what is inside it.

CREATE TABLE: Define a Table

CREATE TABLE names the table and lists its columns with their types and rules. Every other command on this page needs a table to work on.

The page on CREATE TABLE covers rules such as NOT NULL, UNIQUE and DEFAULT.

INSERT: Add Rows

INSERT INTO adds one row or several. List the columns, then the values in the same order.

The id column filled itself in: an INTEGER PRIMARY KEY gets the next number when you leave it out. More on this in INSERT.

SELECT: Read Rows

SELECT is the command you will write most. Choose the columns, filter rows with WHERE and sort them with ORDER BY.

Only rows that pass the WHERE test come back, highest grade first. See SELECT basics and WHERE for the details.

UPDATE: Change Rows

UPDATE sets new values on rows that already exist. The WHERE clause decides which rows; leave it out and every row changes.

When you are not sure what a WHERE clause matches, run it as a SELECT first. The UPDATE page shows more patterns.

DELETE: Remove Rows

DELETE FROM removes the rows that match its WHERE clause. Like UPDATE, it affects every row if the clause is missing.

Chen's row is gone and the others are untouched. See DELETE.

ALTER TABLE and DROP TABLE: Change or Remove a Table

ALTER TABLE changes an existing table without losing its rows. DROP TABLE removes the table completely.

A new column starts as NULL in existing rows until you fill it. ALTER and DROP TABLE lists what SQLite allows.

JOIN: Combine Tables

A JOIN reads rows from two tables at once, matched on a column they share.

Boris has no enrollments, so an INNER JOIN leaves him out. A LEFT JOIN would keep him, with an empty course.

GROUP BY: Summarize Rows

GROUP BY collapses rows that share a value into one row, and an aggregate function such as COUNT or AVG summarizes each group.

Aggregate functions and GROUP BY and HAVING go further.

BEGIN, COMMIT and ROLLBACK: Group Changes

A transaction makes several changes count as one. COMMIT keeps them all; ROLLBACK undoes them all.

The transfer was committed and the second change was rolled back, so Ada ends with 70 and Boris with 80. Transactions explains when to use them.

Where SQLite Differs

The commands on this page are standard SQL, and what you learn here carries over to MySQL and PostgreSQL. The runnable examples are written for SQLite, though, and three details change on the larger databases:

  • Listing tables. The examples read sqlite_master, SQLite's own schema table. MySQL uses SHOW TABLES, and PostgreSQL uses the information_schema.tables view (or \dt in psql).
  • Automatic ids. A column declared INTEGER PRIMARY KEY numbers new rows by itself in SQLite, which is why the INSERT examples leave id out. MySQL needs AUTO_INCREMENT on the column, and PostgreSQL needs an identity column (GENERATED BY DEFAULT AS IDENTITY) or SERIAL.
  • Permissions and schema changes. SQLite has no GRANT or REVOKE, because a SQLite database is a single file with no user accounts, and its ALTER TABLE cannot change a column's type.

Frequently Asked Questions

What are the basic SQL commands?

Five cover most everyday work: CREATE TABLE to define a table, INSERT to add rows, SELECT to read them, UPDATE to change them and DELETE to remove them. ALTER TABLE, DROP TABLE, JOIN, GROUP BY and the transaction commands BEGIN, COMMIT and ROLLBACK come next.

What is the difference between DDL and DML?

DDL (data definition language) changes the structure of the database: CREATE, ALTER and DROP. DML (data manipulation language) changes the rows inside the tables: INSERT, UPDATE and DELETE. SELECT only reads, so it is often listed on its own as DQL.

Are SQL commands case sensitive?

No. SELECT, select and Select all work. Writing keywords in capitals is a convention that makes them stand out from table and column names. Text inside quotes is compared exactly, though, so 'Ada' and 'ada' are different strings in SQLite unless you ask for a case-insensitive comparison.

Do these commands work the same in MySQL and PostgreSQL?

The statements are standard SQL and mean the same thing in all three, so what you learn here carries over. The runnable examples are written for SQLite, though: listing tables reads sqlite_master here, automatic ids need AUTO_INCREMENT in MySQL or an identity column in PostgreSQL, and SQLite has no GRANT or REVOKE.

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED