SQL Joins   «Prev  Next»

Lesson 7 An example of a LEFT join
Objective Understand how LEFT and RIGHT joins work in a real example

LEFT | RIGHT joins (SQL Operations)

If you use the tables we set up in the earlier examples, you can create a join of the first table (Publishers) and the second (Titles) that will show this point.
To construct SQL queries for LEFT JOIN and RIGHT JOIN with the given "Publishers Table" and "Titles Table", the common column `pub_id` in both tables serves as the key for the join. Here’s how you can structure these queries:
  1. LEFT JOIN Query:
    • The LEFT JOIN will return all records from the "Publishers Table" (left table) and the matched records from the "Titles Table" (right table). If there is no match, the result will include NULL values for the columns from the "Titles Table".
    • Query:
      SELECT Publishers.pub_id, Publishers.pub_name, Titles.title_id, Titles.price, Titles.pubdate
      FROM Publishers
      LEFT JOIN Titles ON Publishers.pub_id = Titles.pub_id;
      
    • In this query, it selects `pub_id` and `pub_name` from the Publishers Table and `title_id`, `price`, and `pubdate` from the Titles Table. The query ensures that all publishers are listed, regardless of whether they have a title in the Titles Table.
  2. RIGHT JOIN Query:
    • The RIGHT JOIN will return all records from the "Titles Table" (right table) and the matched records from the "Publishers Table" (left table). If there is no match, the result will include NULL values for the columns from the "Publishers Table".
    • Query:
      SELECT Publishers.pub_id, Publishers.pub_name, Titles.title_id, Titles.price, Titles.pubdate
      FROM Publishers
      RIGHT JOIN Titles ON Publishers.pub_id = Titles.pub_id;
      
    • This query selects the same columns as the LEFT JOIN. However, it ensures that all titles are listed, including those that may not have a corresponding publisher in the Publishers Table.

These queries are instrumental in scenarios where you need to understand the relationship between publishers and their titles, especially in cases where there are publishers without titles or titles without publishers. The LEFT JOIN is useful for a complete list of publishers and their associated titles, if any, while the RIGHT JOIN provides a full list of titles, alongside their publishers if available.

Publishers Table

pub_idpub_namecitystatecountry
0736New Moon BooksBostonMAUSA
0877Binnet & HardleyWashingtonDCUSA
1389Algodata InfosystemsBerkeleyCAUSA
1622Five Lakes PublishingChicagoILUSA
1756Ramona PublishersDallasTXUSA
9901GGG&GMünchen Germany
9952Scootney BooksNew YorkNYUSA
9999Lucerne PublishingParis France
Titles Table (a portion of the table is shown below)
title_idpub_idpricepubdate
BU1032138919.99Jun 12 1991 12:00AM
BU1111138911.95Jun 9 1991 12:00AM

The statement that you could use is
SELECT pub_ID, pub_name, title_id 
FROM publishers LEFT OUTER JOIN Titles 
ON publishers.pub_ID = titles.pub_ID 

The result would be the following:
pub_idpub_nametitle_ID
0736New Moon Books 
0877Binnet & Hardley 
1389Algodata InfosystemsBU1032
1389Algodata InfosystemsBU1111
1389Algodata Infosystems 
1622Five Lakes Publishing 
1756Ramona Publishers 
9901GGG&G 
9952Scootney Books 
9999Lucerne Publishing 

You can see that all the rows from the publishers table are included, and one row for each matching row in the titles table is created. In this case, there are only two books in the titles table, so all title IDs are blank except for those two.
Right joins work in the same way.