vvccs

8

Apr 26th, 2024
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. CREATE TABLE Orders (
  2. OrderID INT PRIMARY KEY,
  3. CustomerID INT,
  4. OrderDate DATE,
  5. TotalAmount DECIMAL,
  6. FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)
  7. );
  8.  
  9. CREATE TABLE Customers (
  10. CustomerID INT PRIMARY KEY,
  11. CustomerName VARCHAR(255),
  12. City VARCHAR(255),
  13. Country VARCHAR(255)
  14. );
  15.  
  16. -- Inserting sample data into Customers table
  17. INSERT INTO Customers VALUES(1, 'John Doe', 'New York', 'USA');
  18. INSERT INTO Customers VALUES(2, 'Jane Smith', 'Los Angeles', 'USA');
  19. INSERT INTO Customers VALUES(3, 'Alice Johnson', 'London', 'UK');
  20. INSERT INTO Customers VALUES(4, 'Bob Brown', 'Sydney', 'Australia');
  21. INSERT INTO Customers VALUES(5, 'Emma Lee', 'Toronto', 'Canada');
  22. SELECT *FROM CUSTOMERS;
  23.  
  24. -- Inserting sample data into Orders table
  25. INSERT INTO Orders VALUES(101, 1,DATE '2024-01-01', 100.50);
  26. INSERT INTO Orders VALUES(102, 1,DATE '2024-01-05', 200.75);
  27. INSERT INTO Orders VALUES(103, 2,DATE '2024-01-10', 150.00);
  28. INSERT INTO Orders VALUES(104, 3,DATE '2024-01-15', 300.25);
  29. INSERT INTO Orders VALUES(105, 4,DATE '2024-01-20', 50.50);
  30. SELECT *FROM ORDERS;
  31.  
  32. --------------------------------------------------------------------------------------------------------------------------------------
  33.  
  34. Cross Join
  35.  
  36. SELECT *
  37. FROM Orders
  38. CROSS JOIN Customers;
  39.  
  40. Self Join
  41.  
  42. SELECT c1.CustomerName AS Customer, c2.CustomerName AS ReferredBy
  43. FROM Customers c1
  44. INNER JOIN Customers c2 ON c1.CustomerID <> c2.CustomerID;
  45.  
  46. Inner Join
  47.  
  48. SELECT o.OrderID, c.CustomerName, o.TotalAmount
  49. FROM Orders o
  50. INNER JOIN Customers c ON o.CustomerID = c.CustomerID;
  51.  
  52. Natural Join
  53.  
  54. SELECT OrderID, CustomerName, TotalAmount
  55. FROM Orders
  56. NATURAL JOIN Customers;
  57.  
  58. Left Outer Join
  59.  
  60. SELECT o.OrderID, c.CustomerName, o.TotalAmount
  61. FROM Orders o
  62. LEFT OUTER JOIN Customers c ON o.CustomerID = c.CustomerID;
  63.  
  64. Right Outer Join
  65.  
  66. SELECT o.OrderID, c.CustomerName, o.TotalAmount
  67. FROM Orders o
  68. RIGHT OUTER JOIN Customers c ON o.CustomerID = c.CustomerID;
  69.  
  70. Full Outer join
  71.  
  72. SELECT o.OrderID, c.CustomerName, o.TotalAmount
  73. FROM Orders o
  74. FULL OUTER JOIN Customers c ON o.CustomerID = c.CustomerID;
  75.  
  76.  
Add Comment
Please, Sign In to add comment