SQL JOINs
Types of JOINs
[INNER] JOIN
For each record from the left table, all records from the right table that satisfy the merge condition will be merged with the original record and added to the resulting table. If no matching records are found in the right table, the original record is also not added to the resulting table (using the INNER keyword is optional).
LEFT/RIGHT JOIN
Returns all the records from the left/right table and the matching values from the right/left table. If some records in one of the tables (for example, the left one) do not match the other (for example, the right one) table, the corresponding columns in the output of this right table will be assigned NULL.
FULL [OUTER] JOIN
Returns all records when a match is in the left or the right table. Records that do not have a match are assigned NULL (using OUTER is optional).
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652ec9ca8819d8a89b9d4c05_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.34.50.png)
CROSS JOIN
Joins up every record from the left table and every record in the right table (the algorithm is illustrated in the picture below).
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652ec9ea7311726a447a7366_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.52.30.png)
So id1 from table1 gets joined with idA, idB, and idC, id2 also gets joined with idA, idB, and idC, and the same holds for id3.
INNER JOIN
To see how different types of JOINs work, let's consider the following tables: character, pet, and universe.
The character table has columns named character_id (INT), character_name (VARCHAR(30)), and universe_id (INT):
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652eca0dce69267a9a6698fd_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.53.03.png)
The pet table has columns named character_id (INT) and pet_species (VARCHAR(50)):
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652eca34c33b31787a69a1a7_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.53.41.png)
The universe table has columns named universe_id (INT), universe_name (VARCHAR(30)), and author (VARCHAR(30)):
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652eca554f1a56b58adff359_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.54.16.png)
The common syntax for INNER JOIN is the following:
The join condition can be some other than equality. The commands in brackets are optional.
Ordering results of INNER JOIN
To find out what pet belongs to what character from the previous example, execute the following query:
The resulting table from this query will be the following:
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652ecb26110ad8be27033a3d_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.57.42.png)
Not all characters were included in the result due to some records in the left table not having matches in the right table. In the example, not all characters had pets. To fix this problem and include all the characters in the resulting table, use LEFT JOIN. Combining information from two tables by matching rows where character_id was the same in the two tables.
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652ecb74abe0f2858a2f4013_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2020.58.59.png)
You can join multiple tables. For example, one can want to know which universe our characters come from. Values that didn't have matches in the right table were assigned with NULL values.
![](https://cdn.prod.website-files.com/65255f01672fcf3029794f94/652ecbc6324c53c11c5bd62d_%D0%A1%D0%BD%D0%B8%D0%BC%D0%BE%D0%BA%20%D1%8D%D0%BA%D1%80%D0%B0%D0%BD%D0%B0%202023-10-17%20%D0%B2%2021.00.26.png)
To understand how CROSS JOIN maybe useful, think of the chess competitions where organizers must pair up all the participants. Also, note that when selecting columns, it is not mandatory to write table_name1.col_name. If the column name is unique for all tables, omit it. However, it is still recommended to specify it because the names of the columns become ambiguous when dealing with multiple tables.
Use aliases for table names. SQL aliases give a table (or a column in a table) a temporary name. For example, SELECT person_name AS name. They are beneficial if tables have long names.
It is not mandatory to use JOIN if you want to connect tables. Use the WHERE condition to get the same result. The script below would give an identical result to the one above:
The commands in brackets are optional.
To join multiple tables, use this general template: