Instruction
Great! If there is a LEFT JOIN, there must be a RIGHT JOIN, too, right? What's the difference? Well, recall that a LEFT JOIN shows all rows from the left table, even if there are no matching rows for the
RIGHT JOIN shows all rows from the right table, even if there are no matching rows for the ON condition in the left table.
Suppose that this time, we want to show all lecturers and the courses they are teaching; if a lecturer has not yet been assigned to a course, we still want to show their name. We can do that like this:
SELECT Course.Name AS CourseName Lecturer.Name AS LecturerName FROM Course RIGHT JOIN Lecturer ON Course.LecturerId = Lecturer.Id
This example would produce the following result:
| CourseName | LecturerName |
|---|---|
| English grammar | John Martinez |
| English listening | Edward McCullen |
| History of England | Justin Haar |
| null | Marcin Wydrowski |
| null | Peter Par |
The pink rows are returned by the INNER JOIN. The blue and pink rows are returned by the RIGHT JOIN.
Exercise
We've added a new table: BookReview. It's simple and contains only three columns:
BookId- the ID of a reviewed book,Review- the summary of the review.Author- the name of the review's author.
Join the BookReview and Book tables using a RIGHT JOIN. Show the title of the book, the corresponding review, and the name of the review's author. Consider all books, even those that weren't reviewed.
Stuck? Here's a hint!
Type:
SELECT Book.Title, BookReview.Review, BookReview.Author FROM BookReview RIGHT JOIN Book ON Book.Id = BookReview.BookId



