Complete the code to calculate the running total of sales.
SELECT sales_date, sales_amount, SUM(sales_amount) OVER (ORDER BY sales_date) AS running_total FROM sales ORDER BY sales_date [1];The running total should be ordered from earliest to latest date, so we use ASC.
Complete the code to find the average salary per department without collapsing rows.
SELECT employee_id, department_id, salary, AVG(salary) OVER (PARTITION BY [1]) AS avg_dept_salary FROM employees;We partition by department_id to calculate average salary per department while keeping all rows.
Fix the error in the code to rank employees by salary within each department.
SELECT employee_id, department_id, salary, RANK() OVER (PARTITION BY department_id ORDER BY [1]) AS salary_rank FROM employees;Ranking should be based on salary, so ORDER BY salary is correct.
Fill both blanks to calculate the difference between each sale and the previous sale.
SELECT sales_date, sales_amount, sales_amount - LAG([1]) OVER (ORDER BY [2]) AS diff FROM sales;
LAG(sales_amount) gets the previous sale amount ordered by sales_date.
Fill all three blanks to assign a row number to each employee per department ordered by hire date.
SELECT employee_id, department_id, hire_date, ROW_NUMBER() OVER (PARTITION BY [1] ORDER BY [2] [3]) AS row_num FROM employees;
Partition by department_id to group employees, order by hire_date ascending to number from earliest hire.