Rank() - the rank function is used to grade data depending on certain criteria.


1. Used in joins ,  where, group by  and having clause.

2. Used in predceeding operaions;

3.Formations of partitioning in partitioning expression.

4. Evaluation of analytical functions

5.  always process by Order by query.

"

example:

                                 RANK()

OVER ( [ partition_by_clause ] order_by_clause )
partition_by_clause divides the result set produced by the FROM clause into partitions to which the function is applied. If not specified, the function treats all rows of the query result set as a single group. order_by_clause determines the order of the data before the function is applied. The order_by_clause is required. The <rows or range clause> of the OVER clause cannot be specified for the RANK function

 select first_name, department_id, salary, RANK( ) OVER (ORDER BY department_id) as r from employees

FIRST_NAME           DEPARTMENT_ID     SALARY          R
-------------------- ------------- ---------- ----------
Jennifer                        10    4577.76          1
Michael                         20    13525.2          2
Pat                             20     6242.4          2
Den                             30    11444.4          4
Alexander                       30    3225.24          4
Shelli                          30    3017.16          4
Sigal                           30    2913.12          4
Guy                             30    2705.04          4
Karen                           30       2601          4
Susan                           40     6762.6         10
Matthew                         50     8323.2         11

FIRST_NAME           DEPARTMENT_ID     SALARY          R
-------------------- ------------- ---------- ----------
Adam                            50    8531.28         11
Payam                           50    8219.16         11
Shanta                          50     6762.6         11
Kevin                           50    6034.32         11
Julia                           50    3329.28         11
Irene                           50    2809.08         11
James                           50    2496.96         11
Steven                          50    2288.88         11
Laura                           50    3433.32         11
Mozhe                           50    2913.12         11
James                           50       2601         11

SQL> select first_name, salary, rank() over (order by salary desc) from (selec
first_name, last_name, salary from employees);

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Steven                  24969.6                             1
Neena                   17686.8                             2
Lex                     17686.8                             2
John                    14565.6                             4
Karen                   14045.4                             5
Michael                 13525.2                             6
Nancy                  12493.12                             7
Shelley                12493.12                             7
Alberto                 12484.8                             9
Lisa                    11964.6                            10
Den                     11444.4                            11

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Gerald                  11444.4                            11
Ellen                   11444.4                            11
Eleni                   10924.2                            14
Clara                   10924.2                            14
Janette                   10404                            16
Peter                     10404                            16
Hermann                   10404                            16
Harrison                  10404                            16
Tayler                  9987.84                            20
Danielle                 9883.8                            21
David                    9883.8                            21

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Patrick                  9883.8                            21
Peter                    9363.6                            24
Alexander                9363.6                            24
Allan                    9363.6                            24
Daniel                   9363.6                            24
Alyssa                  9155.52                            28
Jonathon                8947.44                            29
Jack                    8739.36                            30
William                 8635.32                            31
Adam                    8531.28                            32
John                    8531.28                            32

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Matthew                  8323.2                            34
Lindsey                  8323.2                            34
Christopher              8323.2                            34
Payam                   8219.16                            37
Jose Manuel             8115.12                            38
Ismael                  8011.08                            39
Louise                     7803                            40
Nanette                    7803                            40
William                 7698.96                            42
Elizabeth               7594.92                            43
Mattea                  7490.88                            44

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Oliver                   7282.8                            45
Kimberely                7282.8                            45
Sarath                   7282.8                            45
Luis                    7178.76                            48
David                   7074.72                            49
Susan                    6762.6                            50
Shanta                   6762.6                            50
Sundar                  6658.56                            52
Charles                 6450.48                            53
Amit                    6450.48                            53
Sundita                 6346.44                            55

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Pat                      6242.4                            56
Bruce                    6242.4                            56
Kevin                   6034.32                            58
Valli                   4993.92                            59
David                   4993.92                            59
Jennifer                4577.76                            61
Nandita                 4369.68                            62
Diana                   4369.68                            62
Alexis                  4265.64                            64
Sarah                    4161.6                            65
Britney                 4057.56                            66

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Kelly                   3953.52                            67
Jennifer                3745.44                            68
Renske                  3745.44                            68
Trenna                   3641.4                            70
Julia                   3537.36                            71
Jason                   3433.32                            72
Laura                   3433.32                            72
Julia                   3329.28                            74
Samuel                  3329.28                            74
Winston                 3329.28                            74
Stephen                 3329.28                            74

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Alexander               3225.24                            78
Alana                   3225.24                            78
Jean                    3225.24                            78
Curtis                  3225.24                            78
Kevin                    3121.2                            82
Anthony                  3121.2                            82
Shelli                  3017.16                            84
Timothy                 3017.16                            84
Michael                 3017.16                            84
Sigal                   2913.12                            87
Vance                   2913.12                            87

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Girard                  2913.12                            87
Mozhe                   2913.12                            87
John                    2809.08                            91
Irene                   2809.08                            91
Guy                     2705.04                            93
Douglas                 2705.04                            93
Donald                  2705.04                            93
Randall                 2705.04                            93
Karen                      2601                            97
James                      2601                            97
Randall                    2601                            97

