Kodewick
Start learning →
First look at the data1 / 12

First look at the data

Lesson 1 of 12 · Part 1 · Know your data · about 20 minutes

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:

main.sql
1SELECT *2FROM orders3LIMIT 5;
Output
iduser_idorder_datestatusamount
122025-01-17paid36
212025-02-25cancelled22
3102025-03-13paid46
422025-03-14paid15
542025-03-14paid4
5 rows

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:

users
  • idPK
  • name
  • city
  • signup_date
  • channel
products
  • idPK
  • name
  • category
  • price
orders
  • idPK
  • user_idFK
  • order_date
  • status
  • amount
order_items
  • order_idPKFK
  • product_idPKFK
  • qty
  • price
events
  • idPK
  • user_idFK
  • day
  • action
The shop's 5 tables: an FK column holds an id from another table, and the arrow points to that table

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:

main.sql
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;
Output
tbln
users41
products12
orders135
order_items198
events210
5 rows
Choose one

Which column of the orders table records who placed the order?

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):

main.sql
1SELECT COUNT(*) AS orders, COUNT(DISTINCT user_id) AS buyers2FROM orders;
Output
ordersbuyers
13532
1 row

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 inTap one to fill a blank; tap a blank to take it back

Fill in one keyword to count how many different products were ever sold.

1SELECT COUNT( product_id) AS kinds2FROM order_items;
Choose oneA wrong guess is fine

What's the result?

1SELECT COUNT(DISTINCT city) AS cities2FROM users;

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:

main.sql
1SELECT MIN(order_date) AS first_order, MAX(order_date) AS last_order,2    MIN(amount) AS smallest, MAX(amount) AS biggest3FROM orders;
Output
first_orderlast_ordersmallestbiggest
2025-01-172025-12-260230
1 row

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.

Exercise 1

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.

It should print
first_signuplast_signup
2025-01-032025-10-22
1 row
exercise1.sql
1SELECT2FROM users;
Output
Press Run or Ctrl+Enter

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:

main.sql
1SELECT status, COUNT(*) AS n, SUM(amount) AS total2FROM orders3GROUP BY status;
Output
statusntotal
paid1156423
cancelled15718
refunded4191
test10
4 rows

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.

orders
statusntotal
paid1156423
cancelled15718
refunded4191
test10
WHERE status = 'paid'
result
statusntotal
paid1156423
cancelled15718
refunded4191
test10
For sales, keep only paid orders: cancelled, refunded and test orders don't count
Choose one

The boss asks, "How much did we sell this year?" How should you work it out?

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:

main.sql
1SELECT COUNT(*) AS users,2    COUNT(*) - COUNT(city) AS no_city,3    COUNT(*) - COUNT(channel) AS no_channel4FROM users;
Output
usersno_cityno_channel
4121
1 row

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.

Exercise 2

Who has no channel?

Find the users whose channel is empty. Show two columns: id and name.

It should print
idname
32Gil
1 row
exercise2.sql
1SELECT id, name2FROM users;
Output
Press Run or Ctrl+Enter

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:

main.sql
1SELECT name, city, COUNT(*) AS n2FROM users3GROUP BY name, city4HAVING COUNT(*) > 1;
Output
namecityn
ElenaLisbon2
1 row

Elena shows up twice. Look at the two rows:

main.sql
1SELECT id, name, city, signup_date2FROM users3WHERE name = 'Elena';
Output
idnamecitysignup_date
5ElenaLisbon2025-02-04
41ElenaLisbon2025-09-05
2 rows

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 in orderDrag, or use the arrows

Put these lines in order to make a query that runs: find users who placed more than one order on the same day.

  1. HAVING COUNT(*) > 1;
  2. GROUP BY user_id, order_date
  3. SELECT user_id, order_date, COUNT(*) AS n
  4. FROM orders
Should print
user_id | order_date | n

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, and COUNT(col) skips NULL.
  • On dates, MIN and MAX give 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(*) > 1 finds duplicates.
  • Whatever problems the check finds, say so in your report.
Practise what this lesson taught#041LEFT JOIN + IS NULLNever orderedStarter#044NOT EXISTSSigned up, never cameStarter#045HAVING SUM(…) = 0Only cancelledBasicAll 50 SQL problems →