SQL syntax refers to the rules and structure used to write SQL statements. Learning SQL syntax is important because every SQL command follows a particular structure.
SQL statements are used to create databases and tables, insert data, retrieve information, update records, delete data, and perform many other database operations.
A basic SQL statement consists of keywords, table names, column names, operators, values, and other optional elements depending on the command.
Example:
SELECT name FROM students;
Here:
SQL keywords are reserved words that have special meaning in SQL statements.
Common SQL keywords include:
A semicolon is commonly used to indicate the end of an SQL statement.
SELECT * FROM students;
The semicolon is especially useful when multiple SQL statements are written together.
SELECT * FROM students; SELECT * FROM courses;
Some interactive tools can execute a statement without requiring the semicolon in every situation, but using it is a good SQL habit.
SQL keywords are generally written in uppercase for readability.
SELECT * FROM students;
The same query may also be written as:
select * from students;
In many SQL systems, these forms are equivalent for SQL keywords. However, identifier and case behavior can vary depending on the database system and configuration.
The basic syntax of a SELECT statement is:
SELECT column_name FROM table_name;
Example:
SELECT name FROM students;
This retrieves the name column from the students table.
Multiple columns can be separated using commas.
SELECT name, age, course FROM students;
This retrieves three columns from the students table.
The asterisk * is used to select all columns.
SELECT * FROM students;
This returns all columns from the students table.
The WHERE clause is used to filter rows according to a condition.
SELECT * FROM students WHERE age > 18;
Only students whose age is greater than 18 are selected.
Comparison operators are commonly used with the WHERE clause.
| Operator | Meaning |
|---|---|
| = | Equal to |
| <> | Not equal to |
| != | Not equal to in systems that support it |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
The INSERT statement is used to add new records to a table.
INSERT INTO students
(name, age, course)
VALUES
('Rahul', 21, 'Python');
The column list specifies where the supplied values should be stored.
The UPDATE statement is used to modify existing records.
UPDATE students SET course = 'Django' WHERE id = 1;
The SET clause specifies the new value. The WHERE clause specifies which records should be changed.
The DELETE statement removes rows from a table.
DELETE FROM students WHERE id = 5;
Only the row matching the condition is targeted.
The CREATE TABLE statement is used to create a new table.
CREATE TABLE students (
id INT,
name VARCHAR(100),
age INT
);
Each column has a name followed by its data type.
Constraints can be added while defining table columns.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE
);
Here:
ORDER BY is used to sort query results.
SELECT * FROM students ORDER BY age;
By default, sorting is commonly ascending.
For descending order:
SELECT * FROM students ORDER BY age DESC;
AND and OR are used to combine conditions.
Example using AND:
SELECT * FROM students WHERE age > 18 AND course = 'Python';
Example using OR:
SELECT * FROM students WHERE course = 'Python' OR course = 'Java';
The IN operator checks whether a value matches one of the values in a specified list.
SELECT *
FROM students
WHERE course IN ('Python', 'Java', 'SQL');
This is often shorter than writing several OR conditions.
BETWEEN is used to test whether a value falls within a specified range.
SELECT * FROM students WHERE age BETWEEN 18 AND 25;
The exact behavior of BETWEEN includes both boundary values in standard SQL usage.
LIKE is used for pattern matching with text values.
Example:
SELECT * FROM students WHERE name LIKE 'R%';
The % wildcard can represent zero or more characters in common SQL implementations.
This query can return names beginning with the letter R.
NULL should be checked using IS NULL or IS NOT NULL rather than using the normal equality operator.
SELECT * FROM students WHERE email IS NULL;
To find records where email is not NULL:
SELECT * FROM students WHERE email IS NOT NULL;
An alias provides a temporary name for a column or table within a query.
Column alias:
SELECT name AS student_name FROM students;
Table alias:
SELECT s.name FROM students AS s;
Comments can be used to add explanations to SQL code.
Single-line comment:
-- Get all students SELECT * FROM students;
Multi-line comments can be written as:
/* Get all students from the database */ SELECT * FROM students;
Support for comment syntax can vary slightly between database systems.
Text values are commonly enclosed in single quotation marks.
SELECT * FROM students WHERE course = 'Python';
Numeric values normally do not require quotation marks.
SELECT * FROM students WHERE age = 21;
Date values are commonly written in a standard date format and are handled according to the database system's data types and functions.
Example:
SELECT * FROM students WHERE admission_date = '2026-09-20';
The exact date functions and syntax can vary between database systems.
SQL clauses can be combined to create more useful queries.
SELECT name, course, age FROM students WHERE age >= 18 ORDER BY age DESC;
This query:
A SQL query is written in a particular syntax, but the database engine logically processes its clauses in a defined order.
For a typical SELECT query, the logical processing order includes:
This logical order becomes especially useful when you start learning GROUP BY, HAVING, joins, and aggregate functions.
SQL can often be written on one line, but formatting queries across multiple lines makes them easier to read.
Less readable:
SELECT name,age,course FROM students WHERE age>18 ORDER BY age DESC;
More readable:
SELECT name, age, course FROM students WHERE age > 18 ORDER BY age DESC;
Beginners commonly make mistakes such as:
The following example combines several basic SQL syntax concepts.
CREATE TABLE students (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
course VARCHAR(100)
);
INSERT INTO students
(id, name, age, course)
VALUES
(1, 'Rahul', 21, 'Python'),
(2, 'Priya', 22, 'SQL'),
(3, 'Amit', 19, 'Java');
SELECT name, course, age
FROM students
WHERE age >= 20
ORDER BY age DESC;
This example creates a table, inserts records, and retrieves selected records using a condition and sorting.
For beginners, remember this basic SELECT structure:
SELECT columns FROM table WHERE condition ORDER BY column;
Not every SELECT query requires every clause. You can use only the clauses needed for your particular query.
Question: Which SQL clause is used to filter rows according to a condition?