FIRST_NAME               SALARY RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------
Peter                      2601                            97
Martha                     2601                            97
Joshua                     2601                            97
Ki                      2496.96                           103
James                   2496.96                           103
Hazel                   2288.88                           105
Steven                  2288.88                           105
TJ                      2184.84                           107

107 rows selected.

SQL>

=======================

                                 DENSE_RANK()

dense rank - there are no gaps between the ranks:




SQL> select first_name, salary, dense_rank() over (order by salary desc) from (s
elect first_name, last_name, salary from employees);

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Steven                  24969.6                                   1
Neena                   17686.8                                   2
Lex                     17686.8                                   2
John                    14565.6                                   3
Karen                   14045.4                                   4
Michael                 13525.2                                   5
Nancy                  12493.12                                   6
Shelley                12493.12                                   6
Alberto                 12484.8                                   7
Lisa                    11964.6                                   8
Den                     11444.4                                   9

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Gerald                  11444.4                                   9
Ellen                   11444.4                                   9
Eleni                   10924.2                                  10
Clara                   10924.2                                  10
Janette                   10404                                  11
Peter                     10404                                  11
Hermann                   10404                                  11
Harrison                  10404                                  11
Tayler                  9987.84                                  12
Danielle                 9883.8                                  13
David                    9883.8                                  13

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Patrick                  9883.8                                  13
Peter                    9363.6                                  14
Alexander                9363.6                                  14
Allan                    9363.6                                  14
Daniel                   9363.6                                  14
Alyssa                  9155.52                                  15
Jonathon                8947.44                                  16
Jack                    8739.36                                  17
William                 8635.32                                  18
Adam                    8531.28                                  19
John                    8531.28                                  19

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Matthew                  8323.2                                  20
Lindsey                  8323.2                                  20
Christopher              8323.2                                  20
Payam                   8219.16                                  21
Jose Manuel             8115.12                                  22
Ismael                  8011.08                                  23
Louise                     7803                                  24
Nanette                    7803                                  24
William                 7698.96                                  25
Elizabeth               7594.92                                  26
Mattea                  7490.88                                  27

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Oliver                   7282.8                                  28
Kimberely                7282.8                                  28
Sarath                   7282.8                                  28
Luis                    7178.76                                  29
David                   7074.72                                  30
Susan                    6762.6                                  31
Shanta                   6762.6                                  31
Sundar                  6658.56                                  32
Charles                 6450.48                                  33
Amit                    6450.48                                  33
Sundita                 6346.44                                  34

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Pat                      6242.4                                  35
Bruce                    6242.4                                  35
Kevin                   6034.32                                  36
Valli                   4993.92                                  37
David                   4993.92                                  37
Jennifer                4577.76                                  38
Nandita                 4369.68                                  39
Diana                   4369.68                                  39
Alexis                  4265.64                                  40
Sarah                    4161.6                                  41
Britney                 4057.56                                  42

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Kelly                   3953.52                                  43
Jennifer                3745.44                                  44
Renske                  3745.44                                  44
Trenna                   3641.4                                  45
Julia                   3537.36                                  46
Jason                   3433.32                                  47
Laura                   3433.32                                  47
Julia                   3329.28                                  48
Samuel                  3329.28                                  48
Winston                 3329.28                                  48
Stephen                 3329.28                                  48

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Alexander               3225.24                                  49
Alana                   3225.24                                  49
Jean                    3225.24                                  49
Curtis                  3225.24                                  49
Kevin                    3121.2                                  50
Anthony                  3121.2                                  50
Shelli                  3017.16                                  51
Timothy                 3017.16                                  51
Michael                 3017.16                                  51
Sigal                   2913.12                                  52
Vance                   2913.12                                  52

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Girard                  2913.12                                  52
Mozhe                   2913.12                                  52
John                    2809.08                                  53
Irene                   2809.08                                  53
Guy                     2705.04                                  54
Douglas                 2705.04                                  54
Donald                  2705.04                                  54
Randall                 2705.04                                  54
Karen                      2601                                  55
James                      2601                                  55
Randall                    2601                                  55

