Vous êtes sur la page 1sur 4

JOINS

Joins are used to combine rows from two or more tables.The tables for join operation must have same
column name and datatype . These columns are called the join key or common key.
Types of Joins:
INNER JOIN
NATURAL JOIN
LEFT JOIN
RIGHT JOIN
CROSS JOIN
OUTER JOIN
To illustrate join operations ,we assume two tables Students and courses.
Student Table:
id

name

courseid

Alice

Bob

Caroline

Emma

(NULL)

Course Table:

1. INNER JOIN

coursei
d

Name

JAVA

PHP

MySQL

C++

Inner Join clause matches the


rows in one table with the rows
in another table and produces a set of records which match in both the tables,
Also called equijoin.

Syntax:
SELECT column_name(s) FROM table1 t1 INNER JOIN table2 t2 ON t1.column_name=t2.column_name;

Example: To display the details of students who have enrolled in a course.


SELECT a.name, b.name FROM Students a INNER JOIN course b on a.courseid = b.courseid;
2. Natural Join:
NATURAL JOIN is such a join that performs the same task as an INNER JOIN, in which the ON clause
refers to all columns that the tables to be joined have in common. Dont use ON clause in natural join
operation.
Syntax:
SELECT column_name(s) FROM table1 NATURAL JOIN table2;
Eg: To display the details of students who have enrolled in a course.
SELECT a.name, b.name FROM Students a NATURAL JOIN course b;
The inner Join using ON clause do the same job.ie:
SELECT a.name, b.name FROM Students a INNER JOIN course b on a.courseid = b.courseid;

3. LEFT JOIN:
The LEFT JOIN keyword returns all rows from the left table (table1), with the matching
rows in the right table (table2). The result is NULL in the right side when there is no match.

Syntax:
SELECT column_name(s) FROM table1 t1 LEFT JOIN table2 t2 ON t1.column_name=t2.column_name;
Eg: Display the list of all students and their courses even if theyre not enrolled in any course
SELECT a.name, b.name FROM Students a LEFT JOIN course b on a.courseid = b.courseid;

3.RIGHT JOIN:
The RIGHT JOIN keyword returns all rows from the right table (table2), with the
matching rows in the left table (table1). The result is NULL in the left side when there is no match.

Syntax;
SELECT column_name(s) FROM table1 t1 RIGHT JOIN table2 t2 ON t1.column_name=t2.column_name;
Eg: Display the list of all courses and students even if none has enrolled.
SELECT a.name, b.name FROM Students a RIGHT JOIN course b on a.courseid = b.courseid;

OUTER JOIN (or FULL OUTER JOIN)


The FULL OUTER JOIN keyword returns all rows from the left table (table1) and from the right table
(table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT joins.

OUTER JOIN is less useful than INNER, LEFT or RIGHT and its not implemented in MySQL
In SQL, its syntax:
SELECT column_name(s) FROM table1 t1 FULL OUTER JOIN table2 t2
ON t1.column_name=t2.column_name;

However in MYSQL, we can use union to implement the Outer Join:


SELECT a.name, b.name FROM Students a RIGHT JOIN course b on a.courseid = b.courseid
UNION
SELECT a.name, b.name FROM Students a LEFT JOIN course b on a.courseid = b.courseid;
CROSS JOIN:

In this join the result set appeared by multiplying each row of the first table with all rows in the second
table .
This kind of result is called as Cartesian Product.
SELECT a.name, b.name FROM Students a CROSS JOIN course b ;

Vous aimerez peut-être aussi