ROW_NUMBER function
Lesson 3 of 13 in Coddy's SQL for advanced course.
Window functions are used to calculate stuff about the current row.
For example What if we want to calculate all of the drinks that were bought until 1995, 1997, 1999, etc.? What if we want to find the best Olympic performer for each period (2000, 2004, 2008)?
Maybe the best performer for 2000 and 2004 is James, and for 2008 is John because he is better than James.
All of this can be solved using window functions.
The ROW_NUMBER() OVER () is numbering the rows with a specific criterion. The criterion should be inside the OVER (). If no criterion is specified then it will number the rows with the same order.
For example:
SELECT *, ROW_NUMBER() OVER () as row_num
FROM table1This will add a column row_num that will go from 1 to n (n - number of rows)
Challenge
EasyFetch all liquids with more than 5.677 density.
Number the result and call this column row_number
Try it yourself
All lessons in SQL for advanced
4Summary
Final challenge #12Window Functions part 1
ROW_NUMBER functionORDER BY criterionPARTITION BY criterionLEAD & LAG functionsRecap challenge #1Recap challenge #23Window Functions part 2
RANK & DENSE_RANK functionsNTILE functionAggregation functionsROWS & RANGE criterion