Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 125 ยท Getting Started

SQL Syntax and Style

Learn the grammar of SQL statements, keywords versus identifiers, case sensitivity, single versus double quotes, comments, and a clean formatting style.

Every language has grammar, and SQL's is small. A statement is a few clauses put together in a fixed order, made of keywords the language defines, identifiers that name your tables and columns, and literals such as numbers and text. Learn the handful of rules in this lesson and you'll be able to read almost any SQL you come across.

The second half of the lesson is about style. SQL ignores extra spaces and line breaks, which means the same query can be written as one unreadable line or as a clear, scannable block. Teams that format SQL consistently find bugs faster, so it's worth building good habits from the start.

Statements, clauses and semicolons

A statement is one complete command, such as a query or an insert. It's built from clauses, each starting with a keyword:

SQL
SELECT name, price        -- SELECT clause: which columns
FROM products             -- FROM clause: which table
WHERE price > 100         -- WHERE clause: which rows
ORDER BY price DESC;      -- ORDER BY clause: which order

Clauses must appear in a fixed order. WHERE always comes after FROM, and ORDER BY after WHERE. Swapping them is a syntax error, as this example shows on purpose:

SQL Clauses in the wrong order ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

The run stops with Error: near "FROM": syntax error. Move the FROM line above WHERE and it works.

The semicolon ends a statement. When you send a single statement from application code, it's often optional, but as soon as you run several statements in a row, as in a script, a database shell or the examples on this site, each one needs its semicolon so the database knows where one ends and the next begins:

SQL Two statements, two results ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

Always end statements with a semicolon. It costs nothing and avoids confusing errors when you paste queries together later.

Whitespace and line breaks

Spaces, tabs and line breaks between words are all the same to SQL. These two queries are identical:

SQL
select name,price from products where price>100 order by price desc;

SELECT name, price
FROM products
WHERE price > 100
ORDER BY price DESC;

The database doesn't care, but people do. The second version shows the structure at a glance. Whitespace only matters inside quotes, where 'New York' and 'New York' are different values.

Keywords, identifiers and literals

Every piece of a statement is one of a few kinds of token:

Kind Examples What it is
Keyword SELECT, FROM, WHERE, AND, NULL A word SQL itself defines
Identifier products, price, first_name A name you (or the schema) chose: table, column, alias
String literal 'Berlin', '2025-03-01' Text data, in single quotes
Numeric literal 42, 19.99, 1e3 A number, no quotes
Operator =, <>, +, || Compares or combines values
Function call upper(name), count(*) A built-in calculation

Case sensitivity

Keywords are not case-sensitive anywhere. SELECT, select and SeLeCt all work. The convention on this site, and in most teams, is to write keywords in UPPERCASE and names in lowercase, so the two stand apart.

Unquoted identifiers are not case-sensitive in SQLite either: products, Products and PRODUCTS name the same table.

SQL Case doesn't matter for keywords and names ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

The query finds the Ultralight Tent. Notice the headings: they come back as name and price, because SQLite labels a plain column with the spelling from the table definition. Other databases treat identifier case in slightly different ways:

Database Unquoted names Quoted names
Standard SQL Folded to UPPERCASE Exact case
SQLite Case-insensitive Case-insensitive
PostgreSQL Folded to lowercase Exact case ("Price" and price differ)
MySQL Columns case-insensitive; table names depend on the operating system Same rules
SQL Server Depends on the database collation (usually case-insensitive) Same rules

The PostgreSQL row explains a famous headache: if a table is created as "Customers" with quotes, you must write "Customers" with quotes forever after. The simple fix works everywhere: use lowercase snake_case names and never quote them.

Data is a different matter. Case sensitivity of values depends on the database. In SQLite, = compares text exactly, so 'berlin' doesn't match 'Berlin':

SQL Values are case-sensitive in SQLite ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

The first query returns no rows; the second finds three customers. MySQL and SQL Server usually compare text case-insensitively by default, so the first query would find them there. You'll learn to control this with lower(), LIKE and collations in later lessons.

Quotes: single for text, double for names

This is the rule that confuses beginners most, so here it is plainly:

  • Single quotes '...' make a string literal: text data.
  • Double quotes "..." make a quoted identifier: the name of a table or column.

You need double quotes only when a name contains spaces or symbols, or clashes with a keyword. A column called order is a classic example, because ORDER is a keyword. This example creates such a table and reads it with quotes:

