Advertisement
vvccs

ADS_2_SET_JOIN

Oct 15th, 2024 (edited)
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.68 KB | None | 0 0
  1. ORACLE
  2.  
  3. -- Create tables
  4. CREATE TABLE Cust_Info(
  5.  CustomerID INT PRIMARY KEY,
  6.  CustomerName VARCHAR2(50),
  7.  LastName VARCHAR2(50),
  8.  Country VARCHAR2(50),
  9.  Age INT,
  10.  Phone VARCHAR2(15)
  11. );
  12.  
  13. drop table Cust_Info;
  14.  
  15. -- Insert data into tables
  16. -- Insert data into Cust_Info
  17. INSERT INTO Cust_Info (CustomerID, CustomerName, LastName, Country, Age, Phone)
  18. VALUES (1001, 'John', 'Doe', 'USA', 30, '1111111111');
  19.  
  20. INSERT INTO Cust_Info (CustomerID, CustomerName, LastName, Country, Age, Phone)
  21. VALUES (1002, 'Jane', 'Smith', 'Canada', 25, '2222222222');
  22.  
  23. INSERT INTO Cust_Info (CustomerID, CustomerName, LastName, Country, Age, Phone)
  24. VALUES (1003, 'Alice', 'Johnson', 'UK', 28, '3333333333');
  25.  
  26. INSERT INTO Cust_Info (CustomerID, CustomerName, LastName, Country, Age, Phone)
  27. VALUES (1004, 'Bob', 'Brown', 'USA', 35, '4444444444');
  28.  
  29. -- UNION
  30. SELECT CustomerID, CustomerName, Country FROM Cust_Info
  31. UNION
  32. SELECT CustomerID, CustomerName, Country FROM Customer_Other;
  33.  
  34. -- UNION ALL
  35. SELECT CustomerID, CustomerName, Country FROM Cust_Info
  36. UNION ALL
  37. SELECT CustomerID, CustomerName, Country FROM Customer_Other;
  38.  
  39. -- INTERSECT
  40. SELECT CustomerID, CustomerName, Country FROM Cust_Info
  41. INTERSECT
  42. SELECT CustomerID, CustomerName, Country FROM Customer_Other;
  43.  
  44. -- MINUS
  45. SELECT CustomerID, CustomerName, Country FROM Cust_Info
  46. MINUS
  47. SELECT CustomerID, CustomerName, Country FROM Customer_Other;
  48.  
  49. -- Create Orders table
  50. CREATE TABLE Orders (
  51.  OrderID INT PRIMARY KEY,
  52.  CustomerID INT,
  53.  Product VARCHAR2(100),
  54.  FOREIGN KEY (CustomerID) REFERENCES Cust_Info(CustomerID)
  55. );
  56.  
  57. -- Drop table Orders
  58. DROP TABLE Orders;
  59.  
  60. -- Insert data into Orders table
  61. INSERT INTO Orders (OrderID, CustomerID, Product)
  62. VALUES (2003, 1003, 'Headphones');
  63.  
  64. INSERT INTO Orders (OrderID, CustomerID, Product)
  65. VALUES (2001, 1001, 'Laptop');
  66.  
  67. INSERT INTO Orders (OrderID, CustomerID, Product)
  68. VALUES (2002, 1002, 'Smartphone');
  69.  
  70.  
  71. -- INNER JOIN
  72. SELECT Cust_Info.CustomerID, Cust_Info.CustomerName, Orders.Product
  73. FROM Cust_Info
  74. INNER JOIN Orders ON Cust_Info.CustomerID = Orders.CustomerID;
  75.  
  76. -- LEFT JOIN
  77. SELECT Cust_Info.CustomerID, Cust_Info.CustomerName, Orders.Product
  78. FROM Cust_Info
  79. LEFT JOIN Orders ON Cust_Info.CustomerID = Orders.CustomerID;
  80.  
  81. -- RIGHT JOIN
  82. SELECT Cust_Info.CustomerID, Cust_Info.CustomerName, Orders.Product
  83. FROM Cust_Info
  84. RIGHT JOIN Orders ON Cust_Info.CustomerID = Orders.CustomerID;
  85.  
  86. -- FULL OUTER JOIN
  87. SELECT Cust_Info.CustomerID, Cust_Info.CustomerName, Orders.OrderID
  88. FROM Cust_Info
  89. FULL OUTER JOIN Orders ON Cust_Info.CustomerID = Orders.CustomerID;
  90.  
  91. -- CROSS JOIN
  92. SELECT Cust_Info.CustomerID, Cust_Info.CustomerName, Orders.OrderID
  93. FROM Cust_Info
  94. CROSS JOIN Orders;
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement