Get to know the data
Get to know the OVER() clause
Computations with OVER()
8. Computations with OVER() – part 2
Aggregate functions with OVER()
Using OVER() with WHERE
Summary

Instruction

That's right! Now, take a look at another interesting example:

SELECT
  Id,
  Item,
  Price,
  CAST(Price AS NUMERIC) / SUM(Price) OVER() AS Ratio
FROM Purchase
WHERE DepartmentId = 2;

In the above query, we show all the purchases made by the department where Id = 2.

Note that we divide the item price by the total price of all items purchased by that department. In this way, we can see what part of the total expenditure each purchase constitutes.

Exercise

For all employees where DepartmentId = 3, show their:

  • FirstName,
  • LastName,
  • Salary,
  • the difference between their salary and the departmental salary average. Name this column Difference.