SQL Syntax Basics
Before diving into writing real queries, it helps to get comfortable with a handful of general conventions that apply across nearly every SQL statement you’ll ever write. None of these rules are difficult, but getting them right early will save you confusing errors later — and following the community conventions will make your SQL much easier for other people (and future you) to read.
Keywords: case-insensitive, but conventionally UPPERCASE
SELECT, FROM, WHERE, and INSERT are case-insensitive — select, Select, and SELECT all work identically. However, the overwhelming convention across the SQL community is to write keywords in **UPPERCASE**, which makes it visually easy to separate SQL’s structural vocabulary from your own table and column names at a glance.Same query, two styles
-- Works, but hard to scan select id, name from customers where country = 'Canada'; -- Conventional and easier to read SELECT id, name FROM customers WHERE country = 'Canada';
Identifiers: typically lowercase
snake_case), such as first_name or order_items. Combining UPPERCASE keywords with lowercase identifiers gives you SQL that reads clearly at a glance:UPPERCASE keywords, lowercase snake_case identifiers
SELECT first_name, last_name FROM employees WHERE department_id = 4;
Statements end with a semicolon
;). Most database tools will run a single statement without it, but the semicolon becomes essential the moment you write multiple statements together — it’s how the database knows where one statement ends and the next begins.Multiple statements need semicolons to separate them
INSERT INTO employees (name) VALUES ('Jane Doe');
UPDATE employees SET department_id = 4 WHERE name = 'Jane Doe';String literals use single quotes
'Canada' or '2024-01-01'.WHERE country = "Canada" instead of WHERE country = 'Canada' is one of the most common beginner mistakes, and depending on the database it may throw an error or silently do something you didn’t intend.Single quotes for values, double quotes for special identifiers
-- Correct: single quotes for a string value SELECT * FROM customers WHERE country = 'Canada'; -- Double quotes are for identifiers, e.g. a column name with a space SELECT "first name" FROM legacy_customers;
Whitespace and formatting are flexible
SELECT, FROM, WHERE, and so on) starting on its own line, purely to make the query easier for humans to read and debug.Consistent formatting matters far more for humans than for the database
Most teams adopt a shared style guide so queries look familiar across a codebase
Many editors and tools include a SQL formatter that can auto-apply consistent style for you