You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesi贸n a prueba de IA

Antes: $249

Currency
$209
Suscr铆bete

Termina en:

0 D铆as
14 Hrs
58 Min
42 Seg
Curso de SQL y MySQL

Curso de SQL y MySQL

Genaro Bernardino

Genaro Bernardino

Joins en SQL: Conecta Tablas y Mejora tus Consultas

13/19
Resources

Understanding JOINs in SQL is critical for any developer or data analyst seeking to extract valuable information from relational databases. This concept, although sometimes intimidating for beginners, represents one of the most powerful tools for relating data between tables and obtaining meaningful results. Mastering it will allow you to build efficient and accurate queries that respond exactly to your information needs.

What is a JOIN and why is it so important?

A JOIN is simply a way to cross or relate information between tables through specific conditions. Its main function is to take a table with all the values that meet a certain condition and "paste" another table that increases in columns or rows the available information. This process can be repeated with third and fourth tables, creating a view as wide and rich as we specify in our conditions.

The LEFT JOIN is the most used type (so much that MySQL uses it by default when we only write JOIN), but the important thing is to understand the fundamental concept: we are defining paths between tables to obtain the information we need.

Basic structure of a JOIN

The basic syntax of a JOIN includes:

SELECT columnsFROM table1 AS alias1LEFT JOIN table2 AS alias2 ON alias1.column = alias2.column.

It is important to note that in MySQL the order of the columns in the ON condition does not matter (although in other database drivers it may be crucial). For MySQL, this part is simply a boolean: if true, the relationship exists; if false, there is no relationship between the tuples in both tables.

How to build effective JOIN queries?

To illustrate how JOINs work, we will work with a database containing tables such as Bills, Clients, Bill_Products and Products. These tables are related to each other, allowing us to navigate from an invoice to the products it contains and the associated customers.

Basic example: relating invoices and customers

Let's start with a simple example:

SELECT b.bill_id, b.status, c.name, c.emailFROM bills AS bLEFT JOIN clients AS c ON c.client_id = b.client_id

This query shows us extended bill information with corresponding customer data. We are using the bills table as a starting point and adding information from clients.

Changing the perspective: customers as the central point

If we reverse the order and use clients as the main table:

SELECT c.name, COUNT(b.bill_id)FROM clients AS cLEFT JOIN bills AS b ON b.client_id = c.client_idGROUP BY c.name

Now we are looking at how many bills each customer has. The crucial difference is that we will now bring in all customers, even those without bills (which will show a count of 0).

Filtering results with WHERE

We can refine our results using WHERE:

SELECT c.name, COUNT(b.bill_id)FROM clients AS cLEFT JOIN bills AS b ON b.client_id = c.client_idWHERE b.bill_id IS NULLGROUP BY c.name.

This query shows us only the clients that do not have bills associated with them. If we change it to IS NOT NULL, we would see only customers with at least one invoice.

How to optimize queries with multiple JOIN?

A critical aspect when working with JOIN is efficiency. Bringing in too much unnecessary information can be costly, especially in cloud environments where you charge for data transfer.

Example: products without sales

SELECT p.name, COUNT(bp.bill_product_id)FROM products AS pLEFT JOIN bill_products AS bp ON bp.product_id = p.product_idWHERE bp.product_id IS NULLGROUP BY p.name

This query shows products that do not appear in any bill. By adding the WHERE condition, we significantly reduce the amount of data transferred, showing only what we need.

Complex query with multiple JOIN

To relate information from four different tables:

SELECT b.bill_id, c.name, p.name, bp.quantityFROM bills AS bLEFT JOIN clients AS c ON c.client_id = b.client_idLEFT JOIN bill_products AS bp ON b.bill_id = bp.bill_idLEFT JOIN products AS p ON bp.product_id = p.product_idGROUP BY b.bill_id, c.name, p.name, bp.quantity.

This query shows us which products each customer has bought, with what quantity, all centered on the invoices as a starting point.

If we want a more concise summary:

SELECT b.bill_id, c.name, COUNT(p.product_id)FROM bills AS bLEFT JOIN clients AS c ON c.client_id = b.client_idLEFT JOIN bill_products AS bp ON b.bill_id = bp.bill_idLEFT JOIN products AS p ON bp.product_id = p.product_idGROUP BY b.bill_id, c.name.

This approach gives us a count of products per bill, ideal for summary pages.

What impact does the order of the tables have?

The order of the tables in a JOIN determines what information is considered "main". If we use clients as the base table instead of bills:

SELECT c.name, b.bill_idFROM clients AS cLEFT JOIN bills AS b ON b.client_id = c.client_id

This query will fetch all customers (over 96,000 in our example), even those without bills. If we add WHERE b.bill_id IS NOT NULL, we will get the same result as if we had used bills as the main table, but with a higher computational cost.

Changing the order of tables is, in essence, equivalent to switching between LEFT JOIN and RIGHT JOIN, but in a more intuitive and linear way.

JOINs are powerful tools but require conscious use to avoid bringing in too much unnecessary information. Understanding how they work will allow you to build efficient queries that exactly meet your data analysis needs.

Have you experimented with complex queries using multiple JOINs? Share in the comments your experiences or doubts about how to solve the challenges posed: finding who is the person who has bought the most, which is the best-selling product and which has generated the most revenue for the store.

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?