Oracle – provide a rank

Oracl has a nice feature to add a rank to a row. Let us assume you have a table with 4 values: some rows have an A, others have a B, others have a C and some are D. Oracle is able to provide a rank to this series by providing a 1 to A, 2 to B, 3 to C and 4 to D.

This could be used (for example) is we have prices that we would like to order: the lowest price a 1, second a 2 etc.

The code is as follows:

 CREATE TABLE rank_demo ( col VARCHAR(10) NOT NULL); 
INSERT ALL 
INTO rank_demo(col) VALUES('A')
INTO rank_demo(col) VALUES('A')
INTO rank_demo(col) VALUES('B')
INTO rank_demo(col) VALUES('C')
INTO rank_demo(col) VALUES('C')
INTO rank_demo(col) VALUES('C')
INTO rank_demo(col) VALUES('D')
SELECT 1 FROM dual; 

SELECT  col,  dense_RANK() OVER (ORDER BY col) my_rank
FROM  rank_demo; 

Which provides:

Door tom