FIRST_NAME               SALARY DENSE_RANK()OVER(ORDERBYSALARYDESC)
-------------------- ---------- -----------------------------------
Peter                      2601                                  55
Martha                     2601                                  55
Joshua                     2601                                  55
Ki                      2496.96                                  56
James                   2496.96                                  56
Hazel                   2288.88                                  57
Steven                  2288.88                                  57
TJ                      2184.84                                  58

107 rows selected.

SQL>


==================================

                                Top n queries:

below queries will give you the top salary from rows.


 SQL> select * from (select first_name, salary, dense_rank() over (order by salar
y desc) as top_n from (select first_name, last_name, salary from employees)) whe
re top_n <4;

FIRST_NAME               SALARY      TOP_N
-------------------- ---------- ----------
Steven                  24969.6          1
Neena                   17686.8          2
Lex                     17686.8          2
John                    14565.6          3

SQL>


=======================

2nd example : top n queries:


SQL> select * from (select first_name, salary, rank() over (order by salary desc
) as top_n from (select first_name, last_name, salary from employees)) where top
_n < 10;

FIRST_NAME               SALARY      TOP_N
-------------------- ---------- ----------
Steven                  24969.6          1
Neena                   17686.8          2
Lex                     17686.8          2
John                    14565.6          4
Karen                   14045.4          5
Michael                 13525.2          6
Nancy                  12493.12          7
Shelley                12493.12          7
Alberto                 12484.8          9

9 rows selected.

SQL>
========================

                                 Cume_dist()

it will use for calculate the fraction of number of rows. 


