SQL for Beginners
About Lesson

Joins

SQL joins are used to query data from two or more tables, based on a relationship between certain columns in these tables.

 

 

Different SQL JOINs

Before we continue with examples, we will list the types of JOIN you can use, and the differences between them.

  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables

 

Example:

Given 2 tables:

  • SCHOOL
  • CLASS

 

The diagram is shown below:

 

We want to get the following information using a query:

In order to get information from more than one table we need to use the JOIN. The JOIN is used to join the primary key in one table with the foreign key in another table.

select 
SCHOOL.SchoolName, 
CLASS.ClassName 
from
SCHOOL
INNER JOIN CLASS ON SCHOOL.SchoolId = CLASS.SchoolId