Skip to content

Commit a312c65

Browse files
committed
more window functions
1 parent fef4a9e commit a312c65

1 file changed

Lines changed: 61 additions & 12 deletions

File tree

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
-- VIEW ALL RECORDS
1+
-- VIEW ALL EMPLOYEE RECORDS
22
SELECT * FROM employees;
33

4-
-- MIN and MAX salary from the employees table
4+
-- CALCULATE OVERALL AVERAGE, MINIMUM, AND MAXIMUM SALARY ACROSS ALL EMPLOYEES
55
SELECT
6-
AVG(salary) AS avg_salary,
6+
AVG(salary) AS avg_salary,
77
MIN(salary) AS min_salary,
88
MAX(salary) AS max_salary
99
FROM employees;
1010

11-
-- SHOW EMPLOYEE DETAILS ALONG WITH OVERALL AVERAGE SALARY USING WINDOW FUNCTION
11+
-- SHOW EACH EMPLOYEE'S DETAILS WITH THE OVERALL AVERAGE SALARY USING A WINDOW FUNCTION
12+
-- Does not collapse rows — average appears on each row
1213
SELECT
1314
emp_no,
1415
department,
1516
salary,
1617
AVG(salary) OVER() AS avg_salary
1718
FROM employees;
1819

19-
-- AGGREGATE MIN AND MAX SALARIES (WITHOUT WINDOW FUNCTION) - RESULTS IN SINGLE ROW PER GROUP (IF GROUPED)
20-
-- results in EXCEPTION if not grouped
20+
-- ⚠️ THIS QUERY IS COMMENTED OUT BECAUSE IT WOULD THROW AN ERROR
21+
-- IT TRIES TO USE AGGREGATE FUNCTIONS WITHOUT GROUPING OR A WINDOW FUNCTION
2122
-- SELECT
2223
-- emp_no,
2324
-- department,
@@ -26,16 +27,18 @@ FROM employees;
2627
-- MAX(salary) AS max_salary
2728
-- FROM employees;
2829

29-
-- AGGREGATE MIN AND MAX SALARIES (WITHOUT WINDOW FUNCTION) - RESULTS IN SINGLE ROW PER GROUP (IF GROUPED)
30+
-- SHOW AVERAGE, MINIMUM, AND MAXIMUM SALARIES PER DEPARTMENT
31+
-- GROUPS DATA BY DEPARTMENT AND RETURNS ONE ROW PER DEPARTMENT
3032
SELECT
3133
department,
32-
AVG(salary) AS avg_salary,
34+
AVG(salary) AS avg_salary,
3335
MIN(salary) AS min_salary,
3436
MAX(salary) AS max_salary
3537
FROM employees
3638
GROUP BY department;
3739

38-
-- SHOW EMPLOYEE DETAILS ALONG WITH MIN AND MAX SALARIES ACROSS ALL ROWS
40+
-- SHOW EACH EMPLOYEE'S DETAILS ALONG WITH OVERALL MIN AND MAX SALARIES USING WINDOW FUNCTION
41+
-- MIN() and MAX() are calculated once over the full table and shown with each row
3942
SELECT
4043
emp_no,
4144
department,
@@ -44,7 +47,8 @@ SELECT
4447
MAX(salary) OVER() AS max_salary
4548
FROM employees;
4649

47-
-- SHOW EMPLOYEE DETAILS ALONG WITH MIN AND MAX SALARIES PER DEPARTMENT
50+
-- SHOW EACH EMPLOYEE'S DETAILS WITH DEPARTMENT-WISE AVERAGE, MIN, AND MAX SALARIES
51+
-- Uses PARTITION BY department to group inside the window function
4852
SELECT
4953
emp_no,
5054
department,
@@ -54,11 +58,56 @@ SELECT
5458
MAX(salary) OVER(PARTITION BY department) AS dep_max_salary
5559
FROM employees;
5660

57-
-- SHOW EMPLOYEE DETAILS ALONG WITH MIN AND MAX SALARIES PER DEPARTMENT
61+
-- SHOW EACH EMPLOYEE'S DETAILS WITH DEPARTMENT-WISE AND OVERALL PAYROLL (TOTAL SALARY SUM)
62+
-- Uses SUM() as a window function for both scoped and global totals
5863
SELECT
5964
emp_no,
6065
department,
6166
salary,
6267
SUM(salary) OVER(PARTITION BY department) AS dept_payroll,
6368
SUM(salary) OVER() AS total_payroll
64-
FROM employees;
69+
FROM employees;
70+
71+
-- SHOW EACH EMPLOYEE'S DETAILS ALONG WITH ROLLING SUM (CUMULATIVE) OF SALARIES PER DEPARTMENT
72+
-- Uses ORDER BY inside the window function to generate cumulative total by salary within each department
73+
SELECT
74+
emp_no,
75+
department,
76+
salary,
77+
SUM(salary) OVER(PARTITION BY department ORDER BY salary) AS rolling_dept_payroll,
78+
SUM(salary) OVER(PARTITION BY department) AS dept_payroll
79+
FROM employees;
80+
81+
-- SHOW EACH EMPLOYEE'S DETAILS WITH DEPARTMENT-WISE ROLLING MINIMUM SALARY IN DESCENDING ORDER
82+
-- Shows minimum salary seen so far as we move from highest to lowest in each department
83+
SELECT
84+
emp_no,
85+
department,
86+
salary,
87+
MIN(salary) OVER(PARTITION BY department ORDER BY salary DESC) AS rolling_min
88+
FROM employees;
89+
90+
-- SHOW EMPLOYEE DETAILS ALONG WITH ROW NUMBER, RANK, AND DENSE_RANK FOR SALARIES
91+
-- Demonstrates use of window functions to rank employees within department and overall
92+
SELECT
93+
emp_no,
94+
department,
95+
salary,
96+
97+
-- Assigns a unique row number per department based on descending salary
98+
ROW_NUMBER() OVER(PARTITION BY department ORDER BY salary DESC) AS dept_row_number,
99+
100+
-- Ranks salaries within each department (duplicates get same rank, next rank is skipped)
101+
RANK() OVER(PARTITION BY department ORDER BY salary DESC) AS dept_salary_rank,
102+
103+
-- Overall ranking across all departments (standard rank)
104+
RANK() OVER(ORDER BY salary DESC) AS overall_rank,
105+
106+
-- Overall dense rank (no gaps in ranks for ties)
107+
DENSE_RANK() OVER(ORDER BY salary DESC) AS overall_dense_rank,
108+
109+
-- Assigns a unique row number to each row based on overall descending salary
110+
ROW_NUMBER() OVER(ORDER BY salary DESC) AS overall_num
111+
112+
FROM employees
113+
ORDER BY overall_rank;

0 commit comments

Comments
 (0)