3 ms·
There's no reason ever to use a right join. I consider those to be a code smell.
by bhousel 15y ago
There's no reason ever to use a right join. I consider those to be a code smell.
- sesqu 15y agoThere's about as much reason to use a right join as there is to use a left join.
- endersshadow 15y agoI don't think I've ever used a right join in a production query (perhaps a couple of times when I was doing some digging and it was easier), but I don't understand your statement. Left joins are insanely useful. For example, getting all the sales for a salesperson: select sp.Name, sum(s.Amount) as TotalSales from SalesPerson sp left join Sales s on sp.SalesPersonKey = s.SalesPersonKey group by sp.Name order by TotalSales desc If you did a correlated subquery, it would take a ton of time to complete (`select sum(Amount) from Sales s where s.SalesPersonKey = sp.SalesPersonKey`), especially on large tables. Left joins (and full outer joins) are plenty useful and I use them almost daily. Care to explain what you mean?
- ckuehne 15y agoWhat he (probably) means is that the following query returns the same result as your query: select sp.Name, sum(s.Amount) as TotalSales from Sales s RIGHT join SalesPerson sp on sp.SalesPersonKey = s.SalesPersonKey group by sp.Name order by TotalSales desc EDIT: Formatting.
- jtheory 15y agoThat's what was meant, I assume, but it's misguided. Right joins don't create a "code smell" for technical reasons, but for practical reasons. Because left joins are the standard approach, if you're using a non-standard approach to a common problem you'd better have a damned good reason (because you're just confusing maintenance programmers... and thus causing errors... with no justification, otherwise).
- bhousel 15y agoYes, but one of them means that you're thinking about the problem backwards.
- gnaritas 15y agoDitto.
- og1 15y agoAgree with this as well. Forgot exactly what one was and googled it to make sure. The example that is used here would indicate orphaned records. http://www.w3schools.com/sql/sql_join_right.asp http://www.w3schools.com/sql/sql_join_right.asp