The RANK() OVER() function in SQL is used to assign ranking values to data records. It accepts an ORDER BY clause specifying the columns to rank by and the sort order. Parameters include: column name (column to be ranked), sort order (ascending or descending), and how NULL values are handled (first, last, or only non-NULL values). This function is used to assign the same rank or unique rank to records with the same value, and can exclude or handle NULL values.
RANK() OVER() usage in SQL
RANK() OVER() function is used in SQL Used to rank data and assign a ranking value to each record. This function accepts an ORDER BY clause specifying the columns to rank by and the sort order.
Syntax:
<code>RANK() OVER (ORDER BY 列名 [ASC|DESC] [NULLS FIRST|LAST|ONLY])</code>
Parameters:
Usage:
RANK() OVER() function is used to rank various types of data, including:
Example:
The following example ranks the records in the "Students" table based on the "Score" column, from high to low:
<code>SELECT *, RANK() OVER (ORDER BY 分数 DESC) AS 排名 FROM 学生;</code>
The results are as follows:
Student Number | Name | Score | Ranking |
---|---|---|---|
小明 | 95 | 1 | |
小华 | 90 | 2 | |
小丽 | 85 | 3 | |
Xiaogang | 80 | 4 |
RANK() The ranking value returned by OVER() starts from 1, not 0.
The above is the detailed content of How to use rank(over() in sql. For more information, please follow other related articles on the PHP Chinese website!