Allows you to split the result set into smaller groups and apply the window function independently to each group
Whether or not you need this clause depends on the requirement of the query
If you want to apply a uniform window calculation across the entire result set, you don’t need it
If you need to perform calculations group-wise, then you must use it
ORDER BY
Determines the order of data within the window
Required for rank function
SELECTid,salesperson_id,amount,SUM(amount)OVER(PARTITIONBYsalesperson_id)AStotal_amountFROMsales;-- if you want to compute the average salary for each department
SELECTdepartment,AVG(salary)OVER(PARTITIONBYdepartment)FROMemployees;-- if you want to compute the overall average salary for all employees
SELECTdepartment,AVG(salary)OVER()FROMemployees;-- if you want to rank employees within each department by salary
SELECTdepartment,salary,ROW_NUMBER()OVER(PARTITIONBYdepartmentORDERBYsalaryDESC)FROMemployees;-- if you want to see the difference in salary between each employee and the prev when ranked by salary across the entire company
-- `LAG` for getting previous row, `LEAD` for getting next row
SELECTemployee_name,salary,LAG(salary)OVER(ORDERBYsalaryDESC)FROMemployees;-- combine `GROUP BY` with window funcction
-- in this specific scenario, `PARTITION BY` is not necessary
-- the reason is that the `GROUP BY` clause already ensures that the sum of SaleAmount is calculated per employee
SELECTEmployeeID,SUM(SaleAmount)ASTotalSales,RANK()OVER(ORDERBYSUM(SaleAmount)DESC)ASSalesRankFROMEmployeeSalesGROUPBYEmployeeID;