First look at the data
Check the data before you analyse it: COUNT(*) counts rows, COUNT(DISTINCT col) counts different values, MIN/MAX show the range, COUNT(*) - COUNT(col) counts empty values, and GROUP BY … HAVING COUNT(*) > 1 finds duplicates.
You've just started at a small online shop, and the boss hands you some data: "Tell me how business went this year." Don't start calculating yet. The first step of any analysis is a health check on the data: how many rows, from which day to which day, what's empty, and is anything strange. Skip the check, and every number you work out later could be wrong.
First, look at the first few rows of the orders table:
1SELECT *2FROM orders3LIMIT 5;
| id | user_id | order_date | status | amount |
|---|---|---|---|---|
| 1 | 2 | 2025-01-17 | paid | 36 |
| 2 | 1 | 2025-02-25 | cancelled | 22 |
| 3 | 10 | 2025-03-13 | paid | 46 |
| 4 | 2 | 2025-03-14 | paid | 15 |
| 5 | 4 | 2025-03-14 | paid | 4 |
The shop's tables
In this step you get to know the data. The shop has 5 tables, and the whole Advanced course uses them:
idPKnamecitysignup_datechannel
idPKnamecategoryprice
idPKuser_idFKorder_datestatusamount
order_idPKFKproduct_idPKFKqtyprice
idPKuser_idFKdayaction
users holds the people who signed up, and channel says where they came from (search, ads, a friend, social media). orders holds the orders: status is the order's status and amount is how much it cost. order_items says what was in each order. events records what users did in the app; later lessons use it for retention and funnels.
The first thing to check: how many rows each table has. UNION ALL glues the results of several queries together, so you see them all at once:
1SELECT 'users' AS tbl, COUNT(*) AS n FROM users2UNION ALL SELECT 'products', COUNT(*) FROM products3UNION ALL SELECT 'orders', COUNT(*) FROM orders4UNION ALL SELECT 'order_items', COUNT(*) FROM order_items5UNION ALL SELECT 'events', COUNT(*) FROM events;
| tbl | n |
|---|---|
| users | 41 |
| products | 12 |
| orders | 135 |
| order_items | 198 |
| events | 210 |
Which column of the orders table records who placed the order?
user_id is a foreign key (FK): it holds the buyer's id from the users table. id is the order's own number, and amount is how much it cost.How many people actually bought something
In this step you count different values. COUNT(*) counts rows: 135 rows means 135 orders. But the boss often wants to know how many customers there are, and someone with 10 orders is still one customer. For that, use COUNT(DISTINCT col):
1SELECT COUNT(*) AS orders, COUNT(DISTINCT user_id) AS buyers2FROM orders;
| orders | buyers |
|---|---|
| 135 | 32 |
135 orders came from just 32 people. 41 people signed up, so 9 of them never ordered anything. That number alone is worth telling the boss.
Fill in one keyword to count how many different products were ever sold.
1SELECT COUNT( product_id) AS kinds2FROM order_items;
COUNT(DISTINCT product_id) counts each product id only once, however many times it was sold. Every one of the 12 products sold at least once.What's the result?
1SELECT COUNT(DISTINCT city) AS cities2FROM users;
COUNT(DISTINCT col) doesn't count NULL, so the two people with no city don't add an extra "empty" city.From which day to which day
In this step you check the range of the data. MIN and MAX don't only work on numbers. On dates they give the earliest and the latest:
1SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order,2 MIN(amount) AS smallest, MAX(amount) AS biggest3FROM orders;
| first_order | last_order | smallest | biggest |
|---|---|---|---|
| 2025-01-17 | 2025-12-26 | 0 | 230 |
The orders run from January to December: exactly one year. But the smallest amount is 0. An order that cost nothing? That looks suspicious, and the next step looks into it.
Tip: When you see a 0, a negative number, a huge number, or a date in the future, stop and check it. Real data is full of things like this.
First and last sign-up
✓Find the earliest and latest sign-up dates of the users, in two columns called first_signup and last_signup.
| first_signup | last_signup |
|---|---|
| 2025-01-03 | 2025-10-22 |
1SELECT2FROM users;
Odd values: a test order
In this step you look for data that shouldn't be there. Group by status and see how many orders each status has:
1SELECT status, COUNT(*) AS n, SUM(amount) AS total2FROM orders3GROUP BY status;
| status | n | total |
|---|---|---|
| paid | 115 | 6423 |
| cancelled | 15 | 718 |
| refunded | 4 | 191 |
| test | 1 | 0 |
Besides paid, cancelled and refunded, there's one test order: a colleague placed it before the shop went live and forgot to delete it. That's the order with an amount of 0.
So whenever you work out how much the shop sold, count only orders with status = 'paid'. If you simply add up the amount of every order, money from cancelled and refunded orders sneaks in, and the total comes out too high.
| status | n | total |
|---|---|---|
| paid | 115 | 6423 |
| cancelled | 15 | 718 |
| refunded | 4 | 191 |
| test | 1 | 0 |
WHERE status = 'paid'| status | n | total |
|---|---|---|
| paid | 115 | 6423 |
| cancelled | 15 | 718 |
| refunded | 4 | 191 |
| test | 1 | 0 |
The boss asks, "How much did we sell this year?" How should you work it out?
WHERE status = 'paid', then add up amount.Empty values: where are the NULLs?
In this step you look for empty cells. COUNT(*) counts every row, while COUNT(col) only counts rows where that column isn't NULL. Subtract one from the other and you get the number of empty cells:
1SELECT COUNT(*) AS users,2 COUNT(*) - COUNT(city) AS no_city,3 COUNT(*) - COUNT(channel) AS no_channel4FROM users;
| users | no_city | no_channel |
|---|---|---|
| 41 | 2 | 1 |
2 people didn't give a city, and 1 person has no channel. When you group by city, those two end up in a group of their own, NULL. When you count by channel, that one person isn't in any channel. Mention this in your report, so nobody thinks the data is complete.
Who has no channel?
✓Find the users whose channel is empty. Show two columns: id and name.
| id | name |
|---|---|
| 32 | Gil |
1SELECT id, name2FROM users;
Duplicates: one person, two sign-ups
In this step you look for duplicates. If two users have the same name and the same city, it's probably one person who signed up twice. Group by those two columns and keep the groups that appear more than once:
1SELECT name, city, COUNT(*) AS n2FROM users3GROUP BY name, city4HAVING COUNT(*) > 1;
| name | city | n |
|---|---|---|
| Elena | Lisbon | 2 |
Elena shows up twice. Look at the two rows:
1SELECT id, name, city, signup_date2FROM users3WHERE name = 'Elena';
| id | name | city | signup_date |
|---|---|---|---|
| 5 | Elena | Lisbon | 2025-02-04 |
| 41 | Elena | Lisbon | 2025-09-05 |
One signed up in February, the other in September, and the second account has never ordered anything. When you count users, she gets counted as two people. In a real job, you'd go and find out: is it the same person, or two people who just happen to share a name and a city?
Put these lines in order to make a query that runs: find users who placed more than one order on the same day.
HAVING COUNT(*) > 1;GROUP BY user_id, order_dateSELECT user_id, order_date, COUNT(*) AS nFROM orders
user_id | order_date | n
SELECT, FROM, GROUP BY, HAVING. The result has no rows at all, so nobody ordered twice in one day: the data passes this check.What you learned
- Check the data before you analyse it: rows, range, empty values, odd values and duplicates, one at a time.
COUNT(*)counts rows,COUNT(DISTINCT col)counts different values, andCOUNT(col)skipsNULL.- On dates,
MINandMAXgive the earliest and the latest. When you see a 0, a negative number or a huge number, check it first. - Sales count only paid orders: leave out cancelled, refunded and test orders.
GROUP BY … HAVING COUNT(*) > 1finds duplicates.- Whatever problems the check finds, say so in your report.