HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

SQL joins

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

Let's look at a selection from the "products" table:

product table

Then look at a selection from the "employee" table:

employee table

Notice that the "Employee_id" column in the "products" table refers to the "Employee_id" in the "employee" table. The relationship between the two tables above is the "Employee_id" column.

Then, we can create the following SQL statement (that contains an INNER JOIN), that selects records that have matching values in both tables:

Example

SELECT  products . Order_id , products. Order_date , employee. Employee_name 
 FROM  products 
 INNER JOIN  
 employee ON  products.  Employee_id = employee. Employee_id;
You can click on above box to edit the code and run again.

Output

join 3 example

Different Types of SQL JOINs

  • (INNER) JOIN : Returns records that have matching values in both tables
  • LEFT (OUTER) JOIN : Returns all records from the left table, and the matched records from the right table
  • RIGHT (OUTER) JOIN : Returns all records from the right table, and the matched records from the left table
  • FULL (OUTER) JOIN : Returns all records when there is a match in either left or right table
join 4 example