Advertisement
hadimaster65555

SQL - Introduction to Window Function - Aggregate Function by HadiMaster

Apr 15th, 2024
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PostgreSQL 2.82 KB | Source Code | 0 0
  1. -- Create table for salary data
  2. CREATE TABLE salary_data (
  3.     employee_id SERIAL PRIMARY KEY,
  4.     employee_name VARCHAR(100),
  5.     job_title VARCHAR(100),
  6.     salary INTEGER
  7. );
  8.  
  9. -- Insert sample data into the salary_data table
  10. INSERT INTO salary_data (employee_id, employee_name, job_title, salary) VALUES
  11. (1, 'John Doe', 'Data Scientist', 90000),
  12. (2, 'Jane Smith', 'Data Analyst', 65000),
  13. (3, 'Bob Johnson', 'Machine Learning Engineer', 100000),
  14. (4, 'Sarah Lee', 'Data Engineer', 75000),
  15. (5, 'Michael Brown', 'Data Scientist', 95000),
  16. (6, 'Emily Davis', 'Data Analyst', 60000),
  17. (7, 'Alex Wilson', 'Machine Learning Engineer', 110000),
  18. (8, 'Jessica Clark', 'Data Engineer', 70000),
  19. (9, 'David Rodriguez', 'Data Scientist', 85000),
  20. (10, 'Lisa Martinez', 'Data Analyst', 62000),
  21. (11, 'James Taylor', 'Machine Learning Engineer', 105000),
  22. (12, 'Olivia Anderson', 'Data Engineer', 72000),
  23. (13, 'William Thomas', 'Data Scientist', 92000),
  24. (14, 'Sophia White', 'Data Analyst', 63000),
  25. (15, 'Ethan Hall', 'Machine Learning Engineer', 115000),
  26. (16, 'Mia Moore', 'Data Engineer', 74000),
  27. (17, 'Jacob Garcia', 'Data Scientist', 88000),
  28. (18, 'Ava Martinez', 'Data Analyst', 61000),
  29. (19, 'Benjamin Young', 'Machine Learning Engineer', 120000),
  30. (20, 'Harper Hernandez', 'Data Engineer', 77000),
  31. (21, 'Amelia King', 'Data Scientist', 89000),
  32. (22, 'Ryan Adams', 'Data Analyst', 64000),
  33. (23, 'Zoe Rivera', 'Machine Learning Engineer', 125000),
  34. (24, 'Elijah Long', 'Data Engineer', 78000),
  35. (25, 'Layla Torres', 'Data Scientist', 91000),
  36. (26, 'Jackson Nguyen', 'Data Analyst', 63000),
  37. (27, 'Penelope Scott', 'Machine Learning Engineer', 130000),
  38. (28, 'Chloe Green', 'Data Engineer', 80000),
  39. (29, 'Sebastian Lopez', 'Data Scientist', 93000),
  40. (30, 'Aiden Hill', 'Data Analyst', 65000);
  41.  
  42. -- check data
  43. SELECT * FROM salary_data
  44.  
  45. -- average salary for each job title
  46. SELECT
  47.     employee_id,
  48.     employee_name,
  49.     job_title,
  50.     salary,
  51.     AVG(salary) OVER(PARTITION BY job_title) AS avg_salary_per_job
  52. FROM
  53.     salary_data;
  54.  
  55. -- count of employee for each job title
  56. SELECT
  57.     employee_id,
  58.     employee_name,
  59.     job_title,
  60.     salary,
  61.     COUNT(*) OVER(PARTITION BY job_title) AS num_employees_per_job
  62. FROM
  63.     salary_data;
  64.  
  65. -- minimum salary for each job title
  66. SELECT
  67.     employee_id,
  68.     employee_name,
  69.     job_title,
  70.     salary,
  71.     MIN(salary) OVER(PARTITION BY job_title) AS min_salary_per_job
  72. FROM
  73.     salary_data;
  74.  
  75. -- maximum salary for each job title
  76. SELECT
  77.     employee_id,
  78.     employee_name,
  79.     job_title,
  80.     salary,
  81.     MAX(salary) OVER(PARTITION BY job_title) AS min_salary_per_job
  82. FROM
  83.     salary_data;
  84.  
  85. -- total salary for each job title
  86. SELECT
  87.     employee_id,
  88.     employee_name,
  89.     job_title,
  90.     salary,
  91.     SUM(salary) OVER(PARTITION BY job_title) AS total_salary_per_job
  92. FROM
  93.     salary_data;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement