Consultas y Transformaci贸n Avanzada de Datos
Dise帽o y Optimizaci贸n Avanzada de Bases de Datos con SQL y MySQL
Construcci贸n de Base de Datos en MySQL: Terminal, Tablas y Datos Simulados
Transformaci贸n de Datos con SQL: De Consultas Simples a Superqueries
Columnas Generadas en SQL
Expresiones Regulares y Slugs en SQL y MySQL: Casos de Uso Reales
Automatizaci贸n, Reutilizaci贸n y Eficiencia en Consultas
Vistas y Tablas Virtuales en SQL
Consultas Anidadas y Vistas Materializadas
Triggers y Vistas Materializadas en MySQL (Kinda)
Automatizaci贸n de Bases de Datos con Triggers en MySQL
Llaves Primarias e 脥ndices
Trabajo con Datos Avanzados (JSON)
Uso de JSON en MySQL: Almacenamiento Eficiente de Datos Estructurados
B煤squedas Avanzadas con JSON en MySQL: Indexaci贸n y Optimizaci贸n
Joins en SQL: Conecta Tablas y Mejora tus Consultas
Motores de Almacenamiento y Encodings
Gesti贸n de Usuarios y Permisos en SQL
Gesti贸n Avanzada y An谩lisis de Bases de Datos
Information Schema en MySQL: An谩lisis Interno de tu Base de Datos
Mysqldump
Domina las Migrations: Evoluciona y Gestiona tu Base de Datos con Eficiencia
Optimiza tus Decisiones con Metabase
You don't have access to this class
Keep learning! Join and start boosting your career
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.
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.
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.
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.
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.
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).
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.
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.
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.
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.
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
Want to see more contributions, questions and answers from the community?