Product Sales Analysis I
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 to report the product_name, year, and price for each sale_id in the Sales table. Also, return the output in the order of year.
Example
Output
Try It Yourself
-- TODO: Write your user queries here
Solution
We can utilize SQL query that uses the INNER JOIN clause to retrieve information from two tables, namely "Sales" and "Product." This type of query is used when you want to combine rows from both tables based on a related column.
SELECT product_name, year, price FROM Sales INNER JOIN Product ON Sales.product_id = Product.product_id ORDER BY year;
Let's break down the query step by step:
Step 1: INNER JOIN
INNER JOIN Product ON Sales.product_id = Product.product_id;
This is the INNER JOIN clause that combines rows from the "Sales" table and the "Product" table. The condition for the join is that the "product_id" column in the "Sales" table must match the "product_id" column in the "Product" table.
Output After Step 1:
+---------+------------+------+----------+-------+-------------+ | sale_id | product_id | year | quantity | price |product_name | +---------+------------+------+----------+-------+-------------+ | 1 | 100 | 2008 | 10 | 5000 | Nokia | | 2 | 100 | 2009 | 12 | 5000 | Apple | | 7 | 200 | 2011 | 15 | 9000 | Samsung | +---------+------------+------+----------+-------+-------------+
Step 2: Select specific columns
SELECT product_name, year, price FROM Sales INNER JOIN product ON Sales.product_id = Product.product_id;
This part of the query specifies the columns that you want to retrieve in the result set. In this case, it's the product name, year, and price.
Step 3: Order the table raw by year
ORDER BY year;
Final Output:
+--------------+-------+-------+ | product_name | year | price | +--------------+-------+-------+ | Nokia | 2008 | 5000 | | Nokia | 2009 | 5000 | | Apple | 2011 | 9000 | +--------------+-------+-------+
🤖 Don't fully get this? Learn it with Claude
Stuck on Product Sales Analysis I? 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 I** (Databases) and want to truly understand it. Explain Product Sales Analysis I 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 I** 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 I** 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 I** 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.