single
JOIN 2 Tables
Swipe to show menu
You've got the attention of a company that owns a small online store. They have 2 tables that are related to each other. The first table contains information about the products sold in the online store.
Here's what the product table looks like:
The second table contains product categories on the website, along with a brief description for each category.
Here's what the category table looks like:
Your first task is to join these two tables to find out how many products are in each category. You'll use a JOIN statement to achieve this.
Before diving into the task, let's understand what a JOIN statement is and how it works.
JOIN in SQL is an operation that combines rows from two or more tables based on a related column between them. JOIN allows obtaining data from multiple tables in one query, which simplifies the analysis and processing of related data.
To join two tables, they need to share a common column. For tables, this shared link is product.category_id and category.id.
When writing columns from these tables, start with the table name, add a dot, and then the column name. This helps keep the code clear, especially when tables have columns with the same name. It tells SQL exactly which table and column you mean.
Here's how JOIN works in action. Imagine we want to retrieve the average price of products for each category name.
The query to accomplish this looks like this:
1234SELECT category.name, AVG(product.price) AS average_price FROM product JOIN category ON product.category_id = category.id GROUP BY category.name
Here's a break down of how we used JOIN in our query:
- In the
SELECTpart, we list the columns we want from both tables, making sure to include the table name for clarity; - In the
JOINpart, we specify the table to join (category) and the common columns that link them (product.category_id = category.id); - Then group the data by
category.nameto calculate the average price using theAVG()function.
If this seems complex, here's a simple syntax for using JOIN:
SELECT table1.column, table2.column
FROM table1
JOIN table2 ON table1.common_column = table2.common_column
Unlike the UNION clause, the JOIN statement lets you combine entire tables, not just the columns they share. Note that UNION does not inherently require subqueries — subqueries are only needed if you want to filter or group the combined result set. In many cases, you can use UNION directly between two SELECT statements without subqueries.
Swipe to start coding
Your need to join the two tables: category and product. The common columns for these two tables are product.category_id and category.id.
Your task is to find the total amount of products in each category. To do this, you need to calculate the sum of the product.amount column.
Use the alias total_amount for this column.
At the end of your query, sort the result by the total_amount column in ascending order.
In the response, you should have 2 columns: category.name and total.amount.
Brief Instructions
- Retrieve the
category.namecolumn and the sum of theproduct.amountcolumn from theproducttable. - Assign the alias
total_amountto the second column. - Join the
categorytable using aJOINstatement. - Match the tables on the common column
product.category_id = category.id. - Group the results by
category.name. - Sort the results by
total_amount.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat