Joining tables is an essential skill in relational database management. It allows you to combine data from multiple tables based on relationships defined by keys. In this lab, you will learn how to use different types of joins, including INNER JOIN
, LEFT JOIN
, and RIGHT JOIN
, to retrieve meaningful data from related tables in MySQL.
By the end of this lab, you will:
INNER JOIN
to retrieve matching rows from multiple tables.LEFT JOIN
to retrieve all rows from one table and matching rows from another.RIGHT JOIN
to retrieve all rows from the second table and matching rows from the first.Open your terminal or command prompt.
Log in to the MySQL server:
mysql -u root -p
root
with your MySQL username.Create a new database for this lab:
CREATE DATABASE join_lab;
USE join_lab;
Create the Students
table:
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
Name VARCHAR(100)
);
Insert data into the Students
table:
INSERT INTO Students (StudentID, Name) VALUES
(1, 'Alice'),
(2, 'Bob'),
(3, 'Charlie');
Create the Courses
table:
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
CourseName VARCHAR(100),
StudentID INT
);
Insert data into the Courses
table:
INSERT INTO Courses (CourseID, CourseName, StudentID) VALUES
(101, 'Math', 1),
(102, 'Science', 2),
(103, 'History', 1);
Verify the data:
Students
table:
SELECT * FROM Students;
Courses
table:
SELECT * FROM Courses;
Write a query to retrieve student names and the courses they are enrolled in:
SELECT Students.Name, Courses.CourseName
FROM Students
INNER JOIN Courses
ON Students.StudentID = Courses.StudentID;
Analyze the result:
StudentID
.Expected Output:
Name | CourseName |
---|---|
Alice | Math |
Alice | History |
Bob | Science |
Write a query to retrieve all students, including those not enrolled in any courses:
SELECT Students.Name, Courses.CourseName
FROM Students
LEFT JOIN Courses
ON Students.StudentID = Courses.StudentID;
Analyze the result:
Students
table, including students without matching courses.Expected Output:
Name | CourseName |
---|---|
Alice | Math |
Alice | History |
Bob | Science |
Charlie | NULL |
Write a query to retrieve all courses, including those not assigned to any student:
SELECT Students.Name, Courses.CourseName
FROM Students
RIGHT JOIN Courses
ON Students.StudentID = Courses.StudentID;
Analyze the result:
Courses
table, including courses not linked to any student.Expected Output:
Name | CourseName |
---|---|
Alice | Math |
Alice | History |
Bob | Science |
Add an Instructors
table:
CREATE TABLE Instructors (
InstructorID INT PRIMARY KEY,
Name VARCHAR(100),
CourseID INT
);
Insert data into the Instructors
table:
INSERT INTO Instructors (InstructorID, Name, CourseID) VALUES
(1, 'Dr. Smith', 101),
(2, 'Dr. Jones', 102),
(3, 'Dr. Brown', 103);
Write a query to retrieve student names, course names, and instructor names:
SELECT Students.Name AS StudentName, Courses.CourseName, Instructors.Name AS InstructorName
FROM Students
INNER JOIN Courses ON Students.StudentID = Courses.StudentID
INNER JOIN Instructors ON Courses.CourseID = Instructors.CourseID;
Analyze the result:
Expected Output:
StudentName | CourseName | InstructorName |
---|---|---|
Alice | Math | Dr. Smith |
Alice | History | Dr. Brown |
Bob | Science | Dr. Jones |
Exit the MySQL command line:
EXIT;
Close your terminal or command prompt.
In this lab, you learned how to:
INNER JOIN
to retrieve rows with matching values in related tables.LEFT JOIN
to include all rows from one table, regardless of matches in the other.RIGHT JOIN
to include all rows from the second table, even if no match exists in the first.These skills are fundamental for querying relational databases and retrieving meaningful insights from connected data.