Write your first query and see results instantly using Galaxy’s free SQL editor.
SQL (Structured Query Language) is the standard language used to communicate with databases. Whether you want to analyze data, build applications, or explore a dataset, it all starts with your first query.
In this guide, we’ll walk you through the key components of a basic SQL query, how to write one from scratch, and how to avoid common beginner mistakes. If you've never written SQL before, this is the perfect place to start.
You can try all the examples below using the Galaxy SQL Editor—a fast, free way to explore data in your browser.
A SQL query is a command you send to a database to retrieve or manipulate data. Queries usually start with the SELECT
keyword and can include filters, sorting, and more.
SELECT column1, column2
FROM table_name
WHERE condition;
This tells the database:
SELECT
)FROM
)WHERE
, optional)Before you can query anything, you need to know the table name. You can usually find this in your database’s schema.
Let’s say we’re working with a table called users
that looks like this:
id
(integer)name
(text)email
(text)created_at
(timestamp)Start by selecting specific columns you want to retrieve.
SELECT name, email FROM users;
This returns a list of all user names and their emails.
Want all columns? Use *
:
SELECT * FROM users;
(Just know that SELECT *
isn’t ideal for production use—read why in our Common SQL Errors guide.)
Use the WHERE
clause to return only rows that meet certain conditions.
SELECT name, email FROM users WHERE email IS NOT NULL;
This returns only users who have an email address.
Learn more about filtering in our full WHERE clause guide.
Use ORDER BY
to sort your results.
SELECT name, created_at FROM users ORDER BY created_at DESC;
This shows the most recently created users first.
Explore sorting vs. grouping in our GROUP BY vs ORDER BY guide.
Use LIMIT
to return only a few rows. This is useful when previewing large tables.
SELECT * FROM users LIMIT 10;
Want to paginate through results? Add OFFSET
:
SELECT * FROM users LIMIT 10 OFFSET 10;
More on that in our LIMIT and OFFSET guide.
You can use multiple filters together:
SELECT name FROM users
WHERE email IS NOT NULL AND created_at >= '2023-01-01';
This filters for active users created in 2023.
You don’t always need the data—you might just want a count.
SELECT COUNT(*) FROM users WHERE email IS NOT NULL;
Learn how to use aggregate functions like COUNT
, SUM
, and AVG
in our functions guide.
;
) at the end of your query=
instead of IS
for NULL
values (e.g., email IS NULL
, not email = NULL
)WHERE
in an UPDATE
or DELETE
query—learn more in our INSERT/UPDATE/DELETE guideYour first SQL query is the gateway to understanding your data. Once you learn the basics—SELECT
, FROM
, WHERE
, ORDER BY
, and LIMIT
—you can build more advanced queries step by step.
Want to start querying right away? Head over to the Galaxy SQL Editor and try it live.
Continue learning: