…
…
Find empty fields, impossible values and duplicate records in a customer table from an old system, and give every row a quality label with CASE.
The Plant Pot is a small online store that sells plants. Its customer list has lived in an old system for years, sometimes filled in by hand, and now it will move to a new system. Before the move, the team asks you for a quality audit: which fields are empty, which values cannot be real, who signed up twice, and what is the state of every row? In seven steps you will answer these questions, and at the end you will produce a summary city by city.
Data: one table, fictional sample data.
customer (60 rows): id, first_name, last_name, email, phone, city, birth_year, signup_date, points (loyalty points). email, phone, city and birth_year can be empty (NULL); points is filled in every row.Lessons used: 3–9 (selecting columns, filtering, IN and BETWEEN, sorting, NULL and COALESCE, CASE, order of evaluation and silent errors) and 11–13 (aggregate functions and conditional counting, GROUP BY, finding duplicates with HAVING). No new concept.
Most cells have only comment lines and ___: you will write the query. The first line starts with "Why this query:"; finish it in one sentence before you write the query (it is not checked, it just gathers your thoughts). Every step only reads; you can rerun the cells as often as you like. If you want to see the table, you can start with SELECT * FROM customer;.
What you'll do
customer.sqlDownload file
Reading file…
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
The Plant Pot wants to know how much information is missing before it moves its customer list to the new system. The first question is simple: how many customer rows are in the list, and how many of each field are filled?
Return a one-row summary: total (all rows), email_filled, phone_filled, birth_year_filled and city_filled. You can read how many rows are empty in a field from the difference between total and that field's filled count.
This step's code cell is for members.Sign up free
The marketing team wants the customers who have neither an email nor a phone number; these people cannot be reached in any way. An intern wrote the query below. It raises no error but returns no rows. Given the counts from step 1, is that possible?
Run the query, find out why it comes back empty and fix it. Keep the same columns: id, first_name, last_name
Code cells are for members
You can read the steps. Sign up free to run the code, complete the steps and save your progress.
cityidThis step's code cell is for members.Sign up free
In the old system, birth year and loyalty points were typed by hand; some values cannot be real. The team set this rule: birth year must be between 1940 and 2010 (both ends included), and points cannot be negative.
Return the id, first_name, last_name, birth_year and points of the customers who break the rule; order by id. Customers with an empty birth year must not appear in this list: an empty value is not an error, it is missing information.
This step's code cell is for members.Sign up free
In the new system the email address will be unique for each customer. Before moving, you need to find the places where the same email appears in more than one row.
Return the email addresses that appear in more than one row and how many rows they appear in (times). The most repeated first; ties ordered by email.
Note: if you see a row with an empty email in your result, that is not a duplicate. In step 1 you saw how many customers have an empty email; grouping puts all the empty ones into a single group.
This step's code cell is for members.Sign up free
Step 4 told you which addresses repeat, but not whom those rows belong to. Is every repeat a copy to delete? Before you decide, you have to look at the rows themselves.
Return all rows that have the emails you found in step 4: id, first_name, last_name, email, phone, signup_date. Sort by email first and, within the same email, by signup date; this way the rows of each address sit one below the other, oldest to newest.
Think (not measured): Are all these duplicates the same person registered twice? Which one is different, and what would you suggest for these customers in the new system?
This step's code cell is for members.Sign up free
The migration team wants to see one quality label next to every customer row (status). A row can have several problems; then the most serious one is written. The order of severity is:
1. If both email and phone are empty: Unreachable
2. Otherwise, if the birth year is outside 1940–2010 or the points are negative: Invalid value
3. Otherwise, if one of email, phone or birth year is empty: Incomplete
4. If none of these: Clean
Return the id, first_name, last_name and status of every customer; order by id. Think about whether the order in which you write the conditions changes the result.
This step's code cell is for members.Sign up free
Finally the team wants to see how customers with missing contact details are spread across cities; each city's store manager will get their own list completed.
For every city return city, the number of customers (customers) and the number of customers whose email or phone is empty (missing_contact). Customers with an empty city should appear too, but with Unknown instead of the city name. The city with the most missing comes first; ties are ordered by city name.
This step's code cell is for members.Sign up free