In any real relational database, all information is distributed in separate tables. Many of the tables have fixed relationships with each other fixed in the diagram. However, using Sql queries, itโs quite possible to build a connection between data that is not inherent in the scheme. This is accomplished by performing the join join operation, which allows you to build relationships between any number of tables and join even seemingly disparate data.
This article will discuss specifically the left outer join. Before starting to describe this type of connection, we will add some tables to the database.
Preparation of the necessary tables
Suppose our database contains information about people and their real estate. The basic information is based on three tables: Peoples (people), Realty (real estate), Realty_peoples (table with relations, which of the people which property belongs to). Suppose the following people data are stored in tables:
Peoples |
id | L_name | F_name | Middle_name | Birthday |
1 | Ivanova | Daria | Borisovna | 07/16/2000 |
2 | Pugin | Vladislav | Nikolaevich | 01/29/1986 |
3 | Evgenin | Alexander | Federovich | 04/30/1964 |
4 | Annina | Love | Pavlovna | 12/31/1989 |
5 | Gerasimovskaya | Hope | Pavlovna | 03/14/1992 |
6 | Gerasimovsky | Oleg | Albertovich | 01/29/1985 |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 |
8 | Sukhanovskaya | Yulia | Yuryevna | 10/01/2001 |
Real estate:
Realty |
id | address |
1 | Arkhangelsk, st. Voronina, d. 7, apt. 6 |
2 | Arkhangelsk, st. Severodvinskaya, d. 84, apt. 9, room 5 |
3 | Arkhangelsk region, Severodvinsk, st. Lenin, d. 134, apt. 85 |
4 | Arkhangelsk region, Novodvinsk, st. Proletarskaya, 16, apt. 137 |
5 | Arkhangelsk, pl. Terekhina, d. 89, apt. thirteen |
By relationship people - real estate:
Realty_peoples |
id_peoples | id_realty | type |
7 | 3 | Common joint ownership |
8 | 3 | Common joint ownership |
3 | 5 | Own |
7 | 1 | Own |
5 | 4 | Common ownership |
6 | 4 | Common ownership |
Left join (Sql) - description
The left join has the following syntax:
Table_A LEFT JOIN table_B [{ON predicate } | {USING Column_List }] |
And it looks like this:
And this expression is translated as โSelect all the rows from table A without exception, and from the table B, output only rows matching the predicate. If in table B there were no pairs for the rows of table A, then fill the resulting Null columns with values. โ
Most often, when performing a left join, ON is indicated, USING is used only when the names of the columns on which it is planned to make a join match.
Left join - use cases
Using the left connection, we can see if all the people from the Peoples list have real estate. To do this, execute the following query example in left join sql:
SELECT Peoples. *, Realty_peoples.id_realty, Realty_peoples.type FROM Peoples LEFT JOIN Realty_peoples ON Peoples.id = Realty_peoples.id_peoples; |
And we get the following result:
Request1 |
id | L_name | F_name | Middle_name | Birthday | id_realty | type |
1 | Ivanova | Daria | Borisovna | 07/16/2000 | | |
2 | Pugin | Vladislav | Nikolaevich | 01/29/1986 | | |
3 | Evgenin | Alexander | Federovich | 04/30/1964 | 5 | Own |
4 | Annina | Love | Pavlovna | 12/31/1989 | | |
5 | Gerasimovskaya | Hope | Pavlovna | 03/14/1992 | 4 | Common ownership |
6 | Gerasimovsky | Oleg | Albertovich | 01/29/1985 | 4 | Common ownership |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 1 | Own |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 3 | Common joint ownership |
8 | Sukhanovskaya | Yulia | Yuryevna | 10/01/2001 | 3 | Common joint ownership |
As you can see, Ivanova Daria, Pugin Vladislav and Annina Lyubov have no registered property rights.
And what would we get using the Inner join inner join? As you know, it excludes mismatched lines, so three people from our final sample would simply drop out:
Request1 |
id | L_name | F_name | Middle_name | Birthday | id_realty | type |
3 | Evgenin | Alexander | Federovich | 04/30/1964 | 5 | Own |
5 | Gerasimovskaya | Hope | Pavlovna | 03/14/1992 | 4 | Common ownership |
6 | Gerasimovsky | Oleg | Albertovich | 01/29/1985 | 4 | Common ownership |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 1 | Own |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 3 | Common joint ownership |
8 | Sukhanovskaya | Yulia | Yuryevna | 10/01/2001 | 3 | Common joint ownership |
It would seem that the second option also meets the conditions of our task. However, if we continue to join more and more tables, three people from the result will already irrevocably disappear. Therefore, in practice, when joining multiple tables, Left and Right joins are much more often used than Inner join.
We continue to consider examples with left join sql . Let's attach the table with the addresses of our real estate objects:
SELECT Peoples. *, Realty_peoples.id_realty, Realty_peoples.type, Realty.address FROM Peoples LEFT JOIN Realty_peoples ON Peoples.id = Realty_peoples.id_peoples LEFT JOIN Realty ON Realty.id = Realty_peoples.id_realty |
Now we get not only the type of law, but also the addresses of real estate:
Request1 |
id | L_name | F_name | Middle_name | Birthday | id_realty | type | address |
1 | Ivanova | Daria | Borisovna | 07/16/2000 | | | |
2 | Pugin | Vladislav | Nikolaevich | 01/29/1986 | | | |
3 | Evgenin | Alexander | Federovich | 04/30/1964 | 5 | Own | Arkhangelsk, pl. Terekhina, d. 89, apt. thirteen |
4 | Annina | Love | Pavlovna | 12/31/1989 | | | |
5 | Gerasimovskaya | Hope | Pavlovna | 03/14/1992 | 4 | Common ownership | Arkhangelsk region, Novodvinsk, st. Proletarskaya, 16, apt. 137 |
6 | Gerasimovsky | Oleg | Albertovich | 01/29/1985 | 4 | Common ownership | Arkhangelsk region, Novodvinsk, st. Proletarskaya, 16, apt. 137 |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 3 | Common joint ownership | Arkhangelsk region, Severodvinsk, st. Lenin, d. 134, apt. 85 |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 1 | Own | Arkhangelsk, st. Voronina, d. 7, apt. 6 |
8 | Sukhanovskaya | Yulia | Yuryevna | 10/01/2001 | 3 | Common joint ownership | Arkhangelsk region, Severodvinsk, st. Lenin, d. 134, apt. 85 |
Left join - typical usage errors: incorrect table order
There are two main mistakes made with a left outer join of tables:
- The order of the tables that caused the data loss was incorrectly selected.
- Errors when using Where in a query with joined tables.
Consider the first error. Before solving any problem, you should clearly understand what exactly we want to get in the end. In the example considered above, we brought everyone to a single person, but absolutely lost information about the object at number 2, which did not find an owner.
If we rearranged the tables in the query and started with โ... From Realty left join Peoples ...โ, then we would not lose a single property, which cannot be said about people.
However, you should not, frightened by the left connection, go over to the full external one, which will include as a result both matching and not matching lines.
Indeed, the sample size is often very large, and the extra data is really useless. The main thing is to figure out what you want to get in the end: all people with a list of their real estate, or a list of all real estate with their owners (if any).
Left join - typical usage errors: correct query when setting conditions in Where
The second error is also associated with data loss, and it is not always immediately obvious.
Letโs get back to the query when, using the left connection, we received data on all people and their real estate. Recall the following example using left join sql:
FROM Peoples LEFT JOIN Realty_peoples ON Peoples.id = Realty_peoples.id_peoples; |
Suppose we want to clarify the request and not display data where the type of law is โPropertyโ. If we just add, using left join sql, an example with the following condition:
...
weโll lose data on people who have no real estate, because an empty Null value is not compared in this way:
Request1 |
id | L_name | F_name | Middle_name | Birthday | id_realty | type |
5 | Gerasimovskaya | Hope | Pavlovna | 03/14/1992 | 4 | Common ownership |
6 | Gerasimovsky | Oleg | Albertovich | 01/29/1985 | 4 | Common ownership |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 3 | Common joint ownership |
8 | Sukhanovskaya | Yulia | Yuryevna | 10/01/2001 | 3 | Common joint ownership |
To prevent errors for this reason, it is best to set a selection condition immediately upon connection. We suggest considering the following example with left join sql.
SELECT Peoples. *, Realty_peoples.id_realty, Realty_peoples.type FROM Peoples LEFT JOIN Realty_peoples ON (Peoples.id = Realty_peoples.id_peoples AND type <> "Property") |
The result will be as follows:
Request1 |
id | L_name | F_name | Middle_name | Birthday | id_realty | type |
1 | Ivanova | Daria | Borisovna | 07/16/2000 | | |
2 | Pugin | Vladislav | Nikolaevich | 01/29/1986 | | |
3 | Evgenin | Alexander | Federovich | 04/30/1964 | | |
4 | Annina | Love | Pavlovna | 12/31/1989 | | |
5 | Gerasimovskaya | Hope | Pavlovna | 03/14/1992 | 4 | Common ownership |
6 | Gerasimovsky | Oleg | Albertovich | 01/29/1985 | 4 | Common ownership |
7 | Sukhanovsky | Yuri | Andreevich | 09/25/1976 | 3 | Common joint ownership |
8 | Sukhanovskaya | Yulia | Yuryevna | 10/01/2001 | 3 | Common joint ownership |
Thus, having executed an example simple with left join sql, we got a list of all the people, additionally listing which of them have real estate in shared / joint ownership.
As a conclusion, I would like to emphasize once again that the selection of any information from the database should be treated responsibly. Using left join sql, many of the nuances were revealed to us by a simple example, the explanation of which is one - before starting even making an elementary query, we need to carefully understand what exactly we want to get in the end. Good luck