Advertisement
makispaiktis

Query 5 - Joins

Jun 6th, 2023
1,184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.83 KB | Source Code | 0 0
  1. USE [AdventureWorks2012]
  2.  
  3. -- *********** TABLES ***********
  4.  
  5. -- Table 1 - Employee
  6. CREATE TABLE MyEmployee(EmployeeID INT, FirstName VARCHAR(30), LastName VARCHAR(30))
  7. SELECT * FROM MyEmployee
  8.  
  9. INSERT INTO MyEmployee
  10. VALUES(1, 'Michael', 'Scott')
  11. INSERT INTO MyEmployee
  12. VALUES(2, 'Pam', 'Beesly')
  13. INSERT INTO MyEmployee
  14. VALUES(3, 'Dwight', 'Schrute')
  15. SELECT * FROM MyEmployee
  16.  
  17. -- Table 2 - Salary
  18. CREATE TABLE MySalary(EmployeeID INT, Salary FLOAT)
  19. SELECT * FROM MySalary
  20.  
  21. INSERT INTO MySalary
  22. VALUES(1, 10000)
  23. INSERT INTO MySalary
  24. VALUES(2, 8000)
  25. INSERT INTO MySalary
  26. VALUES(3, 6000)
  27. SELECT * FROM MySalary
  28.  
  29. SELECT * FROM MyEmployee
  30. SELECT * FROM MySalary
  31.  
  32. -- Table 3 - Phone
  33. CREATE TABLE MyPhone(EmploeeID INT, PhoneNumber VARCHAR(30))
  34. INSERT INTO MyPhone
  35. VALUES(1, '2410-111-000')
  36. INSERT INTO MyPhone
  37. VALUES(2, '2310-555-222')
  38. SELECT * FROM MyPhone
  39.  
  40. -- Table 4 - Parking
  41. CREATE TABLE MyParking(EmployeeID INT, ParkingSpot VARCHAR(30))
  42. INSERT INTO MyParking
  43. VALUES(1, 'A1')
  44. INSERT INTO MyParking
  45. VALUES(2, 'A2')
  46. SELECT * FROM MyParking
  47.  
  48. -- Table 5 - Customer
  49. CREATE TABLE MyCustomer(CustomerID INT, CustomerName VARCHAR(30))
  50. INSERT INTO MyCustomer
  51. VALUES(1, 'Rakesh')
  52. INSERT INTO MyCustomer
  53. VALUES(3, 'Jonh')
  54. SELECT * FROM MyCustomer
  55.  
  56. -- Table 6 - Order
  57. CREATE TABLE MyOrder(OrderNumber INT, OrderName VARCHAR(30), CustomerID INT)
  58. INSERT INTO MyOrder
  59. VALUES(1, 'Order1', 1)
  60. INSERT INTO MyOrder
  61. VALUES(2, 'Order2', 2)
  62. INSERT INTO MyOrder
  63. VALUES(3, 'Order3', 7)
  64. INSERT INTO MyOrder
  65. VALUES(4, 'Order4', 8)
  66. SELECT * FROM MyOrder
  67.  
  68.  
  69.  
  70. -- *********** JOINS ***********
  71. -- 1. Inner Join
  72. SELECT * FROM MyEmployee A
  73. INNER JOIN MySalary B
  74. ON A.EmployeeID = B.EmployeeID
  75.  
  76. SELECT A.FirstName, A.LastName, B.Salary FROM MyEmployee A
  77. INNER JOIN MySalary B
  78. ON A.EmployeeID = B.EmployeeID
  79.  
  80. -- 2. Left outer Join
  81. SELECT * FROM MyEmployee
  82. SELECT * FROM MyPhone
  83.  
  84. SELECT * FROM MyEmployee A
  85. LEFT JOIN MyPhone B
  86. ON A.EmployeeID = B.EmploeeID
  87.  
  88. SELECT A.FirstName, A.LastName, B.PhoneNumber FROM MyEmployee A
  89. LEFT JOIN MyPhone B
  90. ON A.EmployeeID = B.EmploeeID
  91.  
  92. -- 3. Right outer Join
  93. SELECT * FROM MyParking
  94. SELECT * FROM MyEmployee
  95.  
  96. SELECT * FROM MyParking A
  97. RIGHT JOIN MyEmployee B
  98. ON A.EmployeeID = B.EmployeeID
  99.  
  100. SELECT A.ParkingSpot, B.FirstName, B.LastName FROM MyParking A
  101. RIGHT JOIN MyEmployee B
  102. ON A.EmployeeID = B.EmployeeID
  103.  
  104. -- 4. Full outer join
  105. SELECT * FROM MyCustomer
  106. SELECT * FROM MyOrder
  107.  
  108. SELECT * FROM MyCustomer A
  109. FULL OUTER JOIN MyOrder B
  110. ON A.CustomerID = B.CustomerID
  111.  
  112. SELECT A.CustomerID, A.CustomerName, B.OrderName, B.OrderNumber FROM MyCustomer A
  113. FULL OUTER JOIN MyOrder B
  114. ON A.CustomerID = B.CustomerID
  115.  
  116.  
  117. -- 5. Cross join (like cross product)
  118. SELECT * FROM MyCustomer
  119. SELECT * FROM MySalary
  120.  
  121. SELECT * FROM MyCustomer A
  122. CROSS JOIN MySalary B
  123.  
  124. SELECT * FROM MyCustomer, MySalary
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement