Advertisement
hadimaster65555

SQL - Introduction to Conditional Statement and Having Syntax Script by HadiMaster

Apr 2nd, 2024 (edited)
1,222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 2.68 KB | Source Code | 0 0
  1. CREATE TABLE Employees (
  2.     EmployeeID INT,
  3.     FirstName VARCHAR(50),
  4.     LastName VARCHAR(50),
  5.     Department VARCHAR(50),
  6.     Salary INT,
  7.     YearsExperience INT
  8. );
  9.  
  10. INSERT INTO Employees (EmployeeID, FirstName, LastName, Department, Salary, YearsExperience) VALUES
  11. (1, 'John', 'Doe', 'Sales', 50000, 3),
  12. (2, 'Jane', 'Smith', 'Sales', 55000, 5),
  13. (3, 'Michael', 'Johnson', 'Sales', 60000, 7),
  14. (4, 'Emily', 'Brown', 'Marketing', 48000, 4),
  15. (5, 'David', 'Jones', 'Marketing', 52000, 6),
  16. (6, 'Sarah', 'Taylor', 'Marketing', 58000, 8),
  17. (7, 'James', 'Wilson', 'Human Resources', 55000, 5),
  18. (8, 'Jessica', 'Martinez', 'Human Resources', 60000, 6),
  19. (9, 'Christopher', 'Garcia', 'Human Resources', 62000, 8),
  20. (10, 'Melissa', 'Anderson', 'Finance', 60000, 4),
  21. (11, 'Daniel', 'Hernandez', 'Finance', 65000, 6),
  22. (12, 'Amanda', 'Lopez', 'Finance', 70000, 7),
  23. (13, 'Robert', 'Perez', 'IT', 55000, 3),
  24. (14, 'Jennifer', 'Gonzalez', 'IT', 60000, 5),
  25. (15, 'William', 'Rodriguez', 'IT', 65000, 7),
  26. (16, 'Linda', 'Lewis', 'Customer Service', 45000, 2),
  27. (17, 'Kevin', 'Lee', 'Customer Service', 50000, 4),
  28. (18, 'Ashley', 'Walker', 'Customer Service', 55000, 6),
  29. (19, 'Thomas', 'Hall', 'Operations', 60000, 3),
  30. (20, 'Karen', 'Allen', 'Operations', 65000, 5),
  31. (21, 'Mark', 'Young', 'Operations', 70000, 7),
  32. (22, 'Elizabeth', 'Harris', 'Logistics', 55000, 4),
  33. (23, 'Ryan', 'Clark', 'Logistics', 60000, 6),
  34. (24, 'Michelle', 'King', 'Logistics', NULL, 8),
  35. (25, 'Steven', 'Scott', 'Logistics', 70000, 9);
  36.  
  37. -- assign 'Junior', 'Intermediate', and 'Senior' to each employee based on
  38. -- YearsExperience
  39. SELECT
  40.     FirstName,
  41.     LastName,
  42.     YearsExperience,
  43.     CASE
  44.         WHEN YearsExperience <= 3 THEN 'Junior'
  45.         WHEN YearsExperience > 3 AND YearsExperience <= 6 THEN 'Intermediate'
  46.         ELSE 'Senior'
  47.     END AS ExperienceLevel
  48. FROM
  49.     Employees;
  50.  
  51. -- assign 'Eligible for Bonus' and 'Not Eligible for Bonus' based on
  52. -- Salary >= 60000 or below it
  53. SELECT
  54.     FirstName,
  55.     LastName,
  56.     Salary,
  57.     CASE
  58.         WHEN Salary >= 60000 THEN 'Eligible for Bonus'
  59.         ELSE 'Not Eligible for Bonus'
  60.     END AS BonusEligibility
  61. FROM
  62.     Employees;
  63.  
  64. -- assign 0 if Salary is null
  65. SELECT
  66.     EmployeeID,
  67.     FirstName,
  68.     LastName,
  69.     COALESCE(Salary, 0) AS Salary
  70. FROM
  71.     Employees;
  72.  
  73.  
  74. -- find departments with an average salary greater than 55000
  75. SELECT
  76.     Department,
  77.     AVG(Salary) AS AvgSalary
  78. FROM
  79.     Employees
  80. GROUP BY
  81.     Department
  82. HAVING
  83.     AVG(Salary) > 55000;
  84.  
  85. -- find department that has more than 3 employees
  86. SELECT
  87.     Department,
  88.     COUNT(*) AS NumEmployees
  89. FROM
  90.     Employees
  91. GROUP BY
  92.     Department
  93. HAVING
  94.     COUNT(*) > 3;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement