SQL Syntax Explained
Structured Query Language (SQL) is the standard language used to communicate with relational databases. Every SQL query follows a specific syntax, which is simply the set of rules that tells the database how to interpret your commands.
Learning SQL syntax is the first step toward becoming a database administrator, software developer, data analyst, or backend engineer. Fortunately, SQL uses simple English-like commands, making it one of the easiest programming languages to learn.
In this guide, you'll learn the most commonly used SQL syntax with practical examples.
What Is SQL Syntax?
SQL syntax refers to the rules used to write valid SQL statements. Every database management system such as MySQL, PostgreSQL, SQL Server, Oracle Database, and SQLite follows standard SQL syntax, although some advanced features differ between systems.
A typical SQL query looks like this:
SELECT column_name
FROM table_name
WHERE condition;
Each SQL statement ends with a semicolon (;), although some database tools allow you to omit it when running a single query.
SELECT Statement
The SELECT statement retrieves data from a table.
Syntax
SELECT column_name
FROM table_name;
Example
Retrieve all customers.
SELECT *
FROM Customers;
Retrieve only names and emails.
SELECT Name, Email
FROM Customers;WHERE Clause
The WHERE clause filters records.
Syntax
SELECT column_name
FROM table_name
WHERE condition;
Example
Find customers from Tanzania.
SELECT *
FROM Customers
WHERE Country = 'Tanzania';
Find employees older than 30.
SELECT *
FROM Employees
WHERE Age > 30;ORDER BY
Use ORDER BY to sort results.
Syntax
SELECT column_name
FROM table_name
ORDER BY column_name ASC;
Example
Sort products by price.
SELECT ProductName, Price
FROM Products
ORDER BY Price ASC;
Sort students by marks from highest to lowest.
SELECT StudentName, Marks
FROM Students
ORDER BY Marks DESC;INSERT INTO
Use INSERT to add new records.
Syntax
INSERT INTO table_name (column1, column2)
VALUES (value1, value2);
Example
INSERT INTO Customers (Name, Country)
VALUES ('John Smith', 'Kenya');
Insert multiple records.
INSERT INTO Customers (Name, Country)
VALUES
('Alice', 'Uganda'),
('Brian', 'Tanzania'),
('David', 'Rwanda');UPDATE Statement
The UPDATE statement modifies existing data.
Syntax
UPDATE table_name
SET column_name = value
WHERE condition;
Example
UPDATE Customers
SET Country = 'Uganda'
WHERE CustomerID = 5DELETE Statement
The DELETE statement removes records.
Syntax
DELETE FROM table_name
WHERE condition;
Example
DELETE FROM Customers
WHERE CustomerID = 8;
Important: Always use a
WHEREclause unless you intend to delete every row in the table.
CREATE TABLE
Use CREATE TABLE to create a new database table.
Syntax
CREATE TABLE table_name (
column_name datatype,
column_name datatype
);
Example
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
FirstName VARCHAR(50),
LastName VARCHAR(50),
Age INT,
Course VARCHAR(100)
);ALTER TABLE
Modify an existing table.
Example
Add a new column.
ALTER TABLE Students
ADD Email VARCHAR(100);DROP TABLE
Delete an entire table.
DROP TABLE Students;GROUP BY
Group rows with similar values.
Example
Count customers by country.
SELECT Country, COUNT(*) AS TotalCustomers
FROM Customers
GROUP BY Country;HAVING Clause
Filter grouped data.
SELECT Country, COUNT(*) AS TotalCustomers
FROM Customers
GROUP BY Country
HAVING COUNT(*) > 5;JOIN StatementJOIN combines records from multiple tables.
Example
SELECT Customers.Name, Orders.OrderDate
FROM Customers
INNER JOIN Orders
ON Customers.CustomerID = Orders.CustomerID;SQL Operators
Common comparison operators include:
| Operator | Description |
|---|---|
| = | Equal |
| <> | Not Equal |
| > | Greater Than |
| < | Less Than |
| >= | Greater Than or Equal |
| <= | Less Than or Equal |
| BETWEEN | Between two values |
| LIKE | Pattern matching |
| IN | Match multiple values |
Example:
SELECT *
FROM Products
WHERE Price BETWEEN 50 AND 200;SQL Comments
Single-line comment
-- This query retrieves all employees
SELECT * FROM Employees;
Multi-line comment
/*
Retrieve active customers
from the customer table
*/
SELECT * FROM Customers;SQL Syntax Best Practices
Write SQL keywords in uppercase for readability.
Use descriptive table and column names.
Always include a
WHEREclause when updating or deleting records.Format queries with proper indentation.
Avoid using
SELECT *in production unless necessary.Back up important databases before making structural changes.
Conclusion
Understanding SQL syntax is the foundation of working with relational databases. Once you become comfortable with statements such as SELECT, INSERT, UPDATE, DELETE, JOIN, and GROUP BY, you'll be able to build powerful applications and analyze data efficiently.
Practice writing SQL queries regularly, experiment with sample databases, and gradually move on to advanced topics like subqueries, indexes, stored procedures, views, and transactions.
The more you practice SQL syntax, the more confident you'll become in managing and querying databases.