Programming & Web Development · Guide
10 SQL queries every beginner should know
Ten patterns that cover most everyday questions, each with the query and what it is for. Learn these and you can answer real questions without asking anyone.
The Nextversity teamProgramming & Web Development schoolUpdated August 10, 20266 min read

On this page
The short answer
Ten query patterns cover most of what a beginner needs. Learn these and you can answer real questions without waiting for someone else's report.
Each one below has the query and, more importantly, what it is for.
1. See what is in a table
SELECT * FROM customers LIMIT 10;
The asterisk means every column, and LIMIT stops the database handing you a million rows. This is the first thing to run against any unfamiliar table.
2. Pick specific columns
SELECT name, email, city FROM customers;
Once you know what is there, ask for what you need. Selecting fewer columns is faster and much easier to read.
3. Filter rows
SELECT name, city
FROM customers
WHERE country = 'Australia';
WHERE is where most of the thinking happens. Combine conditions with AND and OR, and use brackets when you mix them, because precedence surprises people.
4. Filter on a range or a list
SELECT * FROM orders
WHERE order_date >= '2026-01-01'
AND total BETWEEN 50 AND 200
AND status IN ('paid', 'shipped');
BETWEEN is inclusive at both ends. IN is much cleaner than five ORs.
5. Search text
SELECT name FROM customers
WHERE name LIKE 'Sm%';
% matches any number of characters, _ matches exactly one. Be aware that a leading % prevents the database using an index, which matters on big tables.
6. Sort the result
SELECT name, total
FROM orders
ORDER BY total DESC
LIMIT 10;
ORDER BY plus LIMIT is the "top ten" pattern, and you will use it constantly.
7. Count things
SELECT COUNT(*) FROM orders;
SELECT COUNT(DISTINCT customer_id) FROM orders;
The second one answers "how many different customers ordered", which is a genuinely different question from "how many orders", and mixing them up is a classic reporting error.
8. Group and summarize
SELECT country, COUNT(*) AS customers, AVG(total) AS avg_order
FROM orders
GROUP BY country
ORDER BY customers DESC;
This is the SQL version of a pivot table. Every column in the SELECT must either be in the GROUP BY or wrapped in an aggregate function such as COUNT, SUM, AVG, MIN or MAX.
9. Filter the groups
SELECT customer_id, COUNT(*) AS orders
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 5;
WHERE filters rows before grouping. HAVING filters groups after. If the condition involves an aggregate, it goes in HAVING.
10. Join two tables
SELECT c.name, o.order_date, o.total
FROM customers AS c
INNER JOIN orders AS o ON o.customer_id = c.id
WHERE o.order_date >= '2026-01-01';
This is the one that makes SQL powerful. INNER JOIN keeps only rows with a match in both tables. LEFT JOIN keeps everything from the first table and fills in nulls where there is no match, which is how you find customers with no orders:
SELECT c.name
FROM customers AS c
LEFT JOIN orders AS o ON o.customer_id = c.id
WHERE o.id IS NULL;
Joins are where SQL clicks. Everything before them is a filtered list. Everything after them is analysis.
Three habits worth forming early
- Read the query out loud. SQL is close enough to English that a query which does not read sensibly is usually wrong.
- Run it with LIMIT first. Especially on a table you do not know.
- Never run an UPDATE or DELETE without a WHERE. Write the SELECT first, check the rows, then change the verb. Everyone learns this once, and some people learn it the expensive way.
The PostgreSQL tutorial is a solid free reference, and SQLite lets you practice with no installation at all.
What comes after these ten
Subqueries, common table expressions, window functions, and understanding indexes well enough to know why a query is slow. None of them are beginner material, and all of them are easier once the ten above are automatic.
The SQL certificate covers the fundamentals with practice, the advanced SQL certificate covers the rest, and if you are coming from spreadsheets there is a comparison of SQL and Excel worth reading first.
One subscription opens the whole Programming & Web Development school.
Ten patterns. Most working analysts use them every day.
Questions people ask
What SQL should a beginner learn first?
SELECT, WHERE and ORDER BY, then COUNT with GROUP BY, then INNER JOIN. Those five cover a surprising share of everyday questions before you need anything more advanced.
What is the difference between WHERE and HAVING?
WHERE filters individual rows before grouping. HAVING filters the groups afterwards. If your condition uses an aggregate like COUNT, it belongs in HAVING.
What is a JOIN in SQL?
A way to combine rows from two tables using a shared value, usually an id. INNER JOIN keeps only rows that match in both, LEFT JOIN keeps everything from the first table whether or not it matches.
Why does my query return duplicate rows?
Usually a join that matches more rows than you expected, such as one customer with several orders. Check what you are joining on, and use DISTINCT or an aggregate if you genuinely want one row per item.
Do I need to write SQL in capitals?
No, SQL is not case sensitive for keywords. Capitalizing them is a widely used convention because it makes the structure easier to read at a glance.