Parts of an SQL Query
Statement, comment, clause, keyword, function, identifier, operator and value — the vocabulary of every query.
Before writing queries, learn the vocabulary. When a senior developer says "your WHERE clause has the wrong identifier" or an error says "Incorrect syntax near the keyword FROM", you need to know exactly which part of the query they mean. In this chapter every example uses our IPL players table:
| id | name | team | country | runs |
|---|---|---|---|---|
| 1 | Virat Kohli | RCB | India | 750 |
| 2 | Rohit Sharma | MI | India | 500 |
| 3 | Jos Buttler | RR | England | 850 |
| 4 | MS Dhoni | CSK | India | 350 |
| 5 | David Warner | DC | Australia | 700 |
| 6 | Jasprit Bumrah | MI | India | 20 |
- id — unique player number (primary key)
- name — player name
- team — IPL franchise code
- country — where the player is from
- runs — runs scored this season (sample numbers for learning)
Definition
An SQL query is a statement that asks the database to return data. It is built from clauses (SELECT, FROM, WHERE …), and each clause is made of smaller pieces: keywords, identifiers, functions, operators and values.
The full list of clauses a SELECT query can contain, in the order you write them:
sql
SELECT DISTINCT TOP n columns
FROM table
JOIN other_table ON ...
WHERE row_condition
GROUP BY columns
HAVING group_condition
ORDER BY columnsOne query, eight components
sql
-- Retrieve Indian Players
SELECT name, LOWER(team)
FROM players
WHERE country = 'India';| Component | Definition | In the query above |
|---|---|---|
| Statement | One complete instruction for the database. It can span many lines and ends with an optional ; | The whole block |
| Comment | Text for humans that the database ignores. -- comments to the end of the line; /* … */ can span many lines | -- Retrieve Indian Players |
| Clause | A section of a statement that does one job, starting with a keyword | SELECT name, LOWER(team), FROM players, WHERE country = 'India' |
| Keyword | A reserved word with a fixed meaning in SQL | SELECT, FROM, WHERE |
| Function | A built-in routine that takes input and returns a value | LOWER(team) turns RCB into rcb |
| Identifier | The name of your own object — a table, column, alias … | players, name, team, country |
| Operator | A symbol or word that compares or combines values | = (others: >, <, <>, AND, OR) |
| Value (literal) | A fixed value typed directly in the query | 'India' |
Result of the query:
| name | LOWER(team) |
|---|---|
| Virat Kohli | rcb |
| Rohit Sharma | mi |
| MS Dhoni | csk |
| Jasprit Bumrah | mi |
Note: SQL Server would label the second column (No column name) because it is a calculation with no alias. You will fix that with
ASin the next topic.
Rules worth remembering
- Keywords are not case-sensitive:
select,SELECTandSelectall work. Convention: write keywords in UPPERCASE and identifiers in lowercase so queries are easy to read. - Text values go inside single quotes:
'India'. Numbers do not:runs > 500. - Double quotes are for identifiers, not text in SQL Server:
"country"means the column country. SQL Server also lets you wrap identifiers in square brackets:[player name]— needed when a name has spaces or is a reserved word. - Whitespace and new lines don't matter to the database — put one clause per line for humans.
- Semicolon
;ends a statement. It is optional for most statements in SQL Server but required in some cases (e.g. before a CTE), so make it a habit.
Common mistakes
- ❌
WHERE country = "India"— in SQL Server this looks for a column named India → Invalid column name 'India'. ✅WHERE country = 'India'. - ❌ Naming a column with a keyword (
order,select) and getting syntax errors. ✅ Choose another name or wrap it:[order]. - ❌ Forgetting that
--comments out the rest of the line, including code after it.
Interview tip
Q: What is the difference between a keyword and an identifier?
A: A keyword is a reserved word defined by SQL itself (SELECT, FROM, WHERE). An identifier is a name you create for an object — a table, column, alias or view (players, runs).
Key points to remember
- A statement is one complete instruction; a query is a statement that returns data.
- Clauses (SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY) are the sections of a statement, each doing one job.
- Keywords are reserved SQL words; identifiers are the names of your own tables and columns.
- Functions (like LOWER) transform values; operators (=, >, <, <>, AND, OR) compare or combine them.
- Values/literals are fixed data in the query — text goes in single quotes, numbers don't.
- Comments (-- line, /* block */) are ignored by the database and are notes for humans.
- Keywords are case-insensitive; write them in UPPERCASE by convention, one clause per line.
Try it yourself
The eight components in one query
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Both comment styles
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Practice: change each component
Runs on a fresh IPL practice database in your browser · edit the query and press Ctrl + Enter
Revision Flashcards
Tap a card to flip it and see the answer.
