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.
| Command | Group | What it does |
|---|---|---|
CREATE TABLE | DDL | Defines a new table and its columns |
ALTER TABLE | DDL | Adds or renames a column, or renames a table |
DROP TABLE | DDL | Deletes a table and all its rows |
INSERT | DML | Adds new rows |
UPDATE | DML | Changes values in existing rows |
DELETE | DML | Removes rows |
SELECT | DQL | Reads rows, filtered with WHERE and sorted with ORDER BY |
BEGIN, COMMIT, ROLLBACK | TCL | Groups 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 usesSHOW TABLES, and PostgreSQL uses theinformation_schema.tablesview (or\dtinpsql). - Automatic ids. A column declared
INTEGER PRIMARY KEYnumbers new rows by itself in SQLite, which is why theINSERTexamples leaveidout. MySQL needsAUTO_INCREMENTon the column, and PostgreSQL needs an identity column (GENERATED BY DEFAULT AS IDENTITY) orSERIAL. - Permissions and schema changes. SQLite has no
GRANTorREVOKE, because a SQLite database is a single file with no user accounts, and itsALTER TABLEcannot 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.