SQL for beginners does not have to be intimidating. SQL (Structured Query Language) is how you ask a database questions, and about ten patterns cover most of what a junior data analyst does every day. This guide teaches all ten using one simple table.

The example table
Imagine a table called sales for a retailer with branches across Nigeria:
| order_id | order_date | branch | product | quantity | amount |
|---|---|---|---|---|---|
| 1001 | 2026-01-03 | Ikeja | Rice 50kg | 2 | 150000 |
| 1002 | 2026-01-03 | Wuse | Vegetable oil 5L | 5 | 60000 |
| 1003 | 2026-01-04 | Ikeja | Sugar 1kg | 20 | 30000 |
(The figures are made up for practice.) You can follow along free with PostgreSQL on your laptop or an online SQL editor.
1. SELECT: choose columns
SELECT order_date, branch, amount
FROM sales;
2. WHERE: filter rows
SELECT *
FROM sales
WHERE branch = 'Ikeja' AND amount > 50000;
3. ORDER BY and LIMIT: top results
SELECT product, amount
FROM sales
ORDER BY amount DESC
LIMIT 5;
4. Aggregate functions
SELECT COUNT(*) AS orders, SUM(amount) AS revenue, AVG(amount) AS avg_order
FROM sales;
5. GROUP BY: totals per group
SELECT branch, SUM(amount) AS revenue
FROM sales
GROUP BY branch
ORDER BY revenue DESC;
6. HAVING: filter groups
WHERE filters rows before grouping; HAVING filters after.
SELECT branch, SUM(amount) AS revenue
FROM sales
GROUP BY branch
HAVING SUM(amount) > 1000000;
7. JOIN: combine tables
If a branches table holds each branch’s state:
SELECT b.state, SUM(s.amount) AS revenue
FROM sales s
JOIN branches b ON b.branch = s.branch
GROUP BY b.state;
Check row counts before and after a join. Duplicate keys in the second table can multiply your totals.
8. CASE: create categories
SELECT order_id,
CASE WHEN amount >= 100000 THEN 'Large'
WHEN amount >= 20000 THEN 'Medium'
ELSE 'Small' END AS order_size
FROM sales;
9. Dates: monthly trends
SELECT DATE_TRUNC('month', order_date) AS month, SUM(amount) AS revenue
FROM sales
GROUP BY 1
ORDER BY 1;
Date functions differ slightly between databases (PostgreSQL, MySQL, SQL Server, BigQuery), so check your database’s documentation.
10. Subqueries and CTEs
A CTE (WITH clause) breaks a hard question into readable steps:
WITH branch_revenue AS (
SELECT branch, SUM(amount) AS revenue
FROM sales
GROUP BY branch
)
SELECT *
FROM branch_revenue
WHERE revenue > (SELECT AVG(revenue) FROM branch_revenue);
Common beginner mistakes
- Forgetting that NULL is not zero: use COALESCE(amount, 0) when summing
- Putting an aggregate in WHERE instead of HAVING
- Joining on columns with different spellings or extra spaces
- Not checking results against a quick Excel total
What to learn next
Once these feel natural, learn SQL window functions for rankings and running totals, and practise answering real questions. SQL is module 2 of the Datamac data analytics internship.
FAQs
How long does it take to learn SQL for beginners?
Most people can write useful queries within a few weeks of regular practice. Getting fast and confident takes a few months.
Which SQL database should I learn?
PostgreSQL is free and widely used. The core SQL you learn works in MySQL, SQL Server and BigQuery with small differences.
Do data analysts use SQL every day?
In most analyst roles, yes. It is often the first technical test in interviews.
Keep reading
- SQL Window Functions Explained With Simple Examples
- How to Become a Data Analyst in Nigeria With No Experience
- VLOOKUP vs XLOOKUP: Which Should You Use in 2026?
Learn this live with Datamac
Datamac’s 12-month data analytics internship teaches Excel, SQL, Python, Power BI, Tableau and machine learning in live online classes twice a week, with real Nigerian datasets, two capstones and career support.
Apply for the internship · Compare courses · Ask a question on WhatsApp


