Guide · Technology
SQL for Beginners: Learn to Query a Database With Real Examples
SQL is the language for asking questions of data, and it is one of the most useful skills to learn. This guide walks through SELECT, WHERE, GROUP BY, JOIN and more, with a small example database.
For
Beginners, analysts and developers who want to learn SQL and practise on a small example.
Level
Before you start
None. A free tool such as SQLite, DB Fiddle or any online SQL playground is enough.
On this page
Key takeaways
- A relational database stores data in tables of rows and columns. SQL lets you ask questions of those tables.
- Most queries use SELECT, FROM, WHERE, GROUP BY, ORDER BY and JOIN.
- Learn to read a query in order: FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY.
- A JOIN combines tables using a shared key such as customer_id.
- Practise on real, small datasets. Write queries yourself, then check your results.
Every app you use stores data somewhere: your orders, your messages, your bank transactions. Most of it sits in a database, and SQL (Structured Query Language, often said "sequel") is how you ask it questions.
SQL has been around for decades and is still asked about in analyst, developer and data interviews. It is also easier than it looks. A handful of commands covers most real work.
Tables, rows and columns
#A relational database stores data in tables. Each table has columns (the fields) and rows (the records).
We'll use a small shop with two tables.
customers
customer_id | name | city |
|---|---|---|
1 | Asha | Chennai |
2 | Ravi | Pune |
3 | Meena | Chennai |
4 | Kiran | Delhi |
orders
order_id | customer_id | amount | order_date |
|---|---|---|---|
101 | 1 | 1200 | 2026-08-02 |
102 | 2 | 450 | 2026-08-05 |
103 | 1 | 800 | 2026-08-12 |
104 | 3 | 2500 | 2026-09-01 |
105 | 3 | 300 | 2026-09-04 |
customer_id is a primary key in customers (unique per row) and a foreign key in orders (it points to a customer).
You can follow along in a free tool like SQLite or an online SQL playground by creating these tables.
Your first query: SELECT
#SELECT name, cityFROM customers;This returns the name and city of every customer. To get all columns:
SELECT *FROM customers;SELECT * is handy while exploring, but in real work, list the columns you need.
Filtering rows: WHERE
#SELECT nameFROM customersWHERE city = 'Chennai';Result: Asha and Meena.
You can combine conditions:
SELECT *FROM ordersWHERE amount > 500 AND order_date >= '2026-09-01';Useful operators:
=,<>(not equal),<,>,<=,>=AND,OR,NOTIN ('Chennai', 'Pune')BETWEEN 100 AND 1000LIKE 'A%'(starts with A)IS NULL,IS NOT NULL(use these for missing values, not= NULL)
Sorting and limiting
#SELECT *FROM ordersORDER BY amount DESCLIMIT 3;This returns the three largest orders. ASC is the default order, DESC is descending.
Calculations and aggregates
#Aggregate functions summarise many rows into one value:
SELECT COUNT(*) AS total_orders, SUM(amount) AS total_sales, AVG(amount) AS average_orderFROM orders;Common ones: COUNT, SUM, AVG, MIN, MAX. AS gives a column a readable name.
Grouping: GROUP BY
#To see totals per customer:
SELECT customer_id, COUNT(*) AS orders, SUM(amount) AS total_spentFROM ordersGROUP BY customer_id;Result:
customer_id | orders | total_spent |
|---|---|---|
1 | 2 | 2000 |
2 | 1 | 450 |
3 | 2 | 2800 |
Rule: every column in SELECT must either be in GROUP BY or inside an aggregate function.
Filtering groups: HAVING
#WHERE filters rows before grouping. HAVING filters groups after:
SELECT customer_id, SUM(amount) AS total_spentFROM ordersGROUP BY customer_idHAVING SUM(amount) > 1000;Combining tables: JOIN
#The orders table shows only customer_id. To show names, join the tables on the key:
SELECT c.name, o.order_id, o.amountFROM orders AS oJOIN customers AS c ON o.customer_id = c.customer_id;This is an INNER JOIN. It returns only rows that match in both tables.
LEFT JOIN
#Which customers have no orders? Use a LEFT JOIN, which keeps all rows from the left table:
SELECT c.name, o.order_idFROM customers AS cLEFT JOIN orders AS o ON c.customer_id = o.customer_idWHERE o.order_id IS NULL;Result: Kiran.
A quick guide:
- INNER JOIN: only matching rows
- LEFT JOIN: all rows from the left table, matches from the right (or NULL)
- RIGHT JOIN and FULL JOIN exist but are less common
Putting it together
#Total spend per customer, with names, highest first:
SELECT c.name, COUNT(o.order_id) AS orders, SUM(o.amount) AS total_spentFROM customers AS cJOIN orders AS o ON c.customer_id = o.customer_idGROUP BY c.nameORDER BY total_spent DESC;Read it in the order the database runs it: FROM and JOIN pick the data, WHERE filters rows, GROUP BY groups, HAVING filters groups, SELECT chooses columns, ORDER BY sorts, LIMIT trims.
Subqueries
#A query inside a query:
SELECT nameFROM customersWHERE customer_id IN ( SELECT customer_id FROM orders WHERE amount > 1000);This returns customers who have at least one order above 1,000. As you gain confidence, you can also use CTEs (WITH clauses) to make long queries easier to read.
Dates and text
#These functions differ between databases (SQLite, MySQL, PostgreSQL, SQL Server), so check yours:
- Extract a month from a date
- Concatenate strings
- Convert text to upper case
- Date differences
For example, in PostgreSQL: DATE_TRUNC('month', order_date). In SQLite: strftime('%Y-%m', order_date).
Changing data
#SQL also changes data. Be careful.
INSERT INTO customers (customer_id, name, city)VALUES (5, 'Divya', 'Bengaluru');UPDATE customersSET city = 'Mumbai'WHERE customer_id = 2;DELETE FROM ordersWHERE order_id = 105;Always use WHERE with UPDATE and DELETE. Without it, they affect every row. Test with a SELECT first, and never experiment on a live production database.
Common mistakes
#- Forgetting
WHEREon an update or delete - Using
= NULLinstead ofIS NULL - Joining on the wrong column and getting duplicates
- Mixing up
WHEREandHAVING - Selecting columns not in
GROUP BY - Not checking the number of rows before and after a join
When a query returns something unexpected, debug in small steps: run the pieces separately. See how to debug any bug.
How to practise
#- Install SQLite or use an online playground.
- Create your own small tables from something you know: expenses, a cricket tournament, a reading list.
- Use public datasets and practice sites such as SQLBolt, Mode's SQL tutorial, W3Schools SQL, HackerRank SQL and LeetCode database problems.
- Write the query yourself, then check against the answer.
- Explain queries aloud or in writing. See writing in public.
- Review key patterns with flashcards. See spaced repetition.
Where SQL fits
#- Data analyst work: see the data analyst roadmap.
- Python: connect to databases and run queries. See Python roadmap.
- Web apps: backends use databases. See how a website works.
- Cloud: managed databases are a core cloud service. See cloud computing explained.
AI tools can write SQL, but you must understand what it does. Check results on small examples. See using AI at work.
Frequently asked questions
#Is SQL a programming language?
#It is a query language for databases. It is declarative: you describe what you want, not how to get it.
Which database should I learn first?
#Any. SQLite is easy to start with, and PostgreSQL and MySQL are widely used. The basics are the same.
How long does it take to learn?
#The basics take a few weeks of practice. Writing efficient, complex queries takes months.
Is SQL still relevant with AI?
#Yes. Data still lives in databases, and people who understand SQL can check what AI-generated queries do.
What is the difference between SQL and NoSQL?
#SQL databases use tables and a fixed schema. NoSQL databases use other models, such as documents or key-value pairs. Start with SQL.
Syntax varies slightly between database systems. Check the documentation for the one you use.
Sources & further reading
Connected reading
References
- Writing in Public: How to Learn Faster and Be Found by Opportunities
- Spaced Repetition: Remember What You Study Without Re-reading It
- How to Debug Any Bug: A Calm, Repeatable Process
Show all 8 →
- Using AI Tools at Work Without Getting Into Trouble
- How to Learn Python as a Beginner: A Step-by-Step Roadmap
- How a Website Works: From Typing a URL to Seeing the Page
- Cloud Computing in Plain English: What It Is and Why Everyone Uses It
- Data Analyst Roadmap: Skills, Tools and Projects to Get Your First Job
Linked from 8 guides
Other pages in the library that point here.
Share this guide
Keep reading
More in Technology →Guide · Technology
Variables, Types, Operators and Arrays
Guide · Technology
Decisions, Loops, Methods and Scope
Guide · Career
A LinkedIn Profile That Gets Recruiter Messages

