0% found this document useful (0 votes)
58 views24 pages

MySQL - Joins

MySQL JOINS are used to retrieve data from multiple tables. There are different types of joins in MySQL including inner joins, left outer joins, right outer joins, natural joins, and cross joins. Inner joins return rows where the join condition is met between both tables, while left and right outer joins return all rows from the left or right table respectively and matching rows from the other table.

Uploaded by

Sabu K Kuriyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views24 pages

MySQL - Joins

MySQL JOINS are used to retrieve data from multiple tables. There are different types of joins in MySQL including inner joins, left outer joins, right outer joins, natural joins, and cross joins. Inner joins return rows where the join condition is met between both tables, while left and right outer joins return all rows from the left or right table respectively and matching rows from the other table.

Uploaded by

Sabu K Kuriyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

MySQL: Joins

(Retrieve Records from multiple Tables)


MySQL: Joins
MySQL JOINS are used to retrieve data from multiple tables.

A MySQL JOIN is performed whenever two or more tables are joined in a SQL
statement.

There are different types of MySQL joins:

● MySQL INNER JOIN (or sometimes called simple join)


● MySQL LEFT OUTER JOIN (or sometimes called LEFT JOIN)
● MySQL RIGHT OUTER JOIN (or sometimes called RIGHT JOIN)
● Natural Join
● Cross Join
INNER JOIN (simple join)

It is the most common type of


join.
MySQL INNER JOINS return all
rows from multiple tables where
the join condition is met.
The MySQL INNER JOIN would
return the records where table1
and table2 intersect.
INNER JOIN (simple join)
It is the most common type of join. MySQL INNER JOINS return all rows from
multiple tables where the join condition is met.

Syntax :

SELECT columns

FROM table1

INNER JOIN table2

ON table1.column = table2.column;
Supplier Orders

supplier_id supplier_name order_id supplier_id order_date

10000 IBM 500125 10000 2013/05/12

10001 Hewlett Packard 500126 10001 2013/05/13

10002 Microsoft 500127 10004 2013/05/14

10003 NVIDIA
Example

SELECT suppliers.supplier_id, suppliers.supplier_name,


orders.order_date
FROM suppliers
INNER JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
The older implicit syntax

SELECT suppliers.supplier_id, suppliers.supplier_name,


orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
LEFT OUTER JOIN
LEFT OUTER JOIN type of join
returns all rows from the LEFT-hand
table specified in the ON condition
and only those rows from the other
table where the joined fields are
equal (join condition is met).

The MySQL LEFT OUTER JOIN


would return the all records from
table1 and only those records from
table2 that intersect with table1.
LEFT OUTER JOIN

SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column;
Example

SELECT suppliers.supplier_id, suppliers.supplier_name,


orders.order_date
FROM suppliers
LEFT JOIN orders
ON suppliers.supplier_id = orders.supplier_id;
RIGHT OUTER JOIN
MySQL RIGHT OUTER JOIN type of
join returns all rows from the
RIGHT-hand table specified in the
ON condition and only those rows
from the other table where the joined
fields are equal (join condition is
met).
The MySQL RIGHT OUTER JOIN
would return the all records from
table2 and only those records from
table1 that intersect with table2.
RIGHT OUTER JOIN
SELECT columns

FROM table1

RIGHT [OUTER] JOIN table2

ON table1.column = table2.column;
FULL OUTER join

MySql does not support full outer join. But we can implement with the help of
UNION.

SELECT columns
FROM table1
LEFT [OUTER] JOIN table2
ON table1.column = table2.column
UNION
SELECT columns
FROM table1
RIGHT [OUTER] JOIN table2
ON table1.column = table2.column;
Natural Join
In MySQL, the NATURAL JOIN is such a join that performs the same task as an
INNER or LEFT JOIN, in which the ON or USING clause refers to all columns that
the tables to be joined have in common.
The MySQL NATURAL JOIN is structured in such a way that, columns with the
same name of associate tables will appear once only.
Natural Join Guidelines:
● The associated tables have one or more pairs of identically named columns.
● The columns must be the same data type.
● Don’t use ON clause in a NATURAL JOIN.
Natural Join
SELECT columns

FROM table1

NATURAL JOIN table2;


Cross Join

● In MySQL, the CROSS JOIN produced a result set which is the


product of rows of two associated tables when no WHERE clause
is used with CROSS JOIN.
● In this join, the result set appeared by multiplying each row of the
first table with all rows in the second table if no condition
introduced with CROSS JOIN.
● This kind of result is called as Cartesian Product.
Cross Join
SELECT columns

FROM table1

CROSS JOIN table2;


College database

Courses Student
CourseID Coursetitle Category Fees Adno Studname Courseid

10 C-Programming SW 2000 101 Sunil 10


