English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Consultas de Juntada SQL (Join)

In this tutorial, you will learn how to join two tables to get combined data.

SQL Join Basics

So far, all the queries you have seen have been focused on a single table. But in real life, you often need to query two or more tables at once and bring a merged result set. This is technically called a join because it involves joining tables based on common fields (Foreign key) Joining different tables to create a new view of data.

To make this easy to understand, let's look at the followingemployeesedepartmentsTable. Here, the dept id column of the employees table is a foreign key in the departments table. Therefore, these two tables can be joined to get combined data.

+--------+--------------+------------+---------+
| emp_id | emp_name     | hire_date  | dept_id |
+--------+--------------+------------+---------+
|      1 | Ethan Hunt   | 2001-05-01 |       4 |
|      2 | Tony Montana | 2002-07-15 |       1 |
|      3 | Sarah Connor | 2005-10-18 |       5 |
|      4 | Rick Deckard | 2007-01-03 |       3 |
|      5 | Martin Blank | 2008-06-24 |    NULL |
+--------+--------------+------------+---------+

+---------+------------------+
| dept_id | dept_name        |
+---------+------------------+
|       1 | Administration   |
|       2 | Customer Service |
|       3 | Finance          |
|       4 | Human Resources  |
|       5 | Sales            |
+---------+------------------+
Table: employees
Table: departments

Note:To join tables, the data in the columns used to join the tables should match, not necessarily the column names.

Join type

When joining tables, the type of join created in the query affects the rows displayed in the result set. You can create the following types of joins:

Inner join (Inner join)

The join only returns rows that have matching items in both join tables. For example, you can join the employees anddepartmentsTables are joined together to create a result set that displays the department name for each employee. In an inner join, employees without department information are not included in the result set, and departments without employees will also not be included in the result set.

In the next chapter, we will learn aboutInternal joinPara mais informações.

Conexão Externa (Outer join)

A conexão externa é uma extensão da conexão interna. Mesmo que a conexão externa não tenha linhas correspondentes na tabela de conexão, a conexão externa também retornará essas linhas. Existem três tipos de conexão externa: conexão esquerda (left join) ou conexão direita (right join) e conexão completa (full join)

Vamos explorar essas variantes de conexão externa em capítulos posteriores.

Conexão Cruzada (Cross join)

A conexão cruzada é uma junção sem condições de conexão. Cada linha de uma tabela é combinada com cada linha da outra tabela. Este tipo de conjunto de resultados é chamado de produto cartesiano ou cruceiro. Por exemplo,employeesedepartmentsA conexão cruzada entre tabelas gera um conjunto de resultados, onde cada possível funcionário/cada combinação de departamentos tem uma linha.

Nos próximos capítulos, vamos aprender sobreConexão CruzadaPara mais informações.