Complete the code to calculate the total sales for each department.
SELECT department, sales, SUM(sales) OVER ([1]) AS dept_total FROM sales_data;The PARTITION BY clause groups rows by department for the window function. Here, SUM(sales) OVER (PARTITION BY department) calculates the total sales per department.
Complete the code to rank employees by salary within each department.
SELECT employee_id, department, salary, RANK() OVER ([1]) AS dept_rank FROM employees;The PARTITION BY department ORDER BY salary DESC clause ranks employees within each department by descending salary.
Fix the error in the window function to calculate the average salary per department.
SELECT department, employee_id, salary, AVG(salary) OVER ([1]) AS avg_dept_salary FROM employees;For average per department, use PARTITION BY department without ORDER BY. ORDER BY is optional and not needed here.
Fill both blanks to calculate the cumulative sum of sales per region ordered by date.
SELECT region, date, sales, SUM(sales) OVER ([1] [2]) AS cumulative_sales FROM sales_records;
Use PARTITION BY region to group sales by region and ORDER BY date to calculate cumulative sum in date order.
Fill all three blanks to calculate the row number partitioned by department and ordered by hire_date.
SELECT employee_id, department, hire_date, ROW_NUMBER() OVER ([1] [2]) AS row_num FROM employees WHERE salary > [3];
Use PARTITION BY department and ORDER BY hire_date inside OVER() to number rows per department by hire date. The WHERE clause filters salaries above 50000.