SQL Syntax and Statements — Introduction to SQL Vocabulary
SQL Syntax
The office asks for names and cities, but a missing comma makes the intended two-column query unclear.
An SQL statement expresses an operation using keywords, identifiers, literals and punctuation. Its syntax tells the database how those parts fit together.
Explain and apply
Read a SQL statement, identify its clauses and separate a placeholder from a runnable value.
Work it out
Write a two-column class report using a comma, a single-quoted class value and a clear sort order.
Avoid mistakes
Keyword case is usually flexible; identifier case and quoting vary by database and platform.
SQL Syntax: the request and its result
School need
The office asks for names and cities, but a missing comma makes the intended two-column query unclear.
Database operation
An SQL statement expresses an operation using keywords, identifiers, literals and punctuation. Its syntax tells the database how those parts fit together.
Check the outcome
The comma separates two requested columns. The quoted class value is compared with each stored class, and the explicit order makes the displayed rows repeatable.
Understand what the database does
SELECT introduces the requested expressions, FROM names a table when one is needed, and WHERE gives a row condition. A column name identifies stored data; a quoted string such as '10A' is a literal value. Whitespace and line breaks can improve readability without changing a statement’s meaning. A semicolon separates complete statements in many clients.
Related concepts: Select Data and Filter with WHERE.
SQL Syntax
SQL syntax defines the structure and format of commands you write to interact with a database. Think of it as grammar rules for the SQL language — it tells you how to write statements so the database understands what you want.
Basic SQL Syntax Rules
SQL · portable concepts; preserved examples identify their engine
For reference, consider the following SQL statements.
SELECT column1, column2
FROM table_name
WHERE condition;
SELECT * FROM students;
- Keywords like
SELECT,FROM,WHERE, etc., are not case-sensitive, but writing them in uppercase improves readability. - Statements end with a semicolon
;— this marks the end of a complete command. In the above code, we have two commands. - Strings must be in single quotes: for example,
'Delhi'. - Column names and table names are written in lowercase or snake_case by convention, but they can be any valid identifier.
- Spaces between keywords and elements (like table names, column names, etc.) are required for clarity and correctness. Don't write
SELECT*FROM— instead useSELECT * FROM.
Spacing and Formatting
SQL · portable concepts; preserved examples identify their engine
- Always use a space between keywords:
SELECTandFROMmust be separated. - Use commas between column names:
SELECT name, age, city - You can write SQL on one line or across multiple lines — indentation is optional but helpful for readability.
SELECT name, age
FROM students
WHERE age>12;
SELECT name, age FROM students WHERE age>12;
Symbols in SQL Syntax
Query discussed in this section:
SELECT name, age
FROM students
WHERE age>12;
SELECT name, age FROM students WHERE age>12;SQL · portable concepts; preserved examples identify their engine
| Symbol | Meaning | Example |
|---|---|---|
* |
Select all columns | SELECT * FROM students; |
, |
Separate columns in a list | SELECT name, age |
' ' |
Wrap string values | WHERE city = 'Delhi' |
= |
Equality condition | WHERE age = 15 |
; |
End of SQL statement | SELECT * FROM students; |
Identifiers: Table and Column Names
- Table and column names must begin with a letter (A–Z).
- They can include letters, numbers, and underscores
_. - Avoid using SQL keywords like
SELECTorWHEREas table or column names. - If a name contains spaces or special characters, wrap it in double quotes (e.g.,
"student name").
Good Practices
- Use consistent indentation and line breaks for better readability.
- Use uppercase for SQL keywords and lowercase for table/column names.
- End every statement with a semicolon
;to avoid syntax errors.
Important differences and mistakes
Keyword case is usually flexible; identifier case and quoting vary by database and platform. A SELECT can compute a constant without FROM, so FROM is not universally required. Templates containing column1 or condition must be replaced before execution.
Try it with the school data
Write a two-column class report using a comma, a single-quoted class value and a clear sort order.
This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
SQL · create only these practice tables in a disposable database:
CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');
Worked answer and reasoning
This practice uses a separate, fresh fixture named practice_students. It does not continue the changing table state of the earlier examples.
| roll_no | name | class | marks | city |
|---|---|---|---|---|
| 1 | Aarav | 10A | 85 | Pune |
| 2 | Diya | 9B | 70 | Chennai |
| 3 | Sneha | 10A | 85 | Pune |
SQL · create only these practice tables in a disposable database:
CREATE TABLE practice_students (roll_no INTEGER PRIMARY KEY, name VARCHAR(50), class VARCHAR(10), marks INTEGER, city VARCHAR(30));
INSERT INTO practice_students VALUES
(1, 'Aarav', '10A', 85, 'Pune'),
(2, 'Diya', '9B', 70, 'Chennai'),
(3, 'Sneha', '10A', 85, 'Pune');
SQL · this practice variation runs with SQLite 3; the preserved examples retain their stated dialect
SELECT name, city FROM practice_students WHERE class = '10A' ORDER BY roll_no;| name | city |
|---|---|
| Aarav | Pune |
| Sneha | Pune |
The comma separates two requested columns. The quoted class value is compared with each stored class, and the explicit order makes the displayed rows repeatable.
Check your understanding
Use the idea independently
An SQL statement expresses an operation using keywords, identifiers, literals and punctuation. Its syntax tells the database how those parts fit together.
The comma separates two requested columns. The quoted class value is compared with each stored class, and the explicit order makes the displayed rows repeatable.
Continue your understanding with Select Data and Filter with WHERE.
Source for the engine-specific behavior: MySQL 8.4 reference documentation.
Reaching the recap and completing the quizzes are separate milestones.