SQL Keywords and the Shape of a Statement
Keywords are reserved words that mark each clause of a SQL statement, and the clauses of a query are always written in a fixed order.
Read a query by clause and distinguish language keywords from chosen identifiers.
The first time you read a real query it can look like a wall of capital letters. It is far less random than it looks: a handful of reserved words mark where each part of the statement begins, and they always appear in the same order. Learn the order once and every query becomes readable.
The rule
Keywords are SQL's grammar; everything else is a name you chose.
A keyword is a word with a fixed meaning to the database: SELECT, FROM, WHERE, CREATE, TABLE, ORDER, BY. Everything else in a statement is an identifier (a table or column name you created) or a value (like 'Pune' or 42).
SELECT name, city -- keyword, then column names
FROM customers -- keyword, then a table name
WHERE city = 'Pune' -- keyword, then a condition
ORDER BY name; -- keywords, then a column; ; ends the statementA few rules follow from this:
- Keywords are case-insensitive.
select,SELECTandSelectall work. Writing them in capitals is a convention that makes your own names stand out. - A statement ends with a semicolon. Some tools let you skip it for a single statement, but it is required to separate several.
- Reserved words make poor names. A table called
orderclashes with the keywordORDER, which is one reason tables are often plural, likeorders. - Text values go in single quotes:
'Pune'. Double quotes, in standard SQL, are for identifiers.
Keywords also come in families by job. DDL (data definition) shapes tables: CREATE, ALTER, DROP, TRUNCATE. DML (data manipulation) works with rows: SELECT, INSERT, UPDATE, DELETE. DCL controls access: GRANT, REVOKE. TCL controls transactions: COMMIT, ROLLBACK.