SQL> select * from (select salary, first_name,cume_dist() over(order by salary)a
s rank from employees) where rank<20;

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   2184.84 TJ                   .009345794
   2288.88 Hazel                .028037383
   2288.88 Steven               .028037383
   2496.96 Ki                   .046728972
   2496.96 James                .046728972
      2601 Karen                .102803738
      2601 James                .102803738
      2601 Randall              .102803738
      2601 Peter                .102803738
      2601 Martha               .102803738
      2601 Joshua               .102803738

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   2705.04 Guy                  .140186916
   2705.04 Douglas              .140186916
   2705.04 Donald               .140186916
   2705.04 Randall              .140186916
   2809.08 John                 .158878505
   2809.08 Irene                .158878505
   2913.12 Sigal                .196261682
   2913.12 Vance                .196261682
   2913.12 Girard               .196261682
   2913.12 Mozhe                .196261682
   3017.16 Shelli               .224299065

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   3017.16 Timothy              .224299065
   3017.16 Michael              .224299065
    3121.2 Kevin                .242990654
    3121.2 Anthony              .242990654
   3225.24 Alexander            .280373832
   3225.24 Alana                .280373832
   3225.24 Jean                 .280373832
   3225.24 Curtis               .280373832
   3329.28 Julia                .317757009
   3329.28 Samuel               .317757009
   3329.28 Winston              .317757009

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   3329.28 Stephen              .317757009
   3433.32 Jason                .336448598
   3433.32 Laura                .336448598
   3537.36 Julia                .345794393
    3641.4 Trenna               .355140187
   3745.44 Jennifer             .373831776
   3745.44 Renske               .373831776
   3953.52 Kelly                 .38317757
   4057.56 Britney              .392523364
    4161.6 Sarah                .401869159
   4265.64 Alexis               .411214953

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   4369.68 Nandita              .429906542
   4369.68 Diana                .429906542
   4577.76 Jennifer             .439252336
   4993.92 Valli                .457943925
   4993.92 David                .457943925
   6034.32 Kevin                 .46728972
    6242.4 Pat                  .485981308
    6242.4 Bruce                .485981308
   6346.44 Sundita              .495327103
   6450.48 Charles              .514018692
   6450.48 Amit                 .514018692

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   6658.56 Sundar               .523364486
    6762.6 Susan                .542056075
    6762.6 Shanta               .542056075
   7074.72 David                .551401869
   7178.76 Luis                 .560747664
    7282.8 Oliver               .588785047
    7282.8 Kimberely            .588785047
    7282.8 Sarath               .588785047
   7490.88 Mattea               .598130841
   7594.92 Elizabeth            .607476636
   7698.96 William               .61682243

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
      7803 Louise               .635514019
      7803 Nanette              .635514019
   8011.08 Ismael               .644859813
   8115.12 Jose Manuel          .654205607
   8219.16 Payam                .663551402
    8323.2 Matthew              .691588785
    8323.2 Lindsey              .691588785
    8323.2 Christopher          .691588785
   8531.28 Adam                 .710280374
   8531.28 John                 .710280374
   8635.32 William              .719626168

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   8739.36 Jack                 .728971963
   8947.44 Jonathon             .738317757
   9155.52 Alyssa               .747663551
    9363.6 Alexander            .785046729
    9363.6 Daniel               .785046729
    9363.6 Peter                .785046729
    9363.6 Allan                .785046729
    9883.8 Patrick              .813084112
    9883.8 Danielle             .813084112
    9883.8 David                .813084112
   9987.84 Tayler               .822429907

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
     10404 Hermann              .859813084
     10404 Harrison             .859813084
     10404 Janette              .859813084
     10404 Peter                .859813084
   10924.2 Clara                .878504673
   10924.2 Eleni                .878504673
   11444.4 Gerald               .906542056
   11444.4 Den                  .906542056
   11444.4 Ellen                .906542056
   11964.6 Lisa                  .91588785
   12484.8 Alberto              .925233645

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
  12493.12 Shelley              .943925234
  12493.12 Nancy                .943925234
   13525.2 Michael              .953271028
   14045.4 Karen                .962616822
   14565.6 John                 .971962617
   17686.8 Lex                  .990654206
   17686.8 Neena                .990654206
   24969.6 Steven                        1

107 rows selected.

                                          percent_rank()

calculate the percetlie value of row.


