Académique Documents
Professionnel Documents
Culture Documents
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++
Syntax:
SELECT column_name(s) FROM table1 t1 INNER JOIN table2 t2 ON t1.column_name=t2.column_name;
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 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;
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 ;