Contenido del Curso
SQL Intermedio
SQL Intermedio
UNIR 2 Tablas
We'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:
Our first task is to join these two tables to find out how many products are in each category. We'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.
To join two tables, they need to share a common column. Let's see how JOIN
works using the employees
and department
tables. Their common column is employees.department
and departments.name
.
Note
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.
Para unir 2 tablas, deben tener una columna en común entre ellas. Consideremos el uso de JOIN
usando el ejemplo de las tablas employees
y department
. Les recordaré que su columna común es employees.department
y departments.name
.
Nota
Fíjate cómo escribí las columnas de estas tablas. Primero, escribo el nombre de la tabla, luego pongo un punto y el nombre de la columna. Cuando usamos más de una tabla en una consulta, necesitamos especificar la tabla de la cual estamos tomando la columna para hacer el código legible. Además, en el caso de que las tablas tengan columnas con el mismo nombre, necesitamos que SQL entienda a qué tabla y columna nos estamos refiriendo.
SELECT department.type, SUM(employees.salary) AS total_salary FROM employees JOIN department ON employees.department = department.name GROUP BY department.type
Let's break down how we used JOIN
in our query:
- In the
SELECT
part, we list the columns we want from both tables, making sure to include the table name for clarity; - In the
JOIN
part, we specify the table to join and the common column that links them. Here, it'semployees.department
anddepartment.name
; - We then group the data by
type
to calculate the total salary using theSUM()
function.
If this seems complex, here's a simple syntax for using JOIN
:
Vamos a entender paso a paso lo que hicimos y cómo utilizamos JOIN
:
- En la sección
SELECT
, especificamos las columnas que necesitamos recuperar de dos tablas, indicando el nombre de la tabla al que pertenece la columna; - En la sección
JOIN
, especificamos la tabla que queremos unir, seguido de especificar la columna común. En nuestro caso, esemployees.department
ydepartment.name
; - Luego, agregamos los datos por
type
(ya que tenemos una función de agregaciónSUM()
en la secciónSELECT
para encontrar el salario total) y obtenemos el resultado que nos interesa.
La consulta puede parecer compleja, así que veamos la sintaxis general para usar JOIN
:
Swipe to show code editor
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.name
column and the sum of theproduct.amount
column from theproduct
table. - Assign the alias
total_amount
to the second column. - Join the
category
table using aJOIN
statement. - Match the tables on the common column
product.category_id = category_id
. - Group the results by
category.name
. - Sort the results by
total_amount
.
¡Gracias por tus comentarios!
UNIR 2 Tablas
We'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:
Our first task is to join these two tables to find out how many products are in each category. We'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.
To join two tables, they need to share a common column. Let's see how JOIN
works using the employees
and department
tables. Their common column is employees.department
and departments.name
.
Note
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.
Para unir 2 tablas, deben tener una columna en común entre ellas. Consideremos el uso de JOIN
usando el ejemplo de las tablas employees
y department
. Les recordaré que su columna común es employees.department
y departments.name
.
Nota
Fíjate cómo escribí las columnas de estas tablas. Primero, escribo el nombre de la tabla, luego pongo un punto y el nombre de la columna. Cuando usamos más de una tabla en una consulta, necesitamos especificar la tabla de la cual estamos tomando la columna para hacer el código legible. Además, en el caso de que las tablas tengan columnas con el mismo nombre, necesitamos que SQL entienda a qué tabla y columna nos estamos refiriendo.
SELECT department.type, SUM(employees.salary) AS total_salary FROM employees JOIN department ON employees.department = department.name GROUP BY department.type
Let's break down how we used JOIN
in our query:
- In the
SELECT
part, we list the columns we want from both tables, making sure to include the table name for clarity; - In the
JOIN
part, we specify the table to join and the common column that links them. Here, it'semployees.department
anddepartment.name
; - We then group the data by
type
to calculate the total salary using theSUM()
function.
If this seems complex, here's a simple syntax for using JOIN
:
Vamos a entender paso a paso lo que hicimos y cómo utilizamos JOIN
:
- En la sección
SELECT
, especificamos las columnas que necesitamos recuperar de dos tablas, indicando el nombre de la tabla al que pertenece la columna; - En la sección
JOIN
, especificamos la tabla que queremos unir, seguido de especificar la columna común. En nuestro caso, esemployees.department
ydepartment.name
; - Luego, agregamos los datos por
type
(ya que tenemos una función de agregaciónSUM()
en la secciónSELECT
para encontrar el salario total) y obtenemos el resultado que nos interesa.
La consulta puede parecer compleja, así que veamos la sintaxis general para usar JOIN
:
Swipe to show code editor
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.name
column and the sum of theproduct.amount
column from theproduct
table. - Assign the alias
total_amount
to the second column. - Join the
category
table using aJOIN
statement. - Match the tables on the common column
product.category_id = category_id
. - Group the results by
category.name
. - Sort the results by
total_amount
.
¡Gracias por tus comentarios!