SQL — Syntax
SQL is case-insensitive
SQL keywords can be written in any case:
SELECT * FROM students;
select * from Students;
Select * FROM STUDENTS;
All three are identical. Convention is to write keywords in UPPERCASE and table/column names in lowercase:
SELECT name, age FROM students WHERE grade = 'A';
Statements end with semicolons
Each SQL statement ends with a semicolon:
SELECT * FROM students;
INSERT INTO students (name) VALUES ('Ada');
UPDATE students SET age = 25 WHERE name = 'Ada';
Some tools don't require the semicolon for single statements, but always include it — it's required when running multiple statements.
The order of clauses
SQL clauses must appear in a specific order:
SELECT columns
FROM table
WHERE condition
GROUP BY columns
HAVING condition
ORDER BY column
LIMIT number;
Not all clauses are required. The minimum query is:
SELECT * FROM students;
SELECT — choosing columns
-- All columns
SELECT * FROM students;
-- Specific columns
SELECT name, age FROM students;
-- No duplicates
SELECT DISTINCT grade FROM students;
-- Column aliases
SELECT name AS student_name, age AS student_age FROM students;
-- Expressions
SELECT name, age + 1 AS next_year_age FROM students;
FROM — specifying the table
SELECT * FROM students;
SELECT * FROM students AS s;
The FROM clause tells SQL which table to read from. Use aliases for shorter queries when joining tables.
WHERE — filtering rows
SELECT * FROM students WHERE age > 20;
SELECT * FROM students WHERE grade = 'A';
SELECT * FROM students WHERE age >= 18 AND age <= 25;
SELECT * FROM students WHERE name LIKE 'A%';
SELECT * FROM students WHERE grade IN ('A', 'B');
SELECT * FROM students WHERE age BETWEEN 18 AND 25;
SELECT * FROM students WHERE name IS NOT NULL;
ORDER BY — sorting
-- Ascending (default)
SELECT * FROM students ORDER BY name ASC;
-- Descending
SELECT * FROM students ORDER BY age DESC;
-- Multiple columns
SELECT * FROM students ORDER BY grade ASC, age DESC;
LIMIT — restricting results
-- First 5 rows
SELECT * FROM students LIMIT 5;
-- Skip 10, return 5
SELECT * FROM students LIMIT 5 OFFSET 10;
INSERT — adding data
-- Single row
INSERT INTO students (name, age, grade)
VALUES ('Ada', 20, 'A');
-- Multiple rows
INSERT INTO students (name, age, grade)
VALUES ('Bob', 22, 'B'), ('Charlie', 19, 'A');
UPDATE — modifying data
UPDATE students
SET grade = 'A-', age = 21
WHERE name = 'Bob';
DELETE — removing data
DELETE FROM students WHERE name = 'Charlie';
DELETE FROM students; -- removes ALL rows (careful!)
Data types
| Type | Description | Example |
|---|---|---|
INT | Whole number | 42 |
VARCHAR(n) | Text up to n characters | 'Hello' |
TEXT | Long text | paragraphs |
DECIMAL(p,s) | Precise decimal | 9.99 |
FLOAT | Approximate decimal | 3.14 |
DATE | Date only | '2024-01-15' |
DATETIME | Date and time | '2024-01-15 14:30:00' |
BOOLEAN | True/false | TRUE |
BLOB | Binary data | images |
NULL values
NULL means "missing" or "unknown" — not zero, not empty string:
-- Check for NULL
SELECT * FROM students WHERE age IS NULL;
SELECT * FROM students WHERE age IS NOT NULL;
-- NULL in comparisons
SELECT * FROM students WHERE age = NULL; -- WRONG! Returns nothing
SELECT * FROM students WHERE age IS NULL; -- CORRECT
Comments
-- Single line comment
/*
Multi-line comment
spans several lines
*/
SELECT * FROM students; -- inline comment
SQL's structure
Think of a query as a pipeline:
FROM → which table to read
WHERE → which rows to keep
SELECT → which columns to show
ORDER BY → how to sort the results
The database executes these steps in a specific order, not the order you write them. Understanding this helps you write efficient queries.
Mini Practice
- Write a query that selects name and age from a table, sorted by age descending
- Insert three rows with different data types
- Update all rows where a condition is true
- Delete rows matching a specific condition
- Write a query with WHERE, ORDER BY, and LIMIT together
Next: the SELECT statement in depth →
Related Topics
Frequently Asked Questions about Syntax
What is Syntax in SQL?
Syntax is a fundamental concept in SQL. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Syntax?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Syntax.
Why is Syntax important in SQL?
Syntax is essential for SQL development. Understanding this concept will help you write better code and solve real-world problems more effectively.