Complete the code to calculate the total sales per department using the OVER clause with PARTITION BY.
SELECT department, sales, SUM(sales) OVER ([1] department) AS total_sales FROM sales_data;The OVER clause with PARTITION BY divides the rows into partitions to calculate the sum per department.
Complete the code to rank employees by salary within each department using the RANK() function and PARTITION BY.
SELECT employee_id, department, salary, RANK() OVER ([1] department ORDER BY salary DESC) AS salary_rank FROM employees;The RANK() function uses OVER (PARTITION BY ... ORDER BY ...) to rank salaries within each department.
Fix the error in the code to calculate the cumulative sales per region using SUM() with OVER and PARTITION BY.
SELECT region, sales, SUM(sales) OVER (PARTITION BY [1] ORDER BY sales) AS cumulative_sales FROM sales_data;The PARTITION BY clause should use the column that groups the data, here region, to calculate cumulative sales per region.
Fill both blanks to calculate the average salary per department and order the results by department.
SELECT department, AVG(salary) OVER ([1] department [2] salary) AS avg_salary FROM employees;
The PARTITION BY clause groups rows by department, and ORDER BY orders rows within each partition by salary.
Fill all three blanks to calculate the running total of sales per store, ordered by date.
SELECT store_id, sales_date, sales, SUM(sales) OVER ([1] store_id [2] sales_date [3] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total FROM sales_records;
The PARTITION BY groups rows by store, ORDER BY sorts by sales_date, and ROWS defines the frame for the running total.