SQL Syntax

Overview

The SQL syntax defines the rules and structure for writing SQL statements. Understanding SQL syntax is fundamental to writing correct and effective database queries.

SQL syntax is relatively straightforward, but it's important to follow the proper structure and rules to ensure your queries execute correctly and efficiently.

Basic SQL Statement Structure

Most SQL statements follow a consistent pattern:

General SQL Statement Pattern
SELECT column1, column2, ...
FROM table_name
WHERE condition
ORDER BY column_name;

SQL statements typically consist of:

SQL Syntax Rules

1. Case Sensitivity

SQL keywords are case-insensitive. However, conventions vary:

Case-Insensitive Keywords
-- These are all equivalent:
SELECT * FROM Customers;
select * from customers;
Select * From Customers;

2. Semicolons

Semicolons (;) are used to separate multiple SQL statements. Some databases require them, while others treat each statement separately:

Multiple Statements
SELECT * FROM Customers;
SELECT * FROM Orders;
SELECT * FROM Products;

3. Whitespace and Line Breaks

SQL ignores extra whitespace. You can format queries for readability:

Whitespace Examples
-- Single line
SELECT * FROM Customers WHERE age > 25;

-- Multi-line (more readable)
SELECT *
FROM Customers
WHERE age > 25
ORDER BY name;

4. String Literals

String values must be enclosed in single quotes ('). Double quotes are sometimes used for identifiers (table/column names):

String Values
SELECT * FROM Customers WHERE name = 'John Doe';

-- To include a single quote in a string, escape it (varies by database):
SELECT * FROM Customers WHERE name = 'O''Brien';

5. Comments

SQL supports comments to document your code:

Comments in SQL
-- Single-line comment

/* Multi-line
   comment */

SELECT * FROM Customers; -- End-of-line comment

SQL Statement Types

Data Query Language (DQL)

Retrieves data from the database:

SELECT Statement
SELECT column1, column2
FROM table_name
WHERE condition;

Data Manipulation Language (DML)

Modifies data in the database:

DML Statements
-- INSERT
INSERT INTO table_name (column1, column2) VALUES (value1, value2);

-- UPDATE
UPDATE table_name SET column1 = value1 WHERE condition;

-- DELETE
DELETE FROM table_name WHERE condition;

Data Definition Language (DDL)

Creates and modifies database structure:

DDL Statements
-- CREATE
CREATE TABLE table_name (column1 datatype, column2 datatype);

-- ALTER
ALTER TABLE table_name ADD column_name datatype;

-- DROP
DROP TABLE table_name;

Query Execution Order

Although SQL queries are written in a specific order, they are executed in a different logical order:

SQL Execution Order
-- Written order:
SELECT column1, COUNT(*)
FROM table_name
WHERE condition
GROUP BY column1
HAVING COUNT(*) > 5
ORDER BY column1;

-- Logical execution order:
-- 1. FROM (identify tables)
-- 2. WHERE (filter rows)
-- 3. GROUP BY (group rows)
-- 4. HAVING (filter groups)
-- 5. SELECT (select columns)
-- 6. ORDER BY (sort results)

Common SQL Operators

Comparison Operators

Logical Operators

Best Practices

Common Syntax Errors

Next Steps

Now that you understand SQL syntax basics, you're ready to write your first queries with the SELECT statement.