SQL Keywords Reference
A practical reference to common SQL keywords, their purpose, syntax, and examples for querying, filtering, grouping, modifying, and managing data.
SQL keywords are reserved or specially recognized words that define the structure and behavior of SQL statements. Keywords such as SELECT, FROM, WHERE, JOIN, GROUP BY, INSERT, UPDATE, and DELETE tell the database what operation to perform and how the statement should be interpreted.
Learning the most common SQL keywords makes queries easier to read and write. It also helps when working with unfamiliar SQL because the keywords reveal the main structure of a statement even before you understand every table, column, or condition.
This SQL keywords reference groups commonly used keywords by purpose and provides short examples for each group. Exact keyword availability and behavior can differ between database systems, so database-specific documentation should be consulted when writing portable SQL.
What Are SQL Keywords?
An SQL keyword is a word that has a special meaning to the SQL parser. Keywords are used to construct statements, clauses, expressions, conditions, and other parts of SQL syntax.
SELECT name, price
FROM products
WHERE active = true
ORDER BY price DESC;In this query, SELECT, FROM, WHERE, ORDER BY, and DESC are keywords that define different parts of the statement. The words products, name, price, and active are identifiers referring to database objects or columns.
SQL Keywords vs Identifiers
Keywords and identifiers serve different purposes. Keywords provide instructions to the SQL parser, while identifiers name database objects such as tables, columns, schemas, indexes, and constraints.
| Category | Examples | Purpose |
|---|---|---|
| Keywords | SELECT, WHERE, JOIN, GROUP BY | Define SQL operations and structure |
| Table identifiers | users, orders, products | Identify tables |
| Column identifiers | id, name, price | Identify columns |
| Functions | COUNT, SUM, COALESCE | Perform calculations or transformations |
The distinction is important because using a keyword as an unquoted table or column name can cause syntax errors or portability problems, depending on the database system.
Are SQL Keywords Case-Sensitive?
SQL keywords are generally written in uppercase for readability, but many SQL implementations treat keywords case-insensitively. This means SELECT, select, and SeLeCt can often represent the same keyword.
SELECT id, name FROM users;
select id, name from users;Although keyword case often does not change execution, consistent capitalization makes SQL much easier to scan. A common convention is to write keywords in uppercase and identifiers in lowercase or according to the project's naming convention.
SQL Query Keywords
The following keywords are among the most important for retrieving data from relational databases.
SELECT
SELECT specifies the columns or expressions that should appear in the query result.
SELECT id, name, email
FROM customers;SELECT can retrieve individual columns, expressions, function results, or all columns with an asterisk.
DISTINCT
DISTINCT removes duplicate result rows based on the selected expressions.
SELECT DISTINCT country
FROM customers;When multiple expressions are selected, DISTINCT considers the complete combination of values when determining whether rows are duplicates.
FROM
FROM specifies the table, view, subquery, or other source from which data is retrieved.
SELECT name
FROM products;WHERE
WHERE filters rows according to a condition. Rows that do not satisfy the condition are excluded from the result.
SELECT id, name
FROM products
WHERE price > 100;ORDER BY
ORDER BY determines the order of rows in the query result.
SELECT id, name, price
FROM products
ORDER BY price DESC;ASC specifies ascending order and DESC specifies descending order. ASC is commonly the default when no direction is explicitly provided.
LIMIT
LIMIT restricts the number of rows returned in database systems that support this syntax.
SELECT id, name
FROM products
ORDER BY created_at DESC
LIMIT 10;Other database systems use different syntax for limiting results, so LIMIT should not be assumed to be universal SQL syntax.
OFFSET
OFFSET skips a specified number of rows before returning results. It is commonly used together with LIMIT for simple pagination.
SELECT id, name
FROM products
ORDER BY id
LIMIT 20 OFFSET 40;SQL JOIN Keywords
JOIN keywords combine rows from multiple tables according to a relationship or matching condition.
JOIN
JOIN combines rows from two data sources. In practice, JOIN is commonly used with a condition introduced by ON.
SELECT
customers.name,
orders.total
FROM customers
JOIN orders
ON orders.customer_id = customers.id;INNER JOIN
INNER JOIN returns rows where the join condition matches on both sides. INNER JOIN and JOIN commonly refer to the same type of join.
SELECT c.name, o.total
FROM customers AS c
INNER JOIN orders AS o
ON o.customer_id = c.id;LEFT JOIN
LEFT JOIN keeps all rows from the left side and includes matching rows from the right side when available.
SELECT c.name, o.total
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id;RIGHT JOIN
RIGHT JOIN keeps all rows from the right side and includes matching rows from the left side. It is less commonly used than LEFT JOIN because many queries can be expressed more naturally by reversing the table order and using LEFT JOIN.
FULL JOIN
FULL JOIN, often written as FULL OUTER JOIN, returns matching rows and also preserves unmatched rows from both sides where the database supports this operation.
CROSS JOIN
CROSS JOIN produces a Cartesian product containing combinations of rows from both sources.
SELECT colors.name, sizes.name
FROM colors
CROSS JOIN sizes;ON
ON specifies the condition used by a JOIN to determine which rows should be matched.
SELECT c.name, o.total
FROM customers AS c
JOIN orders AS o
ON o.customer_id = c.id;USING
USING can provide a shorter join condition when both tables contain a column with the same name and the database supports this syntax.
SELECT *
FROM orders
JOIN customers
USING (customer_id);USING has specific behavior that differs from writing an explicit ON condition, so it should be used when its semantics match the intended query.
SQL Filtering and Condition Keywords
AND
AND combines conditions. Both conditions must evaluate as true for the combined condition to be true.
SELECT *
FROM products
WHERE active = true
AND price > 100;OR
OR combines conditions where either condition can satisfy the expression.
SELECT *
FROM products
WHERE category = 'books'
OR category = 'games';NOT
NOT negates a condition or expression.
SELECT *
FROM products
WHERE NOT active = false;NOT is also commonly combined with operators such as IN, LIKE, and EXISTS.
IN
IN checks whether a value matches one of the values in a specified list or result set.
SELECT *
FROM products
WHERE category_id IN (1, 3, 5);BETWEEN
BETWEEN checks whether a value falls within an inclusive range.
SELECT *
FROM products
WHERE price BETWEEN 50 AND 100;LIKE
LIKE performs pattern matching, commonly with % for any sequence of characters and _ for a single character.
SELECT *
FROM customers
WHERE name LIKE 'Ann%';IS NULL and IS NOT NULL
IS NULL checks whether a value is NULL. IS NOT NULL checks whether a value is not NULL.
SELECT *
FROM customers
WHERE phone IS NULL;NULL represents the absence or unknown nature of a value and should not normally be compared with the = or <> operators.
SQL Aggregation Keywords
Aggregation keywords and functions are commonly used when queries summarize multiple rows.
GROUP BY
GROUP BY divides rows into groups based on one or more expressions. Aggregate functions can then calculate values for each group.
SELECT
category_id,
COUNT(*) AS product_count
FROM products
GROUP BY category_id;HAVING
HAVING filters groups after aggregation. It is commonly used with GROUP BY and aggregate functions.
SELECT
customer_id,
COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) >= 5;A useful distinction is that WHERE normally filters individual rows before grouping, while HAVING filters groups after aggregation.
Aggregate Functions Commonly Used with SQL Keywords
COUNT, SUM, AVG, MIN, and MAX are functions rather than SQL keywords in the same sense as SELECT or WHERE, but they are essential parts of aggregation queries.
| Function | Purpose | Example |
|---|---|---|
| COUNT | Counts rows or values | COUNT(*) |
| SUM | Calculates a total | SUM(amount) |
| AVG | Calculates an average | AVG(price) |
| MIN | Returns the minimum value | MIN(price) |
| MAX | Returns the maximum value | MAX(price) |
SQL Data Modification Keywords
SQL provides several statements for adding, changing, and removing data from tables.
INSERT INTO
INSERT INTO adds new rows to a table.
INSERT INTO customers (name, email)
VALUES ('Anna', '[email protected]');VALUES
VALUES provides the data values used by an INSERT statement or, in supported contexts, constructs a row value.
INSERT INTO products (name, price)
VALUES ('Keyboard', 75);UPDATE
UPDATE modifies existing rows in a table.
UPDATE products
SET price = 80
WHERE id = 10;SET
SET specifies the column values to assign in an UPDATE statement.
UPDATE users
SET status = 'active'
WHERE id = 42;DELETE
DELETE removes rows from a table according to a condition.
DELETE FROM sessions
WHERE expires_at < CURRENT_TIMESTAMP;SQL Table and Schema Definition Keywords
DDL, or Data Definition Language, contains statements for creating and changing database structures.
CREATE
CREATE is used to create database objects such as tables, schemas, views, indexes, and other objects depending on the database system.
CREATE TABLE products (
id INTEGER,
name VARCHAR(255),
price DECIMAL(10, 2)
);TABLE
TABLE identifies a table in statements such as CREATE TABLE, ALTER TABLE, and DROP TABLE.
ALTER
ALTER changes the definition of an existing database object.
ALTER TABLE products
ADD COLUMN stock INTEGER;DROP
DROP removes a database object such as a table, view, index, or schema, depending on the database system.
DROP TABLE temporary_products;TRUNCATE
TRUNCATE removes all rows from a table in database systems that support the statement. Its behavior, transaction semantics, identity handling, and restrictions can differ between database engines.
TRUNCATE TABLE temporary_products;Common Column Definition Keywords
Column definitions use keywords and constraints to describe what values a column can contain.
| Keyword | Purpose | Example |
|---|---|---|
| PRIMARY KEY | Defines a primary key constraint | id INTEGER PRIMARY KEY |
| FOREIGN KEY | Defines a foreign key constraint | FOREIGN KEY (user_id) |
| REFERENCES | Specifies a referenced table or columns | REFERENCES users(id) |
| NOT NULL | Prevents NULL values | name VARCHAR(100) NOT NULL |
| UNIQUE | Requires unique values | email VARCHAR(255) UNIQUE |
| DEFAULT | Provides a default value | status VARCHAR(20) DEFAULT 'active' |
| CHECK | Enforces a condition | CHECK (price >= 0) |
PRIMARY KEY
PRIMARY KEY identifies a constraint that uniquely identifies rows in a table. A primary key is typically non-null and unique.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name VARCHAR(100)
);FOREIGN KEY and REFERENCES
FOREIGN KEY defines a relationship between columns, while REFERENCES identifies the referenced table and column.
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
customer_id INTEGER,
FOREIGN KEY (customer_id) REFERENCES customers(id)
);NOT NULL
NOT NULL prevents a column from containing NULL values.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email VARCHAR(255) NOT NULL
);UNIQUE
UNIQUE defines a constraint requiring values, or combinations of values, to be unique according to the database's constraint semantics.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
email VARCHAR(255) UNIQUE
);DEFAULT
DEFAULT specifies a value that can be used when an INSERT does not provide a value for the column.
CREATE TABLE users (
id INTEGER PRIMARY KEY,
status VARCHAR(20) DEFAULT 'active'
);CHECK
CHECK defines a condition that inserted or updated values must satisfy according to the database system's constraint rules.
CREATE TABLE products (
id INTEGER PRIMARY KEY,
price DECIMAL(10, 2),
CHECK (price >= 0)
);SQL Set Operation Keywords
Set operations combine the results of multiple SELECT statements.
UNION
UNION combines the results of two compatible SELECT statements and normally removes duplicate rows.
SELECT email FROM customers
UNION
SELECT email FROM subscribers;UNION ALL
UNION ALL combines result sets while preserving duplicate rows.
SELECT email FROM customers
UNION ALL
SELECT email FROM subscribers;INTERSECT
INTERSECT returns rows that appear in both result sets where the database supports the operation.
SELECT email FROM customers
INTERSECT
SELECT email FROM subscribers;EXCEPT
EXCEPT returns rows from the first result set that are not present in the second result set where the database supports the operation. Some database systems use a different keyword for similar behavior.
SELECT email FROM customers
EXCEPT
SELECT email FROM blocked_users;SQL Subquery and Existence Keywords
EXISTS
EXISTS tests whether a subquery returns at least one row.
SELECT c.id, c.name
FROM customers AS c
WHERE EXISTS (
SELECT 1
FROM orders AS o
WHERE o.customer_id = c.id
);IN with a Subquery
IN can compare a value against the result of a subquery.
SELECT name
FROM customers
WHERE id IN (
SELECT customer_id
FROM orders
);WITH
WITH introduces a common table expression, or CTE, that can make complex queries easier to structure.
WITH customer_totals AS (
SELECT
customer_id,
SUM(total) AS total_spent
FROM orders
GROUP BY customer_id
)
SELECT *
FROM customer_totals;CASE
CASE provides conditional logic within an SQL expression.
SELECT
name,
CASE
WHEN price >= 100 THEN 'expensive'
WHEN price >= 50 THEN 'medium'
ELSE 'cheap'
END AS price_group
FROM products;SQL Transaction Keywords
Transaction statements control how groups of database operations are committed or rolled back.
BEGIN
BEGIN commonly starts a transaction, although exact transaction syntax varies between database systems.
BEGIN;
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;COMMIT
COMMIT permanently applies the changes made within a transaction according to the database's transaction rules.
BEGIN;
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
COMMIT;ROLLBACK
ROLLBACK reverses uncommitted changes in a transaction according to the database's transaction semantics.
BEGIN;
UPDATE products
SET price = price * 1.1;
ROLLBACK;SQL Aliases and AS
AS introduces an alias for a column expression or table reference. Aliases make complex queries easier to read and can provide clearer names for calculated results.
SELECT
p.name AS product_name,
p.price AS product_price
FROM products AS p;In many SQL dialects, AS can be omitted for aliases while preserving the same meaning. Using AS consistently can make the intended alias relationship easier to recognize.
Common SQL Keywords at a Glance
| Keyword | Main Purpose |
|---|---|
| SELECT | Retrieve columns or expressions |
| FROM | Specify the data source |
| WHERE | Filter rows |
| DISTINCT | Remove duplicate result rows |
| JOIN | Combine related data |
| ON | Specify a join condition |
| GROUP BY | Create groups for aggregation |
| HAVING | Filter aggregated groups |
| ORDER BY | Sort query results |
| LIMIT | Restrict the number of returned rows |
| OFFSET | Skip rows before returning results |
| INSERT | Add new rows |
| UPDATE | Modify existing rows |
| DELETE | Remove rows |
| CREATE | Create database objects |
| ALTER | Modify database objects |
| DROP | Remove database objects |
| TRUNCATE | Remove all rows from a table |
| UNION | Combine result sets and remove duplicates |
| UNION ALL | Combine result sets and preserve duplicates |
| WITH | Define common table expressions |
| CASE | Implement conditional expressions |
| EXISTS | Test whether a subquery returns rows |
| BEGIN | Start a transaction in supported syntax |
| COMMIT | Commit a transaction |
| ROLLBACK | Roll back a transaction |
SQL Keyword Categories
| Category | Common Keywords |
|---|---|
| Querying | SELECT, FROM, DISTINCT |
| Filtering | WHERE, AND, OR, NOT, IN, BETWEEN, LIKE |
| Sorting and pagination | ORDER BY, ASC, DESC, LIMIT, OFFSET |
| Joins | JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, ON, USING |
| Aggregation | GROUP BY, HAVING |
| Data modification | INSERT, INTO, VALUES, UPDATE, SET, DELETE |
| Schema definition | CREATE, ALTER, DROP, TABLE |
| Constraints | PRIMARY KEY, FOREIGN KEY, REFERENCES, NOT NULL, UNIQUE, DEFAULT, CHECK |
| Set operations | UNION, UNION ALL, INTERSECT, EXCEPT |
| Subqueries and expressions | EXISTS, WITH, CASE |
| Transactions | BEGIN, COMMIT, ROLLBACK |
Reserved Words and Identifiers
Some SQL keywords are reserved words, meaning they cannot be used as ordinary identifiers without quoting in certain database systems. Other words may be recognized as keywords but not strictly reserved.
For example, creating a column named order, group, or user can cause problems in some database systems because these names have special meanings or are reserved in particular SQL dialects.
CREATE TABLE orders (
id INTEGER,
"order" VARCHAR(50)
);The exact quoting character and reserved-word rules depend on the database engine. When possible, choose descriptive identifiers that do not conflict with SQL keywords instead of relying on quoted identifiers.
SQL Keywords Are Not Always Identical Across Databases
SQL is standardized, but database systems implement different dialects and extensions. A keyword or clause supported by one system may be unavailable, behave differently, or use different syntax in another.
| Feature | Portability Note |
|---|---|
| LIMIT | Common but not universal syntax for limiting rows |
| TOP | Commonly associated with SQL Server-style syntax |
| RETURNING | Supported by several systems but not universally |
| ILIKE | Available in some systems for case-insensitive pattern matching |
| EXCEPT | Supported by many systems, but set-operation syntax varies |
| FULL JOIN | Not supported by every database engine |
For portable SQL, avoid assuming that every keyword or clause behaves identically across PostgreSQL, MySQL, SQL Server, Oracle, SQLite, and other database systems.
SQL Keyword Formatting Conventions
SQL does not require keywords to be uppercase in most common environments, but uppercase keywords are a widely used formatting convention.
SELECT
c.name,
COUNT(o.id) AS order_count
FROM customers AS c
LEFT JOIN orders AS o
ON o.customer_id = c.id
WHERE c.status = 'active'
GROUP BY c.name
ORDER BY order_count DESC;The uppercase keywords make the query structure visually distinct from table names, column names, and values. SQL formatters can automate this style so that manually maintaining keyword capitalization is unnecessary.
Common SQL Keyword Mistakes
- Using a keyword as an identifier without considering reserved-word rules.
- Using WHERE when HAVING is required to filter aggregated results.
- Forgetting the ON condition when a JOIN requires one.
- Confusing UNION with UNION ALL when duplicate rows matter.
- Using UPDATE or DELETE without carefully checking the WHERE condition.
- Assuming LIMIT works identically in every SQL dialect.
- Confusing SQL keywords with functions such as COUNT and SUM.
- Using inconsistent keyword capitalization in a shared codebase.
- Assuming a keyword available in one database is supported by another.
- Adding unnecessary keywords or clauses that make a query harder to understand.
How to Learn SQL Keywords
You do not need to memorize every SQL keyword at once. Start with the keywords used in everyday queries and gradually add keywords as you encounter more advanced SQL features.
- Learn SELECT, FROM, WHERE, and ORDER BY first.
- Add AND, OR, IN, BETWEEN, LIKE, and IS NULL for filtering.
- Learn JOIN and ON when working with multiple tables.
- Add GROUP BY and HAVING for aggregation.
- Learn INSERT, UPDATE, DELETE, and SET for data modification.
- Study CREATE, ALTER, DROP, and constraints when working with schemas.
- Learn UNION, EXISTS, WITH, and CASE for more advanced queries.
- Study transaction keywords such as BEGIN, COMMIT, and ROLLBACK when working with transactional operations.
- Check the documentation for your specific database when using less common keywords.
SQL Keywords and Query Structure
Many SELECT queries follow a familiar structure. Recognizing the major keywords makes complex queries easier to navigate.
SELECT columns
FROM table
JOIN another_table
ON condition
WHERE row_condition
GROUP BY columns
HAVING group_condition
ORDER BY columns
LIMIT number;Not every query needs every clause. A simple query may contain only SELECT and FROM, while a reporting query can combine filtering, joins, grouping, aggregation, sorting, and pagination.
Frequently Asked Questions
What are SQL keywords?
SQL keywords are words with special meaning in SQL syntax. They define operations, clauses, conditions, constraints, and other parts of SQL statements. Examples include SELECT, FROM, WHERE, JOIN, GROUP BY, and ORDER BY.
What are the most important SQL keywords to learn first?
Start with SELECT, FROM, WHERE, ORDER BY, JOIN, ON, GROUP BY, HAVING, INSERT, UPDATE, DELETE, and the common filtering keywords AND, OR, IN, LIKE, and IS NULL.
Are SQL keywords case-sensitive?
In many SQL implementations, keywords are not case-sensitive. SELECT and select commonly have the same meaning. Uppercase keywords are widely used as a readability convention.
What is the difference between SQL keywords and functions?
Keywords such as SELECT and WHERE define the structure or behavior of an SQL statement. Functions such as COUNT, SUM, AVG, and COALESCE perform calculations or transformations. The exact classification of particular words can vary by SQL implementation.
What is the difference between WHERE and HAVING?
WHERE normally filters individual rows before grouping and aggregation, while HAVING filters groups after GROUP BY and aggregation have been applied.
Are SQL keywords the same in every database?
No. SQL database systems implement different dialects and extensions. Many core keywords are shared, but syntax and keyword support can differ between systems such as PostgreSQL, MySQL, SQL Server, Oracle, and SQLite.
Can I use an SQL keyword as a column name?
It depends on the database system and whether the word is reserved. Some systems allow keywords as identifiers when they are quoted, but avoiding keyword conflicts is generally simpler and more portable.
Helpful SQL Tools
Several online SQL tools can make working with keywords easier. Keyword case converters can change SQL keywords to uppercase or lowercase consistently. Syntax highlighters can visually distinguish keywords from identifiers and values. SQL formatters can organize clauses and indentation, while query validators can help detect syntax problems after editing a statement.
Conclusion
SQL keywords form the vocabulary used to construct database statements. Querying commonly relies on SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, and ORDER BY, while INSERT, UPDATE, and DELETE modify data. CREATE, ALTER, DROP, and constraint keywords manage database structures.
The most useful way to learn SQL keywords is to understand their role within complete statements rather than memorize a long alphabetical list. Once the major groups become familiar, even unfamiliar SQL queries become easier to read because their keywords reveal the structure of the operation.