SQL> select * from (select salary, first_name,percent_rank() over(order by salar
y)as rank from employees) where rank<20;

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   2184.84 TJ                            0
   2288.88 Hazel                .009433962
   2288.88 Steven               .009433962
   2496.96 Ki                   .028301887
   2496.96 James                .028301887
      2601 Karen                .047169811
      2601 James                .047169811
      2601 Randall              .047169811
      2601 Peter                .047169811
      2601 Martha               .047169811
      2601 Joshua               .047169811

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   2705.04 Guy                  .103773585
   2705.04 Douglas              .103773585
   2705.04 Donald               .103773585
   2705.04 Randall              .103773585
   2809.08 John                 .141509434
   2809.08 Irene                .141509434
   2913.12 Sigal                .160377358
   2913.12 Vance                .160377358
   2913.12 Girard               .160377358
   2913.12 Mozhe                .160377358
   3017.16 Shelli               .198113208

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   3017.16 Timothy              .198113208
   3017.16 Michael              .198113208
    3121.2 Kevin                .226415094
    3121.2 Anthony              .226415094
   3225.24 Alexander            .245283019
   3225.24 Alana                .245283019
   3225.24 Jean                 .245283019
   3225.24 Curtis               .245283019
   3329.28 Julia                .283018868
   3329.28 Samuel               .283018868
   3329.28 Winston              .283018868

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   3329.28 Stephen              .283018868
   3433.32 Jason                .320754717
   3433.32 Laura                .320754717
   3537.36 Julia                .339622642
    3641.4 Trenna               .349056604
   3745.44 Jennifer             .358490566
   3745.44 Renske               .358490566
   3953.52 Kelly                .377358491
   4057.56 Britney              .386792453
    4161.6 Sarah                .396226415
   4265.64 Alexis               .405660377

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   4369.68 Nandita               .41509434
   4369.68 Diana                 .41509434
   4577.76 Jennifer             .433962264
   4993.92 Valli                .443396226
   4993.92 David                .443396226
   6034.32 Kevin                .462264151
    6242.4 Pat                  .471698113
    6242.4 Bruce                .471698113
   6346.44 Sundita              .490566038
   6450.48 Charles                      .5
   6450.48 Amit                         .5

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   6658.56 Sundar               .518867925
    6762.6 Susan                .528301887
    6762.6 Shanta               .528301887
   7074.72 David                .547169811
   7178.76 Luis                 .556603774
    7282.8 Oliver               .566037736
    7282.8 Kimberely            .566037736
    7282.8 Sarath               .566037736
   7490.88 Mattea               .594339623
   7594.92 Elizabeth            .603773585
   7698.96 William              .613207547

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
      7803 Louise               .622641509
      7803 Nanette              .622641509
   8011.08 Ismael               .641509434
   8115.12 Jose Manuel          .650943396
   8219.16 Payam                .660377358
    8323.2 Matthew              .669811321
    8323.2 Lindsey              .669811321
    8323.2 Christopher          .669811321
   8531.28 Adam                 .698113208
   8531.28 John                 .698113208
   8635.32 William              .716981132

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   8739.36 Jack                 .726415094
   8947.44 Jonathon             .735849057
   9155.52 Alyssa               .745283019
    9363.6 Alexander            .754716981
    9363.6 Daniel               .754716981
    9363.6 Peter                .754716981
    9363.6 Allan                .754716981
    9883.8 Patrick               .79245283
    9883.8 Danielle              .79245283
    9883.8 David                 .79245283
   9987.84 Tayler               .820754717

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
     10404 Hermann              .830188679
     10404 Harrison             .830188679
     10404 Janette              .830188679
     10404 Peter                .830188679
   10924.2 Clara                .867924528
   10924.2 Eleni                .867924528
   11444.4 Gerald               .886792453
   11444.4 Den                  .886792453
   11444.4 Ellen                .886792453
   11964.6 Lisa                  .91509434
   12484.8 Alberto              .924528302

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
  12493.12 Shelley              .933962264
  12493.12 Nancy                .933962264
   13525.2 Michael              .952830189
   14045.4 Karen                .962264151
   14565.6 John                 .971698113
   17686.8 Lex                  .981132075
   17686.8 Neena                .981132075
   24969.6 Steven                        1

107 rows selected.

SQL>
=====================================================================
SQL> select salary, first_name,percent_rank() over(order by salary)as rank from
employees;

    SALARY FIRST_NAME                 RANK
---------- -------------------- ----------
   2184.84 TJ                            0
   2288.88 Hazel                .009433962
   2288.88 Steven               .009433962
   2496.96 Ki                   .028301887
   2496.96 James                .028301887
      2601 Karen                .047169811
      2601 James                .047169811
      2601 Randall              .047169811
      2601 Peter                .047169811
      2601 Martha               .047169811
      2601 Joshua               .047169811
=================================================

NTILE (N), 

this will be use for calculate tertiles, quartiles and other common statistics. the function will divide into N buckets and distributes the rows in the buckets according to rank.

