SQL for Data Analytics Guide
Selecting, aggregating, joins, left joins, CTEs, clause order, reliable habits, common mistakes and how NULL values behave.
9 programs
Choose the best training program according to your career.
Improve the performance of your business with our innovative Industrial Training Program.
Read More →
Our instructor-led training classes are designed to make you an expert.
Read More →
Real-life case studies are a great way to demonstrate your practical knowledge.
Read More →
Explore blogs, latest technologies and industry updates with practical examples.
Read More →Browse all available training programs and choose the right one for your career.
Read More →Know more about who we are and what we stand for.
Discover our mission to equip individuals with essential IT skills and industry expertise.
Read More →Learn, practice and plan your career — bilkul free.
Book a free video counselling session and pick the right course for you.
Read More →Download the SQL for data analytics guide PDF from Uncodemy: SELECT, joins, aggregation, CTEs and window functions, with tested examples.
This page offers Uncodemy's SQL for data analytics guide as a PDF download. It covers the SQL patterns analysts use every day: selecting and filtering, aggregating, joining tables, using CTEs and checking results. Examples use a small orders database so you can try them in SQLite, MySQL or PostgreSQL with minor changes.
The download below is its own file. It covers the everyday SQL patterns, safe joins, CTEs, reliable habits, common mistakes and NULL handling, with worked examples.
Selecting, aggregating, joins, left joins, CTEs, clause order, reliable habits, common mistakes and how NULL values behave.
Most business data lives in relational databases, and SQL is the language used to ask those databases questions. Even when the final result is a dashboard or a notebook, the data usually reaches it through a SQL query. Analysts who write SQL well can answer questions on their own, check numbers directly at the source and avoid waiting for someone else to produce a file. SQL is also relatively compact: a small number of ideas, applied carefully, cover the large majority of everyday analysis.
The examples below use three small tables. customers has customer_id, name and city. orders has order_id, customer_id, order_date, status and order_total. order_items has order_id, product, quantity and unit_price. Each customer can place many orders and each order can contain many items. Always start by understanding the grain of each table: here customers has one row per customer, orders one row per order and order_items one row per product line.
The basics: choose columns, filter rows and sort. Dates are stored as text in ISO format in this example.
SELECT order_id, customer_id, order_date, order_total
FROM orders
WHERE status <> 'cancelled'
AND order_date >= '2026-01-01'
ORDER BY order_total DESC
LIMIT 10;
Count orders and sum revenue by customer, keeping only customers with more than two orders.
SELECT customer_id,
COUNT(*) AS orders,
SUM(order_total) AS revenue
FROM orders
WHERE status <> 'cancelled'
GROUP BY customer_id
HAVING COUNT(*) > 2
ORDER BY revenue DESC;
A join connects rows from two tables using a matching column. An inner join keeps only rows that match on both sides. A left join keeps every row from the left table and fills missing matches with nulls, which makes it the right choice when you want to include customers with no orders. The biggest danger with joins is changing the number of rows without noticing. Joining orders to order_items repeats each order once per item, so summing order_total after that join overstates revenue. Before trusting a result, count rows before and after each join and make sure you understand why they changed.
Returns customers who never placed a non-cancelled order.
SELECT c.customer_id, c.name, c.city
FROM customers c
LEFT JOIN orders o
ON o.customer_id = c.customer_id
AND o.status <> 'cancelled'
WHERE o.order_id IS NULL;
A common table expression names an intermediate result, which makes long queries easier to read. Here we compute revenue per customer, then compare each customer with the average.
WITH customer_revenue AS (
SELECT customer_id, SUM(order_total) AS revenue
FROM orders
WHERE status <> 'cancelled'
GROUP BY customer_id
)
SELECT customer_id,
revenue,
ROUND(revenue - (SELECT AVG(revenue) FROM customer_revenue), 2) AS vs_average
FROM customer_revenue
ORDER BY revenue DESC;
| Written order | Clause | Purpose |
|---|---|---|
| 1 | SELECT | Choose columns and calculations |
| 2 | FROM / JOIN | Choose tables and how they connect |
| 3 | WHERE | Filter rows before grouping |
| 4 | GROUP BY | Collapse rows into groups |
| 5 | HAVING | Filter groups after aggregation |
| 6 | ORDER BY | Sort the result |
| 7 | LIMIT | Return only the first rows |
Create a small database of your own, or use a sample database, and write ten questions of increasing difficulty: counts, filters, grouping, joins, top N per group, running totals. Solve each, then explain the grain and the row count of your answer. Return to the same questions after a week and rewrite them more cleanly. Daily short practice beats long occasional sessions.
A valuable analyst skill is reading and understanding existing SQL. Start at the FROM clause to see the tables and joins, then read WHERE for the filters, then GROUP BY to find the grain of the result, and finally the SELECT list to see what is calculated. Run pieces separately: execute a CTE alone, check its rows and move on. Add comments as you learn what each part does. This approach lets you maintain a query written by a colleague without rewriting it from scratch.
| Situation | What happens | What to do |
|---|---|---|
| Compare with NULL using = | Never true | Use IS NULL or IS NOT NULL |
| Aggregate with NULLs | SUM and AVG ignore NULLs; COUNT(column) skips them, COUNT(*) does not | Decide whether to treat NULL as zero |
| Arithmetic with NULL | Result is NULL | Use COALESCE to give a default |
| Join on a NULL key | Rows with NULL keys do not match | Check for NULL keys before joining |
Ready to learn SQL for analysis with a trainer? Explore Uncodemy's Data Analytics course and learn from industry trainers.