You are on page 1of 8

M.

Chinyuku Database Systems 2028

MySQL Practical 8- The Join Operator


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

Different Types of SQL JOINs


• (INNER) JOIN
• LEFT (OUTER) JOIN
• RIGHT (OUTER) JOIN
• FULL (OUTER) JOIN

Exercise
1. Create a database named exercise4
2. Create and populate the tables with the given values in database exercise 4

Lecturer(Lecturer_ID, Lecturer_Name)

Course(Course_ID, CourseName, Lecturer_ID)

CREATE TABLE Lecturer (


Lecturer_ID CHAR(5) NOT NULL,
Lecturer_Name VARCHAR(40) not null,
PRIMARY KEY(Lecturer_ID)
);

CREATE TABLE Course(


Course_ID CHAR(6) NOT NULL,
Course_Name VARCHAR(40) not NULL,
Lecturer_ID CHAR(5),
PRIMARY KEY (Course_ID),
FOREIGN KEY (Lecturer_ID) REFERENCES Lecturer(Lecturer_ID)
on DELETE CASCADE
on UPDATE CASCADE
);

Page 1|8
M. Chinyuku Database Systems 2028

3. List course details without a lecturer

SELECT Course_ID,Course_Name
FROM course
where Lecturer_ID IS NULL;

(INNER) JOIN
• Returns records (rows) that have matching values in both tables
• Alias Join, or natural Join

Syntax- JOIN
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

SELECT *
FROM lecturer
INNER JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

4. Give a list of all lecturer names that teach at least one course

SELECT Lecturer_Name
FROM lecturer
INNER JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

Page 2|8
M. Chinyuku Database Systems 2028

5. List the lecturer names and course details of lecturers who teach at least one course.

SELECT Lecturer_Name,Course_ID, Course_Name


FROM lecturer
INNER JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

6. List all lecturer names and the names of the courses they teach

SELECT lecturer.Lecturer_Name, course.Course_Name


FROM lecturer
INNER JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

7. What is the name of the lecturer that teaches Programming?

SELECT lecturer.Lecturer_Name
FROM lecturer
INNER JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID
where Course_Name = "Programming";

Page 3|8
M. Chinyuku Database Systems 2028

LEFT (OUTER) JOIN


• Return all records from the left table, and the matched records from the right table

Syntax – LEFT JOIN


SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;

SELECT *
FROM lecturer
LEFT JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

8. List the names of lecturers who do not teach any course

SELECT lecturer.Lecturer_Name
FROM lecturer
LEFT JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID
where Course_ID is null;

Page 4|8
M. Chinyuku Database Systems 2028

RIGHT (OUTER) JOIN


• Return all records from the right table, and the matched records from the left table

Syntax – RIGHT JOIN


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

SELECT *
FROM lecturer
RIGHT JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

9. List all courses that do not have lecturers

SELECT course.Course_Name
FROM lecturer
RIGHT JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID
WHERE course.Lecturer_ID is NULL ;

10. List all courses that have lecturers

SELECT course.Course_Name
FROM lecturer
RIGHT JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID
WHERE course.Lecturer_ID is NOT NULL;

Page 5|8
M. Chinyuku Database Systems 2028

11. List all courses and their lecturer names

SELECT course.Course_ID, course.Course_Name, lecturer.Lecturer_Name


FROM lecturer
RIGHT JOIN course
ON lecturer.Lecturer_ID = course.Lecturer_ID;

FULL (OUTER) JOIN


• Return all records when there is a match in either left or right table
• Combines both left and right joins

Syntax- FULL JOIN


SELECT *
FROM table1
LEFT JOIN table 2 ON table1.common_field = table2.common_field
UNION
SELECT *
FROM table1
RIGHT JOIN table 2 ON table1.common_field = table2.common_field;

Page 6|8
M. Chinyuku Database Systems 2028

SELECT * FROM lecturer


LEFT JOIN course ON lecturer.Lecturer_ID = course.Lecturer_ID
UNION
SELECT * FROM lecturer
RIGHT JOIN course ON lecturer.Lecturer_ID = course.Lecturer_ID;

SELECT * FROM course


LEFT JOIN lecturer ON course.Lecturer_ID = lecturer.Lecturer_ID
UNION
SELECT * FROM course
RIGHT JOIN lecturer ON course.Lecturer_ID = lecturer.Lecturer_ID;

General syntax of the SELECT statement used in DML

Summary
• JOINS allow to combine data from more than one table into a single result set.

• JOINS have better performance compared to sub queries

• INNER JOINS only return rows that meet the given criteria.

• OUTER JOINS can also return rows where no matches have been found. The unmatched
rows are returned with the NULL keyword.

Page 7|8
M. Chinyuku Database Systems 2028

• The major JOIN types include Inner, Left Outer, Right Outer, Cross JOINS etc.

• The frequently used clause in JOIN operations is "ON". "USING" clause requires that
matching columns be of the same name.

• JOINS can also be used in other clauses such as GROUP BY, WHERE, SUB QUERIES,
AGGREGATE FUNCTIONS etc.

SQL statement processing order (adapted from van der Lans, 2006, p. 100)

Page 8|8

You might also like