SQL> select salary, first_name, NTILE (4) over (order by salary) as NTILE,per
t_rank() over(order by salary)as rank from employees;

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   2184.84 TJ                            1                             0
   2288.88 Hazel                         1                         .009433962
   2288.88 Steven                        1                         .009433962
   2496.96 Ki                            1                             .028301887
   2496.96 James                         1 .028301887
      2601 Karen                         1 .047169811
      2601 James                         1 .047169811
      2601 Randall                       1 .047169811
      2601 Peter                         1 .047169811
      2601 Martha                        1 .047169811
      2601 Joshua                        1 .047169811

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   2705.04 Guy                           1 .103773585
   2705.04 Douglas                       1 .103773585
   2705.04 Donald                        1 .103773585
   2705.04 Randall                       1 .103773585
   2809.08 John                          1 .141509434
   2809.08 Irene                         1 .141509434
   2913.12 Sigal                         1 .160377358
   2913.12 Vance                         1 .160377358
   2913.12 Girard                        1 .160377358
   2913.12 Mozhe                         1 .160377358
   3017.16 Shelli                        1 .198113208

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   3017.16 Timothy                       1 .198113208
   3017.16 Michael                       1 .198113208
    3121.2 Kevin                         1 .226415094
    3121.2 Anthony                       1 .226415094
   3225.24 Alexander                     1 .245283019
   3225.24 Alana                         2 .245283019
   3225.24 Jean                          2 .245283019
   3225.24 Curtis                        2 .245283019
   3329.28 Julia                         2 .283018868
   3329.28 Samuel                        2 .283018868
   3329.28 Winston                       2 .283018868

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   3329.28 Stephen                       2 .283018868
   3433.32 Jason                         2 .320754717
   3433.32 Laura                         2 .320754717
   3537.36 Julia                         2 .339622642
    3641.4 Trenna                        2 .349056604
   3745.44 Jennifer                      2 .358490566
   3745.44 Renske                        2 .358490566
   3953.52 Kelly                         2 .377358491
   4057.56 Britney                       2 .386792453
    4161.6 Sarah                         2 .396226415
   4265.64 Alexis                        2 .405660377

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   4369.68 Nandita                       2  .41509434
   4369.68 Diana                         2  .41509434
   4577.76 Jennifer                      2 .433962264
   4993.92 Valli                         2 .443396226
   4993.92 David                         2 .443396226
   6034.32 Kevin                         2 .462264151
    6242.4 Pat                           2 .471698113
    6242.4 Bruce                         2 .471698113
   6346.44 Sundita                       2 .490566038
   6450.48 Charles                       2         .5
   6450.48 Amit                          3         .5

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   6658.56 Sundar                        3 .518867925
    6762.6 Susan                         3 .528301887
    6762.6 Shanta                        3 .528301887
   7074.72 David                         3 .547169811
   7178.76 Luis                          3 .556603774
    7282.8 Oliver                        3 .566037736
    7282.8 Kimberely                     3 .566037736
    7282.8 Sarath                        3 .566037736
   7490.88 Mattea                        3 .594339623
   7594.92 Elizabeth                     3 .603773585
   7698.96 William                       3 .613207547

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
      7803 Louise                        3                         .622641509
      7803 Nanette                       3                        .622641509
   8011.08 Ismael                        3 .641509434
   8115.12 Jose Manuel                   3 .650943396
   8219.16 Payam                         3 .660377358
    8323.2 Matthew                       3 .669811321
    8323.2 Lindsey                       3 .669811321
    8323.2 Christopher                   3 .669811321
   8531.28 Adam                          3 .698113208
   8531.28 John                          3 .698113208
   8635.32 William                       3 .716981132

    SALARY FIRST_NAME                NTILE       RANK
---------- -------------------- ---------- ----------
   8739.36 Jack                          3 .726415094
   8947.44 Jonathon                      3 .735849057
   9155.52 Alyssa                        3 .745283019
    9363.6 Alexander                     3 .754716981
    9363.6 Daniel                        4 .754716981
    9363.6 Peter                         4 .754716981
    9363.6 Allan                         4 .754716981
    9883.8 Patrick                       4  .79245283
    9883.8 Danielle                      4  .79245283
    9883.8 David                         4  .79245283
   9987.84 Tayler                        4 .820754717


ROW_number()

 (the row_number function assign a unique rank to  each row ,even  if it has the same value of order  by expression)


