SQL Part 5 Practice Questions and Answers
SQL Part 5 Practice Questions and Answers
StudentName VARCHAR(100),
Age INT
);
Table 2: Courses
This table contains information about courses students have enrolled in.
StudentID INT,
CourseName VARCHAR(100),
(101, 1, 'Math'),
(102, 2, 'Science'),
(103, 3, 'History'),
(104, 5, 'English'),
(105, 5, 'Physics'),
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.
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.
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.
Answer:
StudentName CourseName
Alice Math
Bob Science
Charlie History
Eve English
Eve Physics
NULL Chemistry
Question: Write a query to fetch all students and courses, including students
without any course and courses without any student.
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.
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:
Result