Complete the code to calculate the running total of sales ordered by date.
SELECT date, sales, SUM(sales) OVER (ORDER BY [1]) AS running_total FROM sales_data;The running total needs to be ordered by the date column to accumulate sales in chronological order.
Complete the code to assign a rank to each employee based on their salary in descending order.
SELECT employee_id, salary, RANK() OVER (ORDER BY salary [1]) AS salary_rank FROM employees;Ranking employees by salary from highest to lowest requires DESC order.
Fix the error in the code to calculate the difference between each sale and the previous sale.
SELECT sale_id, sale_amount, sale_amount - LAG([1]) OVER (ORDER BY sale_date) AS diff FROM sales;The LAG function should reference the sale_amount column to compare current and previous sales.
Fill both blanks to calculate the cumulative average sales per region ordered by date.
SELECT region, date, sales, AVG([1]) OVER (PARTITION BY [2] ORDER BY date) AS cum_avg FROM sales_data;
The cumulative average is calculated on sales partitioned by region.
Fill all three blanks to assign a row number per department ordered by hire date, and show the first hired employee per department.
SELECT department, employee_name, hire_date, rn FROM (SELECT department, employee_name, hire_date, ROW_NUMBER() OVER (PARTITION BY [1] ORDER BY [2] [3]) AS rn FROM employees) AS t WHERE rn = 1;
Row numbers are assigned per department, ordered by hire_date ascending to get the earliest hire.