Product Sales Analysis II
Problem
Table: Sales
+-------------+-------+
| Column Name | Type |
+-------------+-------+
| sale_id | int |
| product_id | int |
| year | int |
| quantity | int |
| price | int |
+-------------+-------+
(sale_id, year) is the primary key (combination of columns with unique values) of this table.
product_id is a foreign key (reference column) to Product table.
Each row of this table shows a sale on the product product_id in a certain year.
Note that the price is per unit.
Table: Product
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| product_id | int |
| product_name | varchar |
+--------------+---------+
product_id is the primary key (column with unique values) of this table.
Each row of this table indicates the product name of each product.
Problem Definition
Write a solution that reports the total quantity sold for every product id.
Example
Output
Try It Yourself
-- TODO: Write your user queries here
Solution
We can simply select the product_id and calculate the sum of the quantity column as total_quantity from the Sales table. Then, we group the results by product_id.
SELECT product_id, Sum(quantity) AS total_quantity FROM Sales GROUP BY product_id
Let's break down the query step by step:
Step 1: Select fields
SELECT product_id, Sum(quantity) AS total_quantity FROM Sales
The SELECT clause specifies the columns that will be included in the result set. In this case, it selects the product_id and the sum of the quantity for each product.
Output After Step 1:
+------------+----------------+ | product_id | total_quantity | +------------+----------------+ | 100 | 22 | | 200 | 15 | +------------+----------------+
Step 2: GROUP BY product_id:
GROUP BY product_id
The GROUP BY clause is used to group the results by a specific column or columns. In this query, it groups the sales data by the product_id.
The SUM(quantity) function will then be applied to each group separately, calculating the total quantity for each distinct product_id.
Final Output:
+--------------+----------------+ | product_id | total_quantity | +--------------+----------------+ | 100 | 22 | | 200 | 15 | +--------------+----------------+
🤖 Don't fully get this? Learn it with Claude
Stuck on Product Sales Analysis II? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **Product Sales Analysis II** (Databases) and want to truly understand it. Explain Product Sales Analysis II from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **Product Sales Analysis II** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **Product Sales Analysis II** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **Product Sales Analysis II** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.