20 OOP-Java SW 5000 102 Vidya 10
30 DBMS-mySql SW 3500 103 Renuka NULL
40 Python SW 5000 104 Varghese 40
50 Assembling HW 2500 105 Mohammed 30
60 Networking HW 6000 106 Fatima 40
107 Lawrence 60
List details of students who are joined for a course along
with their course title
SELECT adno , studname , student.courseid , coursetitle FROM
student INNER JOIN courses ON student.courseid =
courses.courseid;
+------+----------+----------+---------------+
| adno | studname | courseid | coursetitle |
+------+----------+----------+---------------+
| 101 | Sunil | 10 | C-Programming |
| 102 | Vidya | 10 | C-Programming |
| 104 | Varghese | 40 | Python |
| 105 | Mohammed | 30 | DBMS-MySql |
| 106 | Fatima | 40 | Python |
| 107 | Lawrence | 60 | Networking |
+------+----------+----------+---------------+
List details of all students even if they are not joined for a
course
SELECT adno , studname , student.courseid , coursetitle FROM student LEFT
OUTER JOIN courses ON student.courseid = courses.courseid;
+------+----------+----------+---------------+
| adno | studname | courseid | coursetitle |
+------+----------+----------+---------------+
| 101 | Sunil | 10 | C-Programming |
| 102 | Vidya | 10 | C-Programming |
| 103 | Renuka | NULL | NULL |
| 104 | Varghese | 40 | Python |
| 105 | Mohammed | 30 | DBMS-MySql |
| 106 | Fatima | 40 | Python |
| 107 | Lawrence | 60 | Networking |
+------+----------+----------+---------------+
Retrieve details of all courses even if no one has enrolled
for the course
SELECT courses.courseid , coursetitle , category , fees, studname FROM
courses LEFT OUTER JOIN student ON courses.courseid = student.courseid;
+----------+---------------+----------+---------+----------+
| courseid | coursetitle | category | fees | studname |
+----------+---------------+----------+---------+----------+
| 10 | C-Programming | SW | 2000.00 | Sunil |
| 10 | C-Programming | SW | 2000.00 | Vidya |
| 40 | Python | SW | 5000.00 | Varghese |
| 30 | DBMS-MySql | SW | 3500.00 | Mohammed |
| 40 | Python | SW | 5000.00 | Fatima |
| 60 | Networking | HW | 6000.00 | Lawrence |
| 20 | OOPS-Java | SW | 5000.00 | NULL |
| 50 | Assembling | HW | 2500.00 | NULL |
+----------+---------------+----------+---------+----------+
Count number of students enrolled for each course
SELECT student .courseid , coursetitle , COUNT( *) FROM
student INNER JOIN courses ON student.courseid =
courses.courseid GROUP BY student.courseid;
+----------+---------------+-----------+
| courseid | coursetitle | COUNT( *) |
+----------+---------------+-----------+
| 10 | C-Programming | 2 |
| 30 | DBMS-MySql | 1 |
| 40 | Python | 2 |
| 60 | Networking | 1 |
+----------+---------------+-----------+
List all records in both tables regardless of any match
( SELECT student.courseid , adno , studname , courses.courseid , coursetitle FROM
courses LEFT OUTER JOIN student ON student.courseid = courses.courseid
) UNION (SELECT student.courseid , adno , studname , courses.courseid , courseti
tle FROM courses RIGHT OUTER JOIN student ON student.courseid = courses.courseid
);
+----------+------+----------+----------+---------------+
| courseid | adno | studname | courseid | coursetitle |
+----------+------+----------+----------+---------------+
| 10 | 101 | Sunil | 10 | C-Programming |
| 10 | 102 | Vidya | 10 | C-Programming |
| 40 | 104 | Varghese | 40 | Python |
| 30 | 105 | Mohammed | 30 | DBMS-MySql |
| 40 | 106 | Fatima | 40 | Python |
| 60 | 107 | Lawrence | 60 | Networking |
| NULL | NULL | NULL | 20 | OOPS-Java |
| NULL | NULL | NULL | 50 | Assembling |
| NULL | 103 | Renuka | NULL | NULL |
+----------+------+----------+----------+---------------+
Display CourseID, CourseTitle, Category, Fees and student names
for Software courses with Fees less than 5000

SELECT courses.courseid , coursetitle , category , fees,


studname FROM courses INNER JOIN student ON courses.courseid
= student.courseid WHERE category='SW' AND fees < 5000;
+----------+---------------+----------+---------+----------+
| courseid | coursetitle | category | fees | studname |
+----------+---------------+----------+---------+----------+
| 10 | C-Programming | SW | 2000.00 | Sunil |
| 10 | C-Programming | SW | 2000.00 | Vidya |
| 30 | DBMS-MySql | SW | 3500.00 | Mohammed |
+----------+---------------+----------+---------+----------+

You might also like