Skip to content

SQL Syntax

A SQL statement is a single instruction to the database, written as a few keywords with names and values between them. This one reads every row and column of the Productions table from the example database:

SELECT * FROM Productions;
  • Keywords such as SELECT, FROM and WHERE say what to do. They are not case sensitive, so select works as well as SELECT. Writing them in capitals is a convention that keeps them apart from table and column names.
  • Names pick out tables and columns, such as Productions and Title. Whether these are case sensitive depends on the database and how it is set up, so it is safest to write them exactly as they were created.
  • Values are the data you compare against or store. Text goes in single quotes, as in 'UK'; numbers do not.
  • Line breaks and spaces do not matter to the database. Splitting a long statement over several lines, one clause per line, makes it easier to read.

A semicolon marks the end of a statement. Some tools need it only to separate several statements sent together; others expect it every time. Ending every statement with one works everywhere.

Command What it does
SELECT reads data
INSERT INTO adds new rows
UPDATE changes existing rows
DELETE removes rows
CREATE DATABASE creates a database
CREATE TABLE creates a table
ALTER TABLE changes a table’s structure
DROP TABLE removes a table and all its data
CREATE INDEX adds an index to speed up searches
DROP INDEX removes an index

The first four work on the data inside tables; the rest work on the tables and databases themselves.