0

Given that the total number of rows returned by this query below must equal the number of rows in the Invoices table

SELECT VendorName, InvoiceNumber
FROM Invoices
LEFT JOIN Vendors ON Invoices.VendorsID = Vendors.VendorID

Why doesn't the total number of rows returned by this query equal the number of rows in the Vendors table when you replace LEFT keyword with RIGHT keyword?

3
  • I think you are mistaken. Can you provide proof using sqlfiddle.com? Commented Oct 6, 2015 at 21:06
  • 3
    blog.codinghorror.com/a-visual-explanation-of-sql-joins/… A right join is just like a left join only backwards. In nearly 20 years with sql server I have used a right join maybe once or twice in actual business code.
    – Sean Lange
    Commented Oct 6, 2015 at 21:09
  • Okay, this question is not clear. It appears that you are saying that you KNOW what a RIGHT join does but for some reason when you use it you are not seeing all the rows from the Vendors table. Or are you simply asking what the difference is between a LEFT and RIGHT join? Commented Oct 6, 2015 at 21:14

3 Answers 3

2

Your vendors table is the dependent table in a 1-to-Many relationship, so when you execute a RIGHT join on Invoices and Vendors, you will get 1 row for every Invoice + Vendor combination in the relationship, plus 1 row for every Vendor that does not have an invoice record in the Invoices table.

So, Let's say you have three vendors, and among them there are three invoices.

Vendor 1 has 2 invoices
Vendor 2 has 1 invoice
Vendor 3 has 0 invoices

With this data, a RIGHT JOIN will return 4 rows: two rows for Vendor 1, one row for Vendor 2, and 1 row for Vendor 3.

This fiddle provides an example of what I've described above.

2

The simple answer is because it would mean every record on the "right" side (in this case Vendors) would be represented in the results.

So if you have a vendor which does not have any invoices, it will show in the RIGHT JOIN, but will not show in the LEFT JOIN (because there is no match on the "left" side table).

2
  • Is that what he's asking? He says "Why doesn't the total number of rows returned equal the number of rows in the Vendors table when I replace LEFT with RIGHT?" Commented Oct 6, 2015 at 21:17
  • @robert_difalco - I believe I explained that. Without seeing the data I would assume every invoice would have a vendor (integrity of an order), but not every vendor would have an invoice which is why the right join would have more records than the left. Commented Oct 6, 2015 at 21:25
1

The right join ensures that all the rows from the right table are present in the results.

But if there are 3 invoices for the same vendor than you still get 3 rows in the result set, not 1. That's how joins work :)

2
  • Actually in your example, you would still get 3 results since each invoice linked to the vendor would return. Where you get a difference is when there are vendors that do not have an invoice associated with them. Commented Oct 6, 2015 at 21:27
  • @JasonFaulkner you should definitely read OP once more, quote: Why doesn't the total number of rows returned by this query equal the number of rows in the Vendors table -- there is no such word less records or more records.
    – GSazheniuk
    Commented Oct 6, 2015 at 23:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.