Level 1 project: a customer call list and a reorder list

To wrap up level 1 you build two lists that every business needs: a call list for the sales team and a reorder list for the warehouse. Each query combines several pieces you have already practiced one at a time.

You need: WHERE, AND and OR, IN and BETWEEN, IS NULL and IS NOT NULL, ORDER BY and LIMIT, and calculations with AS. You have practiced each of them on its own in level 1. This is the first time they come together.

The situation

The office supply shop from the exercises has two requests waiting. Sales wants to call long-standing customers today but only has time for five calls. And the warehouse needs a list of products to reorder, with what each order will cost.

Both lists come from a single table. The hard part is not a new command; it is putting the pieces in the right order and not forgetting a condition.

The project

Part 1: a call list for sales

You are looking for customers in two countries who have a phone number, oldest customers first, five at most. Read the task twice and jot down the conditions before you start typing.

Task Sales is calling long-standing customers today and has time for five calls. Find customers in the USA and the UK who have a phone number on file, oldest customers first (by customer_since), at most 5. Columns: customer (the name), city, phone and customer_since, in this order and with exactly these names.

customers 12 rows, 7 columns
customer_idnamecitycountrycustomer_sincesegmentphone
1Harbor Fremont PartnersBostonUSA2021-03-15Business617-555-0142
2Emily CarterChicagoUSA2022-07-01ConsumerNULL
3Riverside County OfficeRiversideUSA2020-11-20Government951-555-0110
4Raj PatelLeedsUK2023-01-09Consumer0113 496 0123
5Lakeview Hotel LtdManchesterUK2022-04-18Business0161 496 0456
6Olivia BrooksAustinUSA2024-02-27ConsumerNULL
7Greene & Shaw LLPLondonUK2021-09-05Business020 7946 0321
8Maple Grove High SchoolAustinUSA2023-08-14School512-555-0187
9Chloe EvansBostonUSA2025-05-30Consumer617-555-0199
10Northline Printing CoTorontoCanada2020-06-12Business416-555-0133
11Daniel KimSeattleUSA2024-10-03ConsumerNULL
12Westbrook Dental ClinicChicagoUSA2022-12-01Business312-555-0164

Part 2: a reorder list for the warehouse

Now there is math involved. The warehouse keeps at least 20 units of each product. Anything below that is topped up to 20, except furniture.

Task The warehouse keeps at least 20 units of every product in stock. Build the reorder list: all products with fewer than 20 units, except the Furniture category (the manufacturer ships those directly). Columns: product (the name), stock, reorder_qty (how many units are missing to reach 20) and reorder_cost (that quantity times the price). Sorted by reorder_cost, highest first.

products 12 rows, 5 columns
product_idnamecategorypricestock
1Copy paper, 500 sheetsPaper4.99420
2Blue ballpoint pens, 10-packWriting3.49310
3Lever arch file, grayFiling2.79185
4Comfort office chairFurniture18912
5Standing deskFurniture4495
6Black ink cartridgePrinter supplies24.964
7Yellow sticky notes, 12 padsWriting6.990
8Mono laser printer L30Electronics2198
927-inch monitorElectronics27915
10Notebook A5, ruledPaper5.49140
11Black toner XLPrinter supplies89.922
12Cross-cut shredderElectronics1290

What matters

The order of the clauses

A query always follows the same order: SELECT, FROM, WHERE, ORDER BY, LIMIT. Build it in that order too, and run it after every step. Start with SELECT and FROM, add the first condition, then the second, then the sort. After each step you can see whether the row count shrinks the way you expect.

Missing values need IS NULL

A missing phone number is NULL. phone <> NULL or phone != NULL is never true, not even for customers who do have a number. The condition you want is phone IS NOT NULL.

Sort first, then cut

LIMIT takes the first rows of the sorted result. Without ORDER BY you would get any five customers. With ORDER BY customer_since the longest-standing customers come first, because dates are stored as YYYY-MM-DD and therefore sort correctly as text.

Parentheses in the calculation

The reorder cost is the missing quantity times the price: (20 - stock) * price. Without parentheses SQL multiplies first and subtracts stock times price from 20. You can use the AS name in ORDER BY; in WHERE, write your conditions on the columns themselves.

If it doesn't work

  • Press Run and compare your result with the table above the editor. If a customer is missing or one too many shows up, the problem is in WHERE.
  • If a customer without a phone number is on the list, IS NOT NULL is missing. If Northline Printing from Toronto is at the top, the country condition is missing.
  • When you mix AND and OR, add parentheses: (country = 'USA' OR country = 'UK') AND phone IS NOT NULL. With IN you don't need them.
  • The countries are stored as USA and UK. 'US' finds nothing, and the check tells you that too few rows came back.
  • If the check says "The values are right, but the columns have other names", an AS is missing. The message lists the names it wants.
  • If the rows are right but in the wrong order, check ASC and DESC. The highest reorder cost belongs at the top.
  • Both parts are also checked against different data. Typing customer IDs or product names into the condition will fail for that reason.

What you have learned in level 1

Besides the pieces in this project, level 1 also covered LIKE for text patterns, such as every customer whose name contains "Ltd", and DISTINCT for lists without repeats. With those you can pull almost any list an office needs out of a single table. What is still missing are totals and counts across many rows, and queries over several tables. That is what the next levels are about.

FAQ

How many points is the project worth?

Each of the two project tasks counts three times as much as a single exercise, because you have to solve several steps at once.

Is the project free?

Yes. All of level 1 is free, including this project.

Is there only one correct answer?

No. The check looks at your result, not the wording of your query. country IN ('USA', 'UK') and two OR conditions in parentheses are equally good. Only IS NOT NULL, ORDER BY and LIMIT must appear, because that is exactly what the task is about.

What if I get stuck?

The hints under each task help in three steps: first the idea, then the structure, then an almost finished query. If you are still stuck, go back to the lesson for the piece that trips you up. The links are at the top of this page.

Level 1 complete. On to level 2: summarizing data

So far you have pulled rows out of tables. In level 2 you analyze them: count, add up and average, all the way to revenue per customer with GROUP BY. It starts with COUNT: how many customers, how many open orders, how many different cities?

Practice COUNT

Write to us

Choose language

Select your preferred language