The document outlines an SQL assignment focused on window functions, including creating a sample 'Employees' table with various employee data. It consists of five assignments that cover ranking, aggregate functions, LAG and LEAD functions, analytical functions, and advanced queries related to employee salaries and joining dates. Each assignment includes specific tasks such as ranking employees by salary, calculating running totals, and identifying median salaries within departments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views3 pages
8. Assignment on Window Functions
The document outlines an SQL assignment focused on window functions, including creating a sample 'Employees' table with various employee data. It consists of five assignments that cover ranking, aggregate functions, LAG and LEAD functions, analytical functions, and advanced queries related to employee salaries and joining dates. Each assignment includes specific tasks such as ranking employees by salary, calculating running totals, and identifying median salaries within departments.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
SQL Assignment on Window Functions
Creating Sample Data
Table : CREATE TABLE Employees (
EmployeeID INT PRIMARY KEY,
EmployeeName VARCHAR(50), Department VARCHAR(50), Salary DECIMAL(10,2), JoiningDate DATE );
Table Data:
INSERT INTO Employees (EmployeeID, EmployeeName, Department, Salary,
JoiningDate) VALUES (1, 'Alice', 'HR', 50000, '2020-01-15'), (2, 'Bob', 'IT', 70000, '2019-03-10'), (3, 'Charlie', 'Finance', 60000, '2021-06-20'), (4, 'David', 'IT', 75000, '2018-07-25'), (5, 'Eve', 'HR', 52000, '2022-09-30'), (6, 'Frank', 'Finance', 58000, '2017-12-05'), (7, 'Grace', 'IT', 72000, '2019-11-18'), (8, 'Hank', 'Finance', 61000, '2023-02-14'), (9, 'Ivy', 'HR', 53000, '2021-05-12'), (10, 'Jack', 'IT', 74000, '2020-08-08'), (11, 'Kevin', 'HR', 51000, '2023-03-10'), (12, 'Laura', 'Finance', 59000, '2018-05-21'), (13, 'Mike', 'IT', 76000, '2016-09-15'), (14, 'Nancy', 'HR', 54000, '2019-07-30'), (15, 'Oscar', 'Finance', 62000, '2020-11-11'), (16, 'Paul', 'IT', 73000, '2022-06-22'), (17, 'Quincy', 'HR', 55000, '2017-10-25'), (18, 'Rachel', 'Finance', 60000, '2015-04-13'), (19, 'Steve', 'IT', 71000, '2021-09-18'), (20, 'Tracy', 'HR', 56000, '2016-12-31'); Assignment 1: Ranking and Row Number Functions 1. Rank employees in each department based on salary in descending order. 2. Assign a dense rank to employees within each department based on salary. 3. Find the row number of each employee within their department, ordered by salary descending. 4. Rank employees across all departments based on salary and find the top 3 highest- paid employees.
Assignment 2: Aggregate Functions with Windows
5. Compute the running total of salaries for each department, ordered by joining date. 6. Find the cumulative percentage of total salaries for each department, ordered by salary descending. 7. Find the moving average salary of the last three employees who joined within each department. 8. Compute the cumulative salary of the last three employees who joined in each department.
Assignment 3: LAG and LEAD Functions
9. Show the previous employee’s salary for each employee within the same department, based on joining date. 10. Show the next employee’s salary for each employee within the same department, based on joining date. 11. Calculate the difference in salary from the previous row for each department. 12. Calculate the difference between each employee’s salary and the previous two salaries within their department. 13. Find the difference in salary between each employee and the one ranked immediately below them.
Assignment 4: Analytical and Statistical Functions
14. Find the average salary in each department and display it alongside each employee’s salary. 15. Identify employees whose salary is greater than the department's average using window functions. 16. Calculate the percentage difference of each employee’s salary compared to the department’s average salary. 17. Find the variance and standard deviation of salaries within each department using window functions. 18. Find the median salary for each department using window functions. Assignment 5: Special Cases and Advanced Queries 19. Identify the first and last employee who joined in each department. 20. Determine the highest salary for each employee considering only the last five employees who joined their department. 21. Find the sum of salaries for the top 5 highest-paid employees in each department. 22. Identify the median joining date for employees in each department using window functions. 23. Compute the average salary of employees who joined within the last three years in each department. 24. Find the employee who has the second-highest salary in each department. 25. Add a new row dynamically and show how the window functions adjust accordingly.