SQL Quoting awkward names

Press Run (or Ctrl + Enter) to see the result here.

It works, but every query now needs quotes. Remove the quotes around order in the SELECT line and the run fails with a syntax error. That's why good schemas avoid such names: call the column order_id and the table order_lines instead.

To put a single quote inside a string, write it twice:

SQL A quote inside a string

Press Run (or Ctrl + Enter) to see the result here.

The result is It's a sunny day. Backslashes are ordinary characters in standard SQL, so 'It\'s' doesn't work in SQLite or PostgreSQL (MySQL accepts it by default, another dialect difference).

Other quoting styles

Some databases have their own identifier quotes:

SQL
-- MySQL and MariaDB
SELECT `order` FROM `order lines`;

-- SQL Server
SELECT [order] FROM [order lines];

SQLite accepts both styles for compatibility, but double quotes are the standard and work in PostgreSQL, Oracle, SQL Server and (with ANSI_QUOTES mode) MySQL.

A trap: double quotes around text

What happens if you put text in double quotes by mistake? In standard SQL, "Berlin" means "the column named Berlin", which doesn't exist, so you get an error. SQLite is more forgiving in a way that can bite you: if no column has that name, it quietly treats the double-quoted word as a string.

SQL Double-quoted text in SQLite ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

The first query happens to work and returns the three Berlin customers. The second shows the danger: "city" is a column name, so the condition becomes city = city, which is true for every customer with a city, and the query returns all 40 customers instead of none. PostgreSQL would reject the first query outright with an error that column "Berlin" doesn't exist. Always use single quotes for text.

Comments

Comments are notes for people; the database ignores them. SQL has two styles:

  • -- starts a comment that runs to the end of the line.
  • /* ... */ wraps a comment that can span several lines.
SQL Comments ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

The query finds the three sold-out products. The commented-out AND category_id = 1 line is a common trick: you can switch part of a condition on and off while exploring. Remove the -- in front of it to see only the sold-out kitchen product. (MySQL also accepts # for line comments, and requires a space after --; the other databases don't use #.)

A formatting style that scales

SQL has no official style, but most professional style guides agree on the basics. This site follows them:

  • Keywords in UPPERCASE, names in lowercase snake_case: SELECT first_name FROM customers.
  • One clause per line for anything longer than a few words, starting each line with its keyword.
  • Indent with 2 spaces for continued lines, such as extra conditions or long column lists.
  • One column per line when the list is long, so changes show up clearly in version control.
  • Single quotes for text, and semicolons at the end of every statement.
  • Meaningful aliases (AS total_spent, not AS x).

Here's a longer query written that way. You'll understand every part of it by the end of the course:

SQL A well-formatted query ยท database: shop

Press Run (or Ctrl + Enter) to see the result here.

Notice how easy it is to answer "which rows?" (look at WHERE) or "what's the sort?" (look at ORDER BY) without reading every word. Some teams prefer lowercase keywords or put commas at the start of lines; that's fine. Consistency matters more than any particular rule.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does 'Berlin' (single quotes) mean in SQL?

    Single quotes always make a string literal. Double quotes are for identifiers such as column and table names.
  2. 2. How do you write the text Don't as an SQL string literal?

    Inside a single-quoted string, write a single quote twice. Backslash escapes aren't standard, and double quotes make an identifier.
  3. 3. In SQLite, what does this query return? SELECT count(*) FROM customers WHERE city = 'berlin'; (three customers live in Berlin.)

    SQLite's = compares text exactly, including case, so 'berlin' doesn't match 'Berlin'.
  4. 4. Which statement is true about keywords and unquoted names in SQLite?

    select, SELECT, products and PRODUCTS all work. Uppercase keywords are a convention for readability.
  5. 5. Why is WHERE city = "city" dangerous in SQLite?

    Double quotes make an identifier. Because a column named city exists, the condition becomes city = city.

Summary

  • A statement is made of clauses in a fixed order and ends with a semicolon.
  • Whitespace and line breaks don't matter outside quotes; use them to make structure visible.
  • Keywords and (in SQLite) unquoted identifiers are case-insensitive, but text values compare case-sensitively with = in SQLite.
  • Single quotes make text; double quotes quote names. Double a single quote to include it in text.
  • Comments use -- for one line and /* ... */ for several.
  • Write keywords in UPPERCASE, names in snake_case, one clause per line.

Next: Creating, Selecting and Dropping Databases.