Lab 7 · Week 7
SQL: Asking the Database a Question
Every number you have worked with this quarter came to you in a spreadsheet that somebody exported for you. Today you go to where the numbers actually live and ask for them yourself. You will run six short queries in SQL, the language every database speaks, and draw two more with your mouse. Every one is printed for you, most are one line, and by the end you will have asked a question that no report at the co-op was built to answer. That is the whole lab: not to make you a database person, but to show you that you can get an answer from the source when you need one.
Wanna go analog?or save it as a PDF; the navigation drops out
SQL error messages are terse, and asking an AI to explain one is exactly the right use: paste the error and your query, understand the explanation, then fix it yourself. Asking it to write the checkpoint queries from scratch is different; in Week 7 the syntax is still the thing you are building.
A query is a question you ask a database, written in a language called SQL (say "sequel" or spell it out; both are fine). It is a sentence with a fixed word order, and here is a complete one with each line labeled:
Three things to know about every query. It always comes back as a table, even if that table is one number. It never changes the data; asking is not editing. And the word order is fixed: SELECT, then FROM, then WHERE, the same every time, which is why after three or four of them the shape stops looking strange. Once the sandbox below has loaded, type that exact query into it and press Run. Two rows come back, Spokane and Boise. You have just asked a database a question.
stores(store_id, store_name, region, opened_year, square_feet) ·
products(product_id, product_name, category, supplier_id, unit_cost, list_price) ·
sales(sale_id, sale_date, store_id, product_id, member_id, quantity, unit_price)
These are the same sales you have been analyzing since Lab 2, before anyone exported them to Excel.
Here is how this works. Each checkpoint sits directly under the work that produces it. You do the actual work in your own file, then paste the answer here. Get it right and the next question opens up. There is no penalty for a wrong answer, so guess, check, and lean on the hint if you get stuck. That is what it is there for.
Once you have cleared them all, write your reflection and download your certificate for Canvas. Your progress saves automatically in this browser, so you can close the tab and pick up where you left off. It does not follow you to a different computer or a private window, and clearing your browser data clears it. If you need to start this lab over, use the button.
Part 1Why the data is not in Excel
Everything you have touched this quarter has been a spreadsheet, so it is worth saying plainly: no real company keeps its operating data in Excel. Cascadia's registers do not write to a workbook. They write to a database, and the difference is not cosmetic.
A spreadsheet is one grid, edited by one person at a time, that tops out around a million rows and lives in a file someone can move, break, or email around. A relational database is different on every count. It is a set of tables living on a server. Hundreds of registers and websites write to it at the same moment. It holds effectively unlimited rows. And you read it by asking questions in SQL, not by scrolling.
The word relational is the important one. Look at the schema box above: the sales table does not contain store names. It contains a store_id, a number pointing at one row of the stores table, where the name, region, and square footage live exactly once. That pointer is called a foreign key, and the column it points at, the one that uniquely identifies each row, is a primary key.
Why build it that way? Because facts should live in one place. If Bellingham's name were spelled out on all of its sales rows and the co-op renamed the store, someone would have to fix thousands of rows and would miss some. Storing each fact once and pointing at it is how a database stays right. Monday's lecture calls this normalization; today you get to see what it looks like from the inside.
Excel is where analysis happens. A database is where the truth lives. The skill this lab teaches is going to the truth directly.
ToolThe sandbox: where every query in this lab gets typed
The dark box below is not a checkpoint and nothing you type in it is graded. It is your workbench. Every step in Part 2 says "type this into the sandbox"; this is the sandbox. Press Dock and it follows you down the page, so you never have to scroll back up to it.
Loading database…
The sandbox is docked. Press Undock on it to put it back here.
From: Controller ·
Subject: Five quick questions, no report exists for them
"Our reporting person is out this week and the board meets Thursday. I have five questions that no
existing report answers, and I am told anyone with access to the database can get them in a few minutes.
Please try:
1. How many sales did we ring up in 2025, in total?
2. How many of those were at Bellingham?
3. What is the most expensive product we sell?
4. What was our best store's total revenue for the year?
5. How many sales did the Inland region ring up? (I know the sales export does not say which region;
that is exactly why I cannot get this from a spreadsheet.)
And one more, if you get that far: which store has the most sales with no member attached. The board
keeps asking about Bellingham, and I want the answer from the source this time."
The SQL cheat sheet: every word this lab uses, plus a few more, all runnable
Every example below runs in the sandbox as written. The four groups are the order the steps use them in. Come back here whenever a word looks unfamiliar.
1. Getting data out: SELECT and FROM. Every query starts with these two. The star means all columns; naming columns keeps the result narrow; DISTINCT means each value once.
SELECT * FROM stores; SELECT store_name, region FROM stores; SELECT DISTINCT category FROM products;
2. Keeping some rows: WHERE. Compare with =, >, <, and <> (not equal). LIKE matches a pattern, where % means "anything here." AND needs both conditions true; OR needs either. Text goes in single quotes; numbers do not.
SELECT product_name, list_price FROM products WHERE list_price < 30; SELECT product_name FROM products WHERE product_name LIKE 'Ridgeline%'; SELECT COUNT(*) FROM sales WHERE store_id = 4 AND quantity > 2; SELECT COUNT(*) FROM sales WHERE store_id = 5 OR store_id = 8;
3. Sorting and stopping: ORDER BY and LIMIT. ORDER BY sorts smallest first unless you add DESC. LIMIT stops after that many rows, and it always goes last.
SELECT product_name, list_price FROM products ORDER BY list_price DESC LIMIT 5;
4. Adding things up: COUNT, SUM, AVG, MIN, MAX, and GROUP BY. On their own, these turn a whole table into one number. Add GROUP BY and you get one number per group, which is a pivot table in words.
SELECT AVG(list_price) FROM products; SELECT MIN(list_price), MAX(list_price) FROM products; SELECT category, COUNT(*) FROM products GROUP BY category; SELECT store_id, SUM(quantity * unit_price) FROM sales GROUP BY store_id;
Then JOIN, which Steps 5 and 6 teach on their own, because it is the one idea here with no spreadsheet equivalent.
Part 2Follow along: the GM's questions, one query at a time
Every query is printed for you. Type it into the sandbox (typing, not pasting, is how the words get into your hands), press Run, and enter what comes back. Each step adds one word to what you already know. Seven steps, and one of them is clicking rather than typing.
Start by looking at a whole table. The stores table is small enough to see at once:
SELECT * FROM stores;
The star means every column. Eight rows come back, one per store, and you can see the store_id number that the sales table uses instead of the name. Now look at sales, which is not small, so ask for the first ten rows only:
SELECT * FROM sales LIMIT 10;
Then count them all. COUNT(*) means count the rows:
SELECT COUNT(*) FROM sales;
That is the GM's first question, and you answered it without opening a spreadsheet.
WHERE keeps only the rows that meet a condition. Bellingham is store_id 4 (you saw that in Step 1), so:
SELECT COUNT(*) FROM sales WHERE store_id = 4;
Change one thing and you have a different question. Try store_id = 1
for Seattle, or quantity > 2 for the larger sales, or both at once with
WHERE store_id = 4 AND quantity > 2. Each is one edit and one Run.
Instead of every column, name the ones you want, and sort them. DESC means largest first:
SELECT product_name, list_price FROM products ORDER BY list_price DESC LIMIT 5;
The top row is the co-op's most expensive product. Take off DESC and you get the cheapest five
instead. Or ask for one number rather than a list: SELECT MAX(list_price) FROM
products; gives you the same top price with no sorting at all, and MIN and AVG work the same
way.
This is the pivot table, in words. GROUP BY collects the rows for each store into one line, and SUM adds up something for each group. Revenue is quantity times price:
SELECT store_id, ROUND(SUM(quantity * unit_price), 2) AS revenue FROM sales GROUP BY store_id ORDER BY revenue DESC;
Eight rows, one per store, largest first. AS revenue just gives the new column a name. You built this same table in Lab 5 with a pivot and in Lab 6 with a bar chart. The numbers should look familiar.
Step 4 gave you store_id 1, 6, 2. The names live in a different table. Getting them means connecting two tables, which is the one idea in SQL that has no Excel equivalent, so look at it before you type it.
Open the Join Builder in a new tab. It shows the
co-op's tables as boxes. Click the store_id column in sales, then the store_id column in stores. An
arrow appears between them, and in the panel below the boxes, one line of SQL appears with it,
highlighted: JOIN stores ON sales.store_id = stores.store_id. The arrow and
that line are the same thing. Read the rows total above the result table; that number is
Checkpoint 5.
Then click Start over and connect sales to members instead. Read the rows total again. It is smaller, and the difference matters: a JOIN keeps only the rows that found a match, and a sale with no member has no members row to match. The rows that vanished are the non-member sales. That number is Checkpoint 6, and it is the same 405 sales, 168 of them at one store, that Lab 5 found. The Join Builder has a Back to Lab 7 button when you are done.
Here is the Join Builder's line, typed by you. Everything else in this query you have already used:
SELECT stores.store_name, sales.sale_date, sales.quantity FROM sales JOIN stores ON sales.store_id = stores.store_id LIMIT 10;
Ten sales rows, each with its store's name attached. Because two tables are in play, every column is written as table.column so the database knows which one you mean. Now the payoff: with stores joined in, you can ask sales a question about region, a column that sales does not have. Change the query to count instead of show, and add one WHERE line:
SELECT COUNT(*) FROM sales JOIN stores ON sales.store_id = stores.store_id WHERE stores.region = 'Inland';
That is the GM's fifth question, and no spreadsheet the co-op exports can answer it, because the region lives in a different table from the sales. JOIN is the reason the data is stored this way.
The Join Builder showed you that 405 sales have no member attached. The GM wants to know where they are. This last query has no new words in it. Every line comes from a step you have already done:
SELECT stores.store_name, COUNT(*) AS no_member FROM sales JOIN stores ON sales.store_id = stores.store_id WHERE sales.member_id IS NULL GROUP BY stores.store_name ORDER BY no_member DESC;
- JOIN stores ON is Step 6, so the store has a name.
- WHERE sales.member_id IS NULL is Step 2's WHERE. IS NULL means "is blank"; a sale with no member has a blank member_id.
- GROUP BY is Step 4, one line per store.
- COUNT(*) AS no_member is Step 1's count, given a name.
- ORDER BY … DESC is Step 3, biggest first.
Read the top row. You have now found this store three ways: a pivot table in Lab 5, a chart in Lab 6, and a query today. The tool kept changing and the fact did not, which is what it means for a finding to be real. And this time nobody exported anything for you.
If you finish early. Change two words in that last query: replace both
stores.store_name with stores.region. Now the
same question is asked by region. Does the result tell the board anything the store version did not?
No checkpoint; use it in your reflection if you like.
One week is enough to start. If this lab clicked, the optional SQL for Business bootcamp takes you through joins, grouping, dates, CTEs, and window functions on this same database, for extra credit and a certificate. Finish this lab first.
FinishReflection & certificate
AI could coach you earlier in this lab. Not here. Write the reflection below yourself. Do not ask an AI tool to write it, rewrite it, or clean it up.
This is the one part of the lab that shows your lab professor how you think. Rough is fine and typos are fine. It just has to be yours.
One last thing before your certificate
You just answered six questions from the source, including one that no report at the co-op was built to answer, and you did it in six short queries. Think about the job you are heading toward. What is one question you would want to ask the data directly rather than wait for someone to build you a report? Who owns that database today, and what would change if you could ask it yourself?
Fifty words is the minimum, but do not write to the counter. This prints as page 2 of your certificate, so it is the part your lab professor actually reads. Answer it like someone asked you the question out loud.
0 words
All checkpoints cleared
Put your name in exactly as it appears in Canvas, then download your certificate. The code on it is generated from your name, so it is yours and nobody else's.
CanvasWhat to submit
Go to the Lab 7 assignment in Canvas and upload two files.
File 1: your queries. A plain text file or Word doc named
LAB7_Last_First, containing your six working queries, one for each checkpoint that asked you to type one (Checkpoints 5 and 6 came from the Join Builder and need no query),
numbered so we can match them up. Paste the query text, not screenshots.
File 2: your completion certificate PDF. Clear all the checkpoints above, write your reflection, and click Download certificate. Your reflection prints as page 2, so you do not submit it separately.