Advertisement
sunraycafe

SQL Joins

Nov 14th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.18 KB | None | 0 0
  1. /*  Behavior of Joins by Type (Inner, Left Outer, Right Outer, Full Outer)  */
  2.  
  3. /*  Build Table1 (left) and Table2 (right) and populate
  4.     Note particular exclusion of certain records, to show effect of imbalance by join type */
  5.  
  6. declare @table1 table (id int, letter char)
  7. insert into @table1 values (1, 'a')
  8. insert into @table1 values (2, 'b')
  9. --insert into @table1 values (3, 'c')
  10. insert into @table1 values (4, 'd')
  11. insert into @table1 values (5, 'e')
  12. insert into @table1 values (6, 'f')
  13. --insert into @table1 values (7, 'g')
  14. insert into @table1 values (8, 'h')
  15.  
  16. declare @table2 table (id int, letter char)
  17. insert into @table2 values (1, 'z')
  18. insert into @table2 values (2, 'y')
  19. insert into @table2 values (3, 'x')
  20. insert into @table2 values (4, 'w')
  21. --insert into @table2 values (5, 'v')
  22. insert into @table2 values (6, 'u')
  23. insert into @table2 values (7, 't')
  24. insert into @table2 values (8, 's')
  25.  
  26.  
  27. -- Select all records from each table to show full data set
  28.  
  29. select * from @table1
  30.  
  31. /*  Result:
  32. id          letter
  33. ----------- ------
  34. 1           a
  35. 2           b
  36. 4           d
  37. 5           e
  38. 6           f
  39. 8           h
  40. */
  41.  
  42. select * from @table2
  43.  
  44. /* Result:
  45. id          letter
  46. ----------- ------
  47. 1           z
  48. 2           y
  49. 3           x
  50. 4           w
  51. 6           u
  52. 7           t
  53. 8           s
  54. */
  55.  
  56. -- Inner Join - Select only record that exist in both tables
  57. select * from @table1 t1 inner join @table2 t2 on t1.id = t2.id
  58.  
  59. /* Result:
  60. id          letter id          letter
  61. ----------- ------ ----------- ------
  62. 1           a      1           z
  63. 2           b      2           y
  64. 4           d      4           w
  65. 6           f      6           u
  66. 8           h      8           s
  67. */
  68.  
  69. -- Left Outer Join - Select everything from Table1, regardless of whether it has a match in Table2
  70. select * from @table1 t1 left outer join @table2 t2 on t1.id = t2.id
  71.  
  72. /* Result:
  73. id          letter id          letter
  74. ----------- ------ ----------- ------
  75. 1           a      1           z
  76. 2           b      2           y
  77. 4           d      4           w
  78. 5           e      NULL        NULL
  79. 6           f      6           u
  80. 8           h      8           s
  81. */
  82.  
  83. -- Right Outer Join - Select everything from Table2, regardless of whether it has a match in Table1
  84. select * from @table1 t1 right outer join @table2 t2 on t1.id = t2.id
  85.  
  86. /* Result:
  87. id          letter id          letter
  88. ----------- ------ ----------- ------
  89. 1           a      1           z
  90. 2           b      2           y
  91. NULL        NULL   3           x
  92. 4           d      4           w
  93. 6           f      6           u
  94. NULL        NULL   7           t
  95. 8           h      8           s
  96. */
  97.  
  98. -- Full Outer Join - Select everything from both tables, lining up whatever has a match
  99. select * from @table1 t1 full join @table2 t2 on t1.id = t2.id
  100.  
  101. /* Result:
  102. id          letter id          letter
  103. ----------- ------ ----------- ------
  104. 1           a      1           z
  105. 2           b      2           y
  106. 4           d      4           w
  107. 5           e      NULL        NULL
  108. 6           f      6           u
  109. 8           h      8           s
  110. NULL        NULL   3           x
  111. NULL        NULL   7           t
  112. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement