Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. ## A tricky left join
  2.  
  3. Consider the following two queries, one that uses an inner join and one that uses a left join
  4.  
  5. ```sql
  6. -- A. Inner join with a where condition
  7.  
  8. select foo.*, bar.*
  9. from foo
  10. join bar
  11. on foo.x = bar.x
  12. where bar.y = 'my_favorite_y'
  13.  
  14. -- B. Left join with a where condition
  15.  
  16. select foo.*, bar.*
  17. from foo
  18. left join bar
  19. on foo.x = bar.x
  20. where bar.y = 'my_favorite_y'
  21. ```
  22.  
  23. It turns out that even though query B uses a left join, the two queries will return the same results!
  24.  
  25. ## WAT
  26.  
  27. (TODO)
  28.  
  29. ## The solution
  30.  
  31. ```sql
  32. select foo.*, bar.*
  33. from foo
  34. left join bar
  35. on (foo.x = bar.x and bar.y = 'my_favorite_y')
  36. ```
  37.  
  38. (TODO)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement