10 SQL Query Examples Every Beginner Must Know (With Code)

Dev Support

September 16, 2025

-

4 min read

Learn

If you’re just getting started with SQL, it can feel overwhelming to know where to begin. The good news? Writing your first queries is simpler than you think. In this beginner SQL tutorial, we’ll go through ten of the most common SQL query examples that every new learner should practice.

By the end, you’ll not only understand how to write SQL queries, you’ll also feel confident applying them in real projects.

Interactive SQL Sandbox

Click the button below to open the SQL Sandbox. Copy any of the queries from this article and paste them into the editor. Press Run Query to see how the database responds. It’s the easiest way to learn by doing.

1. Select All Data from a Table

SELECT *
FROM customers;

This query retrieves every column and row from the customers table. It’s the simplest way to look at everything inside a dataset.

2. Select Specific Columns

SELECT first_name, last_name, email
FROM customers;

Instead of pulling all the data, you can target only the columns you need. This keeps results clean and focused.

3. Filter Rows with WHERE

SELECT *
FROM orders
WHERE order_date = '2025-09-01';

The WHERE clause filters results. Here, we’re only seeing orders placed on a specific date.

4. Sort Results with ORDER BY

SELECT *
FROM products
ORDER BY price DESC;

Use ORDER BY to sort data. In this case, we’re showing products in descending order of price—most expensive first.

5. Limit the Number of Rows

SELECT *
FROM customers
LIMIT 5;

Sometimes you just need a quick sample. LIMIT keeps your results manageable by returning only a set number of rows.

6. Count Rows

SELECT COUNT(*)
FROM orders;

The COUNT() function tells you how many rows exist in a table. A quick way to measure table size or query results.

7. Find Unique Values with DISTINCT

SELECT DISTINCT country
FROM customers;

DISTINCT removes duplicates. This is useful when you want to see unique entries, like how many different countries your customers are from.

8. Use Aliases for Clarity

SELECT first_name AS fname, last_name AS lname
FROM customers;

Aliases (AS) let you rename columns in the result set, making outputs easier to read or shortening long names.

9. Filter with Multiple Conditions

SELECT *
FROM orders
WHERE amount > 100 AND status = 'completed';

Combine conditions with AND or OR to refine results. Here, we’re finding only completed orders worth more than $100.

10. Combine Data with a JOIN

SELECT orders.id, customers.first_name, customers.last_name
FROM orders
JOIN customers ON orders.customer_id = customers.id;

The JOIN command lets you combine data from multiple tables—one of SQL’s most powerful features.

Final Thoughts

These 10 SQL query examples form the foundation for working with databases. Practice them until you’re comfortable, and you’ll be ready to tackle more advanced topics like grouping, aggregations, and subqueries.

FAQs About Learning SQL

1. Is SQL easy for beginners to learn?
Yes. SQL has a straightforward, English-like syntax, making it easier to pick up than many programming languages. Most beginners can write their first query within an hour.

2. What’s the best way to practice writing SQL queries?
The best way is hands-on practice with sample databases. Many courses and tools provide sandbox environments where you can try queries safely.

3. Do I need to know programming before learning SQL?
No. SQL is a query language, not a general-purpose programming language. You don’t need prior coding experience to start writing SQL queries.

4. What are the most important SQL commands for beginners?
Start with SELECT, FROM, WHERE, ORDER BY, COUNT, DISTINCT, and JOIN. These cover 80% of beginner use cases.

5. How long does it take to become good at SQL?
With consistent practice, most people feel comfortable with basic queries in 2–4 weeks. Becoming advanced (using joins, subqueries, window functions) usually takes a few months of real-world use.

👉 Build real-world database skills in our SQL Basics course.

https://teamtreehouse.com/tracks/beginning-sql

Leave a Reply

You must be logged in to post a comment.

You might also like other posts...

Learning to code can be fun!

Get started today with a free trial and discover why thousands of students are choosing Treehouse to learn about web development, design, and business.

Learn more