SQL Syntax
Chapter 2 of 40 · Estimated read time: 5 min
The Structure of an SQL Statement
SQL statements are made up of clauses. Each clause starts with a keyword. A SELECT statement follows this order:
SELECT column1, column2
FROM table_name
WHERE condition
GROUP BY column1
HAVING aggregate_condition
ORDER BY column1
LIMIT number;
Not all clauses are required. The only mandatory parts of a SELECT statement are SELECT and FROM. Every other clause is optional.
SQL Keywords Are NOT Case-Sensitive
All three of these queries do the same thing:
-- All three are identical to the database engine: SELECT product_name FROM products WHERE price > 500; select product_name from products where price > 500; Select Product_Name From Products Where Price > 500;
Convention: Write SQL keywords in UPPERCASE and table/column names in lowercase. This makes queries easier to read and is the standard in professional environments.
Semicolons
A semicolon ; marks the end of an SQL statement. It is required when running multiple statements together, and recommended as a habit even for single statements.
-- Running two statements together requires semicolons SELECT COUNT(*) FROM products; SELECT COUNT(*) FROM customers;
Whitespace and Line Breaks
SQL ignores extra whitespace and line breaks. These two are identical:
-- Compact (hard to read): SELECT product_name,price FROM products WHERE category='Electronics' ORDER BY price DESC; -- Readable (recommended): SELECT product_name, price FROM products WHERE category = 'Electronics' ORDER BY price DESC;
Comments in SQL
-- This is a single-line comment (standard SQL, works everywhere) # This is also a single-line comment (MySQL only) /* This is a multi-line comment */ SELECT product_name -- inline comment after a clause FROM products;
Text Values Need Quotes
String (text) values in conditions must be wrapped in single quotes. Numbers do not need quotes.
-- ✅ Correct SELECT * FROM customers WHERE city = 'Noida'; SELECT * FROM products WHERE price > 500; -- ❌ Wrong — missing quotes around text SELECT * FROM customers WHERE city = Noida; -- ❌ Wrong — quotes around a number (works in some DBs but is bad practice) SELECT * FROM products WHERE price > '500';
Double vs Single quotes: Standard SQL uses single quotes for string values. Double quotes are for identifiers (column/table names with spaces or reserved words). PostgreSQL follows this strictly; MySQL treats both similarly by default.