Master the building blocks of SQL to filter, sort, and retrieve the data you need.
If you’re just starting to learn SQL, understanding the basic syntax is the key to writing your first queries. SQL may look a bit different from other programming languages, but its structure is consistent, readable, and beginner-friendly.
This guide covers the core elements of SQL syntax with simple examples and best practices. You can test everything in the Galaxy SQL Editor.
SQL is made up of commands called statements, and each one begins with a keyword:
SELECT
– to retrieve dataINSERT INTO
– to add dataUPDATE
– to modify existing recordsDELETE FROM
– to remove dataThese statements often follow a consistent structure with clauses like FROM
, WHERE
, and ORDER BY
.
The most common SQL command is SELECT
, used to query data from a table.
SELECT column1, column2
FROM table_name
WHERE condition
ORDER BY column1 ASC
LIMIT 10;
SELECT name, email
FROM users
WHERE is_active = true
ORDER BY created_at DESC
LIMIT 5;
This retrieves the five most recently created active users.
In most databases, every SQL statement ends with a semicolon (;
). This tells the database that the command is complete.
SELECT * FROM users;
SQL keywords (like SELECT
, FROM
, WHERE
) are case-insensitive. But table names and column names can be case-sensitive, depending on the database.
Best Practice: Use uppercase for SQL keywords and lowercase for table/column names:
SELECT name FROM users;
Use single quotes ('
) for string and date values:
SELECT * FROM orders WHERE status = 'shipped';
SELECT * FROM users WHERE created_at >= '2023-01-01';
Don’t use double quotes for values—that’s used for identifiers in some databases.
FROM
Specifies which table you’re querying.
SELECT * FROM products;
WHERE
Filters rows based on a condition.
SELECT * FROM users WHERE country = 'USA';
Learn more in our WHERE Clause guide.
ORDER BY
Sorts the result set by one or more columns.
SELECT * FROM users ORDER BY created_at DESC;
LIMIT
and OFFSET
Limits how many rows are returned and skips a certain number.
SELECT * FROM users LIMIT 10 OFFSET 20;
See our full LIMIT and OFFSET guide.
You can leave notes in your SQL using comments:
-- This is a single-line comment
SELECT * FROM users; -- Get all users
/*
This is a multi-line comment
for longer explanations.
*/
SQL ignores extra spaces and line breaks, so feel free to format your queries for readability.
SELECT
name,
email
FROM
users
WHERE
is_active = true;
Here’s a complete example with multiple clauses:
SELECT name, created_at
FROM users
WHERE email IS NOT NULL
ORDER BY created_at DESC
LIMIT 5;
This query:
name
and created_at
columnsMastering SQL syntax is the first step toward building queries that extract powerful insights from your data. Start small, get familiar with each clause, and experiment with real queries in a live environment.
You can write and run your first queries instantly using the Galaxy SQL Editor.
Continue learning: