Table 1: Students
This table contains information about students.
CREATE TABLE Students (
StudentID INT PRIMARY KEY,
StudentName VARCHAR(100),
Age INT
);
INSERT INTO Students (StudentID, StudentName, Age) VALUES
(1, 'Alice', 20),
(2, 'Bob', 22),
(3, 'Charlie', 21),
(4, 'David', 23),
(5, 'Eve', 19);
Table 2: Courses
This table contains information about courses students have enrolled in.
CREATE TABLE Courses (
CourseID INT PRIMARY KEY,
StudentID INT,
CourseName VARCHAR(100),
FOREIGN KEY (StudentID) REFERENCES Students(StudentID)
);
INSERT INTO Courses (CourseID, StudentID, CourseName) VALUES
(101, 1, 'Math'),
(102, 2, 'Science'),
(103, 3, 'History'),
(104, 5, 'English'),
(105, 5, 'Physics'),
(106, NULL, 'Chemistry'); -- No student enrolled in Chemistry
Questions and Answers for Joins
1. Inner Join:
Question: Write a query to fetch all students along with the courses they are
enrolled in, displaying only students who are enrolled in at least one course.
SELECT Students.StudentName, Courses.CourseName
FROM Students
INNER JOIN Courses ON Students.StudentID = Courses.StudentID;
Answer:
StudentName CourseName
Alice Math
Bob Science
Charlie History
Eve English
StudentName CourseName
Eve Physics
2. Left Join:
Question: Write a query to fetch all students along with the courses they are
enrolled in, including students who are not enrolled in any course.
SELECT Students.StudentName, Courses.CourseName
FROM Students
LEFT JOIN Courses ON Students.StudentID = Courses.StudentID;
Answer:
StudentName CourseName
Alice Math
Bob Science
Charlie History
David NULL
Eve English
Eve Physics
3. Right Join:
Question: Write a query to fetch all courses along with the names of the students
enrolled in them, including courses without any students enrolled.
SELECT Students.StudentName, Courses.CourseName
FROM Students
RIGHT JOIN Courses ON Students.StudentID = Courses.StudentID;
Answer:
StudentName CourseName
Alice Math
Bob Science
Charlie History
Eve English
Eve Physics
NULL Chemistry
4. Full Outer Join:
Question: Write a query to fetch all students and courses, including students
without any course and courses without any student.
SELECT Students.StudentName, Courses.CourseName
FROM Students
FULL OUTER JOIN Courses ON Students.StudentID = Courses.StudentID;
Answer:
StudentName CourseName
Alice Math
Bob Science
Charlie History
David NULL
Eve English
Eve Physics
NULL Chemistry
5. Self Join:
To ensure that the self-join query produces results, we will insert a new student
record with the same age as an existing student.
-- Insert a new student with the same age as 'Charlie'
INSERT INTO Students (StudentID, StudentName, Age) VALUES (6, 'Frank',
21);
Question: Write a query to Find Pairs of Students with the Same Age
Now that we have two students (Charlie and Frank) with the same age, the self-join
query will produce results:
SELECT A.StudentName AS Student1, B.StudentName AS Student2, A.Age
FROM Students A, Students B
WHERE A.Age = B.Age AND A.StudentID <> B.StudentID;
Result
Student1 Student2 Age
Charlie Frank 21
Frank Charlie 21