SQL> select salary, first_name, NTILE (4) over (order by salary) as ntl,row_numb
er() over (order by salary) as row_number from employees;

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   2184.84 TJ                            1          1
   2288.88 Hazel                         1          2
   2288.88 Steven                        1          3
   2496.96 Ki                            1          4
   2496.96 James                         1          5
      2601 Karen                         1          6
      2601 James                         1          7
      2601 Randall                       1          8
      2601 Peter                         1          9
      2601 Martha                        1         10
      2601 Joshua                        1         11

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   2705.04 Guy                           1         12
   2705.04 Douglas                       1         13
   2705.04 Donald                        1         14
   2705.04 Randall                       1         15
   2809.08 John                          1         16
   2809.08 Irene                         1         17
   2913.12 Sigal                         1         18
   2913.12 Vance                         1         19
   2913.12 Girard                        1         20
   2913.12 Mozhe                         1         21
   3017.16 Shelli                        1         22

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   3017.16 Timothy                       1         23
   3017.16 Michael                       1         24
    3121.2 Kevin                         1         25
    3121.2 Anthony                       1         26
   3225.24 Alexander                     1         27
   3225.24 Alana                         2         28
   3225.24 Jean                          2         29
   3225.24 Curtis                        2         30
   3329.28 Julia                         2         31
   3329.28 Samuel                        2         32
   3329.28 Winston                       2         33

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   3329.28 Stephen                       2         34
   3433.32 Jason                         2         35
   3433.32 Laura                         2         36
   3537.36 Julia                         2         37
    3641.4 Trenna                        2         38
   3745.44 Jennifer                      2         39
   3745.44 Renske                        2         40
   3953.52 Kelly                         2         41
   4057.56 Britney                       2         42
    4161.6 Sarah                         2         43
   4265.64 Alexis                        2         44

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   4369.68 Nandita                       2         45
   4369.68 Diana                         2         46
   4577.76 Jennifer                      2         47
   4993.92 Valli                         2         48
   4993.92 David                         2         49
   6034.32 Kevin                         2         50
    6242.4 Pat                           2         51
    6242.4 Bruce                         2         52
   6346.44 Sundita                       2         53
   6450.48 Charles                       2         54
   6450.48 Amit                          3         55

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   6658.56 Sundar                        3         56
    6762.6 Susan                         3         57
    6762.6 Shanta                        3         58
   7074.72 David                         3         59
   7178.76 Luis                          3         60
    7282.8 Oliver                        3         61
    7282.8 Kimberely                     3         62
    7282.8 Sarath                        3         63
   7490.88 Mattea                        3         64
   7594.92 Elizabeth                     3         65
   7698.96 William                       3         66

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
      7803 Louise                        3         67
      7803 Nanette                       3         68
   8011.08 Ismael                        3         69
   8115.12 Jose Manuel                   3         70
   8219.16 Payam                         3         71
    8323.2 Matthew                       3         72
    8323.2 Lindsey                       3         73
    8323.2 Christopher                   3         74
   8531.28 Adam                          3         75
   8531.28 John                          3         76
   8635.32 William                       3         77

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
   8739.36 Jack                          3         78
   8947.44 Jonathon                      3         79
   9155.52 Alyssa                        3         80
    9363.6 Alexander                     3         81
    9363.6 Daniel                        4         82
    9363.6 Peter                         4         83
    9363.6 Allan                         4         84
    9883.8 Patrick                       4         85
    9883.8 Danielle                      4         86
    9883.8 David                         4         87
   9987.84 Tayler                        4         88

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
     10404 Hermann                       4         89
     10404 Harrison                      4         90
     10404 Janette                       4         91
     10404 Peter                         4         92
   10924.2 Clara                         4         93
   10924.2 Eleni                         4         94
   11444.4 Gerald                        4         95
   11444.4 Den                           4         96
   11444.4 Ellen                         4         97
   11964.6 Lisa                          4         98
   12484.8 Alberto                       4         99

    SALARY FIRST_NAME                  NTL ROW_NUMBER
---------- -------------------- ---------- ----------
  12493.12 Shelley                       4        100
  12493.12 Nancy                         4        101
   13525.2 Michael                       4        102
   14045.4 Karen                         4        103
   14565.6 John                          4        104
   17686.8 Lex                           4        105
   17686.8 Neena                         4        106
   24969.6 Steven                        4        107

107 rows selected.

SQL>