SQL for Beginners: Write Your First Query in 30 Minutes
Never written SQL before? This beginner guide takes you from zero to writing real SELECT, WHERE, JOIN, and GROUP BY queries in 30 minutes – with practical examples on tables you might actually work with on the job.
SQL is the most valuable skill a data analyst, business analyst, or aspiring data engineer can learn.
It is also one of the fastest to get started with. You do not need to set up a local database, install anything, or spend weeks reading documentation. In 30 minutes, you can write queries that answer real business questions.
This guide takes you from zero to your first real queries — step by step.
1. What is SQL and why does it matter?
SQL (Structured Query Language) is how you talk to a database.
Databases store data in tables — like spreadsheets, but much more powerful. SQL lets you:
- Retrieve specific data from massive tables
- Filter, sort, and aggregate data in any way you need
- Combine data from multiple tables
- Answer business questions like "which products drove the most revenue last month?"
Every company that stores data uses SQL somewhere. It is the lingua franca of data work.
2. Understanding tables
A table is like a spreadsheet with rows and columns.
Example customers table:
| customer_id | name | country | status | signup_date |
|---|---|---|---|---|
| 1 | Priya Sharma | IN | active | 2024-01-15 |
| 2 | James Liu | US | active | 2024-02-03 |
| 3 | Sara Ahmed | AE | inactive | 2023-11-20 |
- Each column is a field (customer_id, name, country...)
- Each row is one record (one customer)
- The primary key (
customer_id) uniquely identifies each row
Most databases have many tables that are linked together by shared keys.
3. Your first query: SELECT
SELECT retrieves data from a table.
sqlSELECT * FROM customers;
This returns every column and every row from the customers table. The * means "all columns."
To select specific columns:
sqlSELECT name, country, status FROM customers;
Style tip: write SQL keywords in uppercase (
SELECT,FROM,WHERE) and table/column names in lowercase. It is not required, but it makes queries much easier to read.
4. Filtering with WHERE
WHERE filters which rows are returned.
sqlSELECT name, country FROM customers WHERE country = 'IN';
Returns only customers from India.
You can combine conditions with AND and OR:
sqlSELECT name, country, status FROM customers WHERE country = 'IN' AND status = 'active';
Returns active customers from India only.
Common comparison operators:
=equals<>or!=not equal>greater than<less than>=greater than or equalBETWEEN x AND ywithin a rangeIN (val1, val2)matches any value in the listLIKE 'pattern%'matches a text pattern
sql-- Customers from India or the US SELECT name, country FROM customers WHERE country IN ('IN', 'US'); -- Customers whose name starts with "P" SELECT name FROM customers WHERE name LIKE 'P%';
5. Sorting with ORDER BY
ORDER BY sorts the result:
sqlSELECT name, signup_date FROM customers ORDER BY signup_date DESC;
DESC means newest first. ASC (the default) means oldest first.
You can sort by multiple columns:
sqlSELECT name, country, signup_date FROM customers ORDER BY country ASC, signup_date DESC;
6. Limiting results with LIMIT
LIMIT caps the number of rows returned:
sqlSELECT name, signup_date FROM customers ORDER BY signup_date DESC LIMIT 10;
Returns the 10 most recently signed-up customers. This is useful when you just want a quick look at the data.
7. Counting and aggregating with GROUP BY
So far you have been returning raw rows. Now you can start summarizing.
COUNT — how many rows?
sqlSELECT COUNT(*) AS total_customers FROM customers;
GROUP BY — count per group
sqlSELECT country, COUNT(*) AS customer_count FROM customers GROUP BY country ORDER BY customer_count DESC;
Returns one row per country, with how many customers are in each.
SUM and AVG
sqlSELECT DATE_TRUNC('month', order_date) AS month, COUNT(*) AS total_orders, SUM(amount) AS total_revenue, AVG(amount) AS avg_order_value FROM orders GROUP BY DATE_TRUNC('month', order_date) ORDER BY month;
This is the kind of query you would write in a real analytics job.
8. Joining two tables
Most real-world questions require data from more than one table.
Say you have:
customers— one row per customerorders— one row per order (each order belongs to a customer)
They are linked by customer_id.
sqlSELECT c.name, c.country, o.order_id, o.amount FROM customers c JOIN orders o ON c.customer_id = o.customer_id;
The c and o are aliases — short names for the tables so you do not have to repeat the full name.
JOIN (also called INNER JOIN) returns only rows where there is a match on both sides.
LEFT JOIN keeps all customers even if they have no orders:
sqlSELECT c.name, COUNT(o.order_id) AS total_orders FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.name;
Customers with no orders appear with total_orders = 0 rather than disappearing from the result.
9. Putting it all together
Here is a query that combines everything you have learned:
"Find the top 5 countries by total revenue from active customers in 2024."
sqlSELECT c.country, SUM(o.amount) AS total_revenue FROM customers c JOIN orders o ON c.customer_id = o.customer_id WHERE c.status = 'active' AND o.order_date >= '2024-01-01' AND o.order_date < '2025-01-01' GROUP BY c.country ORDER BY total_revenue DESC LIMIT 5;
You can now read and write this kind of query.
10. The 5 SQL concepts you need first
If you are just getting started, focus on mastering these in order:
- SELECT + FROM — retrieve data from a table
- WHERE — filter rows
- GROUP BY + aggregates — summarize data
- JOIN — combine tables
- ORDER BY + LIMIT — sort and cap results
Everything else — window functions, CTEs, subqueries, indexes — builds on top of these five.
11. How to practice SQL effectively
Reading examples is a start, but you will only truly learn SQL by writing queries yourself.
The most effective practice approach:
- Start with a real schema — not a toy table with 5 rows.
- Read the business question in plain English first.
- Identify which tables and columns you need.
- Write the query step by step —
FROM, thenJOIN, thenWHERE, thenGROUP BY, thenSELECT. - Run it, check the results, and adjust.
TheQueryLab is built exactly for this. You get:
- Realistic schemas from actual business scenarios
- Problems ordered from beginner to advanced
- An in-browser SQL editor so you can practice without setting anything up
If you are new to SQL, start with the SQL Interview Preparation Kit — the first set of problems are beginner-friendly and walk you through the core patterns before ramping up.
The fastest path to SQL confidence is writing real queries on real-looking data. Start today.
