Lesson 10 of 60 – SQL Syntax
17%

SQL Syntax

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.

Note: SQL syntax can vary slightly between database systems such as MySQL, PostgreSQL, SQL Server, and Oracle. The basic SQL concepts are largely similar, but database-specific features and syntax should be checked when needed.

1. Basic SQL Statement

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:

  • SELECT is a SQL keyword.
  • name is a column name.
  • FROM is a SQL keyword.
  • students is the table name.
  • ; terminates the statement in many SQL environments.

2. SQL Keywords

SQL keywords are reserved words that have special meaning in SQL statements.

Common SQL keywords include:

  • SELECT
  • FROM
  • WHERE
  • INSERT
  • INTO
  • VALUES
  • UPDATE
  • DELETE
  • CREATE
  • ALTER
  • DROP
  • ORDER BY

3. SQL Statements End with a Semicolon

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.

4. SQL is Generally Case-Insensitive for Keywords

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.

Best Practice: Write SQL keywords in uppercase to make queries easier to read.

5. SELECT Statement Syntax

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.

6. Selecting Multiple Columns

Multiple columns can be separated using commas.

SELECT name, age, course
FROM students;

This retrieves three columns from the students table.

7. Selecting All Columns

The asterisk * is used to select all columns.

SELECT *
FROM students;

This returns all columns from the students table.

8. WHERE Clause Syntax

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.

9. Comparison Operators

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

10. INSERT Statement Syntax

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.

11. UPDATE Statement Syntax

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.

Warning: An UPDATE statement without a WHERE condition can modify every row in the table.

12. DELETE Statement Syntax

The DELETE statement removes rows from a table.

DELETE FROM students
WHERE id = 5;

Only the row matching the condition is targeted.

Warning: A DELETE statement without a WHERE clause can remove all rows from a table.

13. CREATE TABLE Syntax

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.

14. SQL Constraints in Syntax

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:

  • PRIMARY KEY uniquely identifies rows.
  • NOT NULL prevents NULL values in the column.
  • UNIQUE requires values to be unique under the database system's rules.

15. ORDER BY Syntax

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;

16. AND and OR Syntax

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';

17. IN Operator Syntax

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.

18. BETWEEN Operator Syntax

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.

19. LIKE Operator Syntax

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.

20. NULL Syntax

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;

21. Aliases

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;

22. SQL Comments

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.

23. SQL String Values

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;

24. SQL Date Values

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.

25. Combining SQL Clauses

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:

  1. Selects specific columns.
  2. Reads data from the students table.
  3. Filters students whose age is at least 18.
  4. Sorts the results by age in descending order.

26. SQL Query Execution Order

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:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY

This logical order becomes especially useful when you start learning GROUP BY, HAVING, joins, and aggregate functions.

27. SQL Syntax Formatting

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;
Best Practice: Use indentation and line breaks for complex SQL queries.

28. Common SQL Syntax Mistakes

Beginners commonly make mistakes such as:

  • Misspelling SQL keywords.
  • Using the wrong table name.
  • Using the wrong column name.
  • Forgetting quotation marks around text values.
  • Forgetting commas between selected columns.
  • Using the wrong comparison operator.
  • Forgetting a required closing parenthesis.
  • Forgetting a semicolon when the tool requires statement termination.

29. Complete SQL Example

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.

30. Basic SQL Syntax Formula

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.

📌 Key Points

  • SQL syntax defines the structure used to write SQL statements.
  • SQL keywords have special meanings.
  • SELECT is used to retrieve data.
  • INSERT is used to add data.
  • UPDATE is used to modify data.
  • DELETE is used to remove rows.
  • WHERE is used to filter records.
  • ORDER BY is used to sort results.
  • AND and OR combine conditions.
  • LIKE is used for pattern matching.
  • IS NULL is used to check for NULL values.
  • SQL comments can explain code.
  • Good formatting makes complex SQL queries easier to understand.

🧠 Quick Quiz

Question: Which SQL clause is used to filter rows according to a condition?