SQLite ROW_NUMBER() Function
Example
Number the products from the most expensive:
SELECT ROW_NUMBER() OVER (ORDER BY Price DESC)
AS Place, ProductName, Price
FROM Products;
Try it Yourself »
Definition and Usage
The ROW_NUMBER() function gives each row a number, starting with 1, in the order given by the ORDER BY in the window.
Note: Window functions need an OVER clause. Read more in the SQLite Window Functions chapter.
Tip: Also look at RANK(), DENSE_RANK().
Syntax
ROW_NUMBER() OVER (PARTITION BY column ORDER BY column)
Technical Details
| Return type: | Integer |
|---|---|
| Works in: | SQLite 3.25.0 and later |
More Examples
Example
Number the products in each category:
SELECT CategoryID, ProductName,
ROW_NUMBER() OVER (
PARTITION BY CategoryID
ORDER BY Price DESC) AS Place
FROM Products;
Try it Yourself »