Advertisement
hadimaster65555

SQL - Introduction to JOIN and UNION Script by HadiMaster

Mar 31st, 2024
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 3.98 KB | Source Code | 0 0
  1. -- Create table for Employees
  2. CREATE TABLE Employees (
  3.     EmployeeID INT PRIMARY KEY,
  4.     FirstName VARCHAR(50),
  5.     LastName VARCHAR(50),
  6.     DepartmentID INT
  7. );
  8.  
  9. -- Populate Employees table with sample data
  10. INSERT INTO Employees (EmployeeID, FirstName, LastName, DepartmentID)
  11. VALUES
  12.     (1, 'John', 'Smith', 101),
  13.     (2, 'Jane', 'Doe', 102),
  14.     (3, 'Michael', 'Johnson', 101),
  15.     (4, 'Emily', 'Brown', 103),
  16.     (5, 'David', 'Davis', 102),
  17.     (6, 'Sarah', 'Wilson', 103),
  18.     (7, 'James', 'Jones', 101),
  19.     (8, 'Jennifer', 'Lee', 103),
  20.     (9, 'Christopher', 'Anderson', 102),
  21.     (10, 'Amanda', 'Martinez', 101),
  22.     (11, 'Matthew', 'Taylor', 103),
  23.     (12, 'Ashley', 'White', 102),
  24.     (13, 'Ryan', 'Jackson', 101),
  25.     (14, 'Jessica', 'Harris', 103),
  26.     (15, 'Daniel', 'Thompson', 102),
  27.     (16, 'Melissa', 'Clark', 101),
  28.     (17, 'Kevin', 'Thomas', 103),
  29.     (18, 'Elizabeth', 'Lewis', 102),
  30.     (19, 'Eric', 'Walker', 101),
  31.     (20, 'Samantha', 'Hall', 103);
  32.  
  33. -- Create table for Departments
  34. CREATE TABLE Departments (
  35.     DepartmentID INT PRIMARY KEY,
  36.     DepartmentName VARCHAR(50)
  37. );
  38.  
  39. -- Populate Departments table with sample data
  40. INSERT INTO Departments (DepartmentID, DepartmentName)
  41. VALUES
  42.     (101, 'HR'),
  43.     (102, 'Finance'),
  44.     (103, 'Marketing'),
  45.     (104, 'IT'),
  46.     (105, 'Operations'),
  47.     (106, 'Sales'),
  48.     (107, 'Research'),
  49.     (108, 'Customer Service'),
  50.     (109, 'Legal'),
  51.     (110, 'Administration');
  52.  
  53. -- Create table for Orders
  54. CREATE TABLE Orders (
  55.     OrderID INT PRIMARY KEY,
  56.     CustomerID INT,
  57.     OrderDate DATE,
  58.     TotalAmount DECIMAL(10, 2)
  59. );
  60.  
  61. -- Populate Orders table with sample data
  62. INSERT INTO Orders (OrderID, CustomerID, OrderDate, TotalAmount)
  63. VALUES
  64.     (1, 101, '2023-01-01', 1000),
  65.     (2, 102, '2023-01-02', 1500),
  66.     (3, 103, '2023-01-03', 2000),
  67.     (4, 104, '2023-01-04', 1200),
  68.     (5, 105, '2023-01-05', 1800),
  69.     (6, 106, '2023-01-06', 900),
  70.     (7, 107, '2023-01-07', 1300),
  71.     (8, 108, '2023-01-08', 1750),
  72.     (9, 109, '2023-01-09', 1100),
  73.     (10, 110, '2023-01-10', 1600),
  74.     (11, 101, '2023-01-11', 1400),
  75.     (12, 102, '2023-01-12', 1900),
  76.     (13, 103, '2023-01-13', 2200),
  77.     (14, 104, '2023-01-14', 1350),
  78.     (15, 105, '2023-01-15', 1950),
  79.     (16, 106, '2023-01-16', 950),
  80.     (17, 107, '2023-01-17', 1200),
  81.     (18, 108, '2023-01-18', 1650),
  82.     (19, 109, '2023-01-19', 1000),
  83.     (20, 110, '2023-01-20', 1500);
  84.  
  85. -- INNER JOIN
  86. SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
  87. FROM Employees
  88. INNER JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  89.  
  90. -- LEFT JOIN
  91. SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
  92. FROM Employees
  93. LEFT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  94.  
  95. -- RIGHT JOIN
  96. SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
  97. FROM Employees
  98. RIGHT JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  99.  
  100. -- FULL JOIN
  101. SELECT Employees.FirstName, Employees.LastName, Departments.DepartmentName
  102. FROM Employees
  103. FULL JOIN Departments ON Employees.DepartmentID = Departments.DepartmentID;
  104.  
  105. -- SEMI JOIN
  106. SELECT Employees.FirstName, Employees.LastName
  107. FROM Employees
  108. WHERE EXISTS (
  109.     SELECT 1
  110.     FROM Departments
  111.     WHERE Employees.DepartmentID = Departments.DepartmentID
  112. );
  113.  
  114. -- ANTI JOIN
  115. SELECT Employees.FirstName, Employees.LastName
  116. FROM Employees
  117. WHERE NOT EXISTS (
  118.     SELECT 1
  119.     FROM Departments
  120.     WHERE Employees.DepartmentID = Departments.DepartmentID
  121. );
  122.  
  123. -- UNION and UNION ALL
  124.  
  125. -- UNION
  126. SELECT OrderID, CustomerID, OrderDate, TotalAmount
  127. FROM Orders
  128. WHERE TotalAmount > 1500
  129.  
  130. UNION
  131.  
  132. SELECT OrderID, CustomerID, OrderDate, TotalAmount
  133. FROM Orders
  134. WHERE OrderDate > '2023-01-10';
  135.  
  136. -- UNION ALL
  137. SELECT OrderID, CustomerID, OrderDate, TotalAmount
  138. FROM Orders
  139. WHERE TotalAmount > 1500
  140.  
  141. UNION ALL
  142.  
  143. SELECT OrderID, CustomerID, OrderDate, TotalAmount
  144. FROM Orders
  145. WHERE OrderDate > '2023-01-10';
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement