SQL ยท Chapter 2 of 42

SQL Syntax

SQL statements end with a semicolon `;`. Keywords like SELECT, FROM, WHERE are case-insensitive but conventionally written in UPPERCASE.

String literals use single quotes: `'hello'`. Identifiers (table/column names) use no quotes, or double quotes for reserved words.

Syntax
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1;

Basic statement layout

SELECT columns FROM table [WHERE condition] [GROUP BY col] [ORDER BY col] [LIMIT n];

Comments

`-- single line` and `/* multi line */`.

Example 1 (sql)
-- Get top 3 oldest users
SELECT name, age FROM users
ORDER BY age DESC
LIMIT 3;
Output
name | age
Ben  | 30
Ana  | 25
Cara | 22

Read + sort + limit โ€” the most common pattern.

Example 2 (sql)
SELECT * FROM users WHERE name = 'Ana';
Output
Ana | 25

String literals use single quotes.

Key points

  • Statements end with `;`.
  • Keywords case-insensitive; UPPERCASE by convention.
  • String literals use single quotes.
  • Comments: `--` or `/* */`.
๐Ÿ’ก Note: Double quotes are for identifiers, single quotes are for values. Mixing them up is a common beginner error.

๐Ÿ“ Quick Quiz

1. SQL statements end with:

2. String literals use:

3. SQL keywords are: