SQL

SQL Joins

SQL Joins

This tutorial covers SQL Joins.

Joins perform retrieval of data from two or more tables based on certain connection (or relationship).

In other words, a join statement merge two or more tables on the basis of a specific column. Most commonly, the specific column refers to a foreign key column in one table and its related key in the other table.

This page encompasses:

  • inner join
  • left outer join
  • right outer join
  • full outer join
  • cross join

Here we use two tables. First is the table “student_info” from the previous tutorials, and the second is a table named “grade“, linked to the first table with a foreign key.

idfirstNamelastNameage
1JohnSmith21
2PatrickCasey26
3SamAdams19
4MikeGarciaNULL
5PatrickDumont21
idsubjectNamegradepassedstudentID
1HistoryC11
2PhysicsA12
3HistoryF03
4BiologyA1NULL
5BiologyE15

Inner Join

The “inner join” statement retrieves all data that matches in all tables.

Below is an example.

SELECT firstName, age FROM student_info
INNER JOIN grade ON student_info.id = grade.studentID; 
firstNameage
John21
Patrick26
Sam19
Patrick21

The above query selects the first name and age of all students that match the “id” of the first table with the “studentID” column in the second table.

4th student id does not match in both tables, hence not displayed in the output.

Left Join

The “left join” statement retrieves all data from the first table as well as all data that matches in all tables.

SELECT firstName, age FROM student_info
LEFT JOIN grade ON student_info.id = grade.studentID; 
firstNameage
John21
Patrick26
Sam19
MikeNULL
Patrick21

The above query selects the first name and age of all students in the first table. And any relevant data matching the “id” column of the first table with the “studentID” column in the second table.

Although 4th student id does not match in both tables, the query retrieves all data from the first table’s id column.

Right Join

The “right join” statement retrieves all data from the second table as well as all data that matches in all tables.

SELECT firstName, age FROM student_info
RIGHT JOIN grade ON student_info.id = grade.studentID; 
firstNameage
John21
Patrick26
Sam19
MikeNULL
Patrick21

The above query selects the first name and age of all students in the second table. And any relevant data matching the “id” column of the first table with the “studentID” column in the first table.

Although 4th student id does not match in both tables, the query retrieves all data from the second table’s id column.

Full Join

The “full join” statement retrieves all data from the first and second table, whether matching or not.

SELECT firstName, age FROM student_info
FULL JOIN grade ON student_info.id = grade.studentID; 
firstNameage
John21
Patrick26
Sam19
MikeNULL
Patrick21
NULLNULL

The above query selects the first name and age of all students in both, the first and second table. It also includes any matching or otherwise relevant data.

The 6th record row is the unmatched data from the 4th student id. The record cell under the first name column is also NULL as it is empty.

Cross Join

The “cross join” statement retrieves the Cartesian product of the data.

The Cartesian product is the product of two sets. In other words, each row from the first table is combined with each row from the second table.

SELECT student_info.firstName, grade.grade
FROM student_info
CROSS JOIN grade;

Cross joins can produce large results. For this reason, we will limit the output to 10 records with the “select top” statement.

SELECT TOP 10 student_info.firstName, grade.grade
FROM student_info
CROSS JOIN grade;
firstNamegrade
JohnC
PatrickC
SamC
MikeC
PatrickC
JohnA
PatrickA
SamA
MikeA
PatrickA

The above query produces the Cartesian product of the two selections.

Each table has 5 records. This creates the Cartesian product outcome of 25 records (5 x 5).


Next: SQL Case

by AICorr Team

We are proud to offer our extensive knowledge to you, for free. The AICorr Team puts a lot of effort in researching, testing, and writing the content within the platform (aicorr.com). We hope that you learn and progress forward.