TExcelTAD
Home › SQL › SQL Syntax

SQL Syntax

An SQL program is made of statements. Each statement is a complete command — read some rows, insert a row, create a table — and normally ends with a semicolon.

Anatomy of a statement

sql
SELECT name, price          -- clause: which columns
FROM products               -- clause: which table
WHERE stock = 0             -- clause: which rows
ORDER BY name;              -- clause: in which order

A statement is built from clauses (SELECT …, FROM …, WHERE …). Inside the clauses you use keywords (SELECT, FROM), identifiers (table and column names), literals (0, 'Berlin') and operators (=, <, +).

sql · try it yourself
SELECT name, price
FROM products
WHERE stock = 0
ORDER BY name;
name            | price
----------------+------
Avocados        | 4.5
Sparkling Water | 4.6
(2 rows)

Rules of thumb

Rule Details
Keywords are case-insensitive SELECT, select and Select are the same. Writing keywords in UPPER CASE is a common convention.
Whitespace is free Line breaks and indentation are ignored; use them to make queries readable.
Semicolons separate statements Required between statements; optional after the last one in many tools.
Text literals use single quotes 'Berlin', and a quote inside text is doubled: 'Nonna''s Kitchen'.
Identifiers may be quoted "order" (standard/SQLite/PostgreSQL), `order` (MySQL), [order] (SQL Server) — needed only for reserved words or spaces.
Comments -- to end of line and /* block */

These two queries are identical to the database:

sql · try it yourself
select NAME, CITY from CUSTOMERS where COUNTRY = 'USA' order by name;

SELECT name,
       city
  FROM customers
 WHERE country = 'USA'
 ORDER BY name;
name            | city
----------------+---------
Night Owl Books | New York
Tech Nest       | Austin
(2 rows)

name            | city
----------------+---------
Night Owl Books | New York
Tech Nest       | Austin
(2 rows)

Note: Keywords and column names are case-insensitive, but text values are not in comparisons: country = 'usa' would find nothing. SQLite compares text exactly unless you ask otherwise (see the LIKE lesson).

Quotes inside text

Use two single quotes to write one quote character inside a string:

sql · try it yourself
SELECT customer_id, name, city
FROM customers
WHERE name = 'Nonna''s Kitchen';
customer_id | name            | city
------------+-----------------+-------
3           | Nonna's Kitchen | London
(1 row)

Double quotes mean "identifier" in standard SQL, not text. SQLite tolerates "Berlin" as text when no column of that name exists, but other databases reject it — always use single quotes for strings.

Main kinds of statements

Group Statements Purpose
Query SELECT Read data
DML (Data Manipulation) INSERT, UPDATE, DELETE Change rows
DDL (Data Definition) CREATE, ALTER, DROP Change structure
Transaction control BEGIN, COMMIT, ROLLBACK Group changes safely

Several statements in one run

A script may mix kinds. The playground shows one result block per statement that produces output; an error stops the script at that point:

sql · try it yourself
UPDATE products SET stock = stock + 10 WHERE product_id = 4;
SELECT name, stock FROM products WHERE product_id = 4;
SELECT nme FROM products;
SELECT 'never reached';
1 row affected.

name            | stock
----------------+------
Sparkling Water | 10
(1 row)

Error: no such column: nme

The misspelled column nme raises an error, and the last statement never runs.

Test yourself

Which of these is a valid text literal in standard SQL?

  1. "Berlin"
  2. 'Berlin'
  3. Berlin
  4. [Berlin]

How do you write the text It's as an SQL string?

  1. 'It\'s'
  2. 'It''s'
  3. "It's"
  4. 'It's'

Are SQL keywords case-sensitive?

  1. Yes, always upper case
  2. Yes, always lower case
  3. No
  4. Only in SQLite