Beginners Resources

How to Write Your First SQL Query

Welcome to the Galaxy, Guardian!
Oops! Something went wrong while submitting the form.

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.

What Is a SQL Query?

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.

Anatomy of a Simple Query:

SELECT column1, column2
FROM table_name
WHERE condition;

This tells the database:

  • What to show (SELECT)
  • Where to find it (FROM)
  • When to return it (WHERE, optional)

Step 1: Choose a Table

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)

Step 2: Select Specific Columns

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.)

Step 3: Filter Results with WHERE

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.

Step 4: Sort Results with ORDER BY

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.

Step 5: Limit How Many Rows You Get

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.

Step 6: Combine Conditions with AND / OR

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.

Bonus: COUNT How Many Results

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.

Common Mistakes to Avoid

  • Forgetting a semicolon (;) at the end of your query
  • Misspelling table or column names
  • Using = instead of IS for NULL values (e.g., email IS NULL, not email = NULL)
  • Leaving out WHERE in an UPDATE or DELETE query—learn more in our INSERT/UPDATE/DELETE guide

Final Thoughts

Your 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:

Check out some other beginners resources