In SQL, ranking functions are used to assign a rank to each row within the result set based on specific criteria. These functions can be helpful for tasks like finding the top N rows, identifying the highest or lowest values, and more. There are several ranking functions in SQL, including:
RANK(): This function assigns a unique rank to each row within the result set. If there are ties (rows with the same values), they will receive the same rank, and the next rank will be skipped.
DENSE_RANK(): DENSE_RANK() is similar to RANK(), but it does not skip rank values in the event of ties. If multiple rows have the same values, they will receive the same rank, and the next rank will be consecutive.
NTILE(n): NTILE divides the result set into "n" roughly equal parts and assigns a rank or group number to each row based on which part it falls into. For example, if you use NTILE(4), it will divide the data into four groups and assign a rank from 1 to 4 to each row based on its group.
ROW_NUMBER(): ROW_NUMBER() assigns a unique number to each row in the result set, similar to RANK(). However, it does not handle ties. Rows with the same values will receive different row numbers.
Discover more about SQL ranking function in the video!
0 Comments