SQL Window Functions


1. row_number()

•Business Use Case – We want to isolate only rows for employees with the their most recent salaries

•Could be still
active employees, i.e. to_date = ‘9999-01-01’ or employees who have left the company**, what was their salary when they left?

Screen Shot 2020-04-28 at 6.37.10 PM.png

2. lead/lag

  1. lead ———take value from row below

1) wrong ex:
image.png
2) correct ex:
image.png

  1. lag ———take value from row above

image.png
image.png

3. cumulative sum

image.pngimage.png