The popularity of window functions is only rivaled by their usefulness. This is especially true for the subset of window functions commonly referred to as ranking functions. I’ll first introduce the why behind ranking functions and then dive into the how and when. The how walks through calculating rank using SQL to meet a common reporting requirement while the when explores scenarios for each specific ranking function. At the end of this page, I’ll briefly comment on why it is important to provide proper context to your audience around ranking outcomes. The schemas, corresponding tables, and data used to support the examples on this page can be found at livesql.oracle.com.
Why Ranking Functions?
A number by itself is arguably meaningless, so we give life to numbers by providing context. We have a propensity for using numbers to describe things in terms of relative size, importance, and position. To illustrate, consider the following statements regarding the data displayed in the table below: sales representative 161 is the number one sales representative, the top three performers, located in rows two through four, are sales representatives 161, 156, and 154, and the bottom three performers, located in rows seven through nine, are 163, 153, and 160.
| sales_rep_id | total_sales |
|---|---|
| 161 | 661734.5 |
| 156 | 202617.6 |
| 154 | 171973.1 |
| 158 | 156296.2 |
| 159 | 151167.2 |
| 155 | 134415.2 |
| 163 | 128249.5 |
| 153 | 114215.7 |
| 160 | 88238.4 |
Associating each sales representative’s performance with an integer value is easy, natural, and at the same time, powerful. By doing so, we can quickly convey how great or poor each sales representative’s performance is relative to other sales representatives’ performances. This is the concept of rank, and it is usually done based on some specification or set of instructions. For clarity, see the augmented table below where the rank value associated with each sales representative is based on the corresponding total sales value. The largest total sales value has a corresponding rank of 1, second largest total sales value a rank value of 2, and so on.
| sales_rep_id | total_sales | performance_rank |
|---|---|---|
| 161 | 661734.5 | 1 |
| 156 | 202617.6 | 2 |
| 154 | 171973.1 | 3 |
| 158 | 156296.2 | 4 |
| 159 | 151167.2 | 5 |
| 155 | 134415.2 | 6 |
| 163 | 128249.5 | 7 |
| 153 | 114215.7 | 8 |
| 160 | 88238.4 | 9 |
The integer value we associate with each performance can also be used to convey position or location. For example, we can refer to an observation simply by the row number. Stating, “the performance of sales representative 163 can be found in row 8” allows the consumer of the data to quickly navigate their eyes to this location within the data.
Calculating Rank Using SQL
Modern relational database management systems (RDBMS) allow us to incorporate the ideas of rank and location using window or analytic functions. The specific window functions used to do so are RANK, DENSE_RANK, and ROW_NUMBER. These window functions are shared across most flavors of SQL and are somewhat straightforward to use after a brief introduction on the syntax and clauses. If you’re unfamiliar with the syntax and individual clauses for window functions, then consider reading my brief explanation here before continuing further.
The ranking functions have significant overlap in terms of the SQL code using to implement them; however, the outcome when used against the same data can differ significantly. Let’s walk through a concrete example to better understand the similarity and differences across ranking functions.
Suppose we are given the task of generating a report containing the product ID, product name, category ID, and list price for each product. In addition to these attributes, we are also required to rank each of the products, in descending order, based on their list prices. This is where this seemingly simple task can result in generating an erroneous report. In the absence of details concerning the treatment of ties (i.e., multiple products with the same list price), any of the three ranking functions could be used. Let’s dive into this further.
In the result set below, notice the integer values returned by each function, ROW_NUMBER, RANK, and DENSE_RANK are identical up until the 13th product listed. The difference observed in rows 12 and 13 is attributable to the products having the same list price value of 999. The ROW_NUMBER function assigns consecutive integer values without addressing ties while RANK and DENSE_RANK functions do address ties. RANK assigns the same integer value to the rows with the same list price value and determines the next row’s integer value (i.e., rank) by adding the number of tied rows to the previous or tied rank value. In this case, there are only two rows with the same list price value associated with rank 12, so the rank value assigned to the row following the tie is 14 (i.e., 12 + 2). DENSE_RANK also addresses ties by assigning the tied rows the same rank value; however, the function doesn’t incorporate the same gaps in rank values. Instead, DENSE_RANK assigns the row immediately following a tie the subsequent rank value. Notice, the sequence is 12, 12, 13 where the RANK function assigned the integer values, 12, 12, 14 for the same rows.
| product_id | product_name | category_id | list_price | row_number | rank | dense_rank |
|---|---|---|---|---|---|---|
| 2779 | Desk – OS/O/F | 31 | 3980 | 1 | 1 | 1 |
| 3003 | Laptop 128/12/56/v90/110 | 19 | 3219 | 2 | 2 | 2 |
| 2351 | Desk – W/48/R | 31 | 2900 | 3 | 3 | 3 |
| 3004 | Laptop 64/10/56/220 | 19 | 2768 | 4 | 4 | 4 |
| 3001 | Laptop 48/10/56/110 | 19 | 2556 | 5 | 5 | 5 |
| 2231 | Desk – S/V | 31 | 2510 | 6 | 6 | 6 |
| 2350 | Desk – W/48 | 31 | 2500 | 7 | 7 | 7 |
| 3000 | Laptop 32/10/56 | 19 | 1749 | 8 | 8 | 8 |
| 1822 | SPNIX4.0 – SL | 24 | 1500 | 9 | 9 | 9 |
| 3064 | Monitor 21/SD | 11 | 1023 | 10 | 10 | 10 |
| 1799 | SPNIX3.3 – SL | 24 | 1000 | 11 | 11 | 11 |
| 3065 | Monitor 21/D | 11 | 999 | 12 | 12 | 12 |
| 2999 | Laptop 16/8/110 | 19 | 999 | 13 | 12 | 12 |
| 2236 | Plasma Monitor 10/TFT/XGA | 11 | 964 | 14 | 14 | 13 |
| 2252 | Monitor 21/HR/M | 11 | 889 | 15 | 15 | 14 |
| 3331 | Monitor 21/HR | 11 | 879 | 16 | 16 | 15 |
| 2382 | HD 18.2GB@10000 /I | 13 | 850 | 17 | 17 | 16 |
| 3399 | HD 18GB /SE | 13 | 815 | 18 | 18 | 17 |
| 1743 | HD 18.2GB @10000 /E | 13 | 800 | 19 | 19 | 18 |
| 3337 | Mobile Web Phone | 31 | 800 | 20 | 19 | 18 |
| 2255 | HD 12GB @7200 /SE | 13 | 775 | 21 | 21 | 19 |
| 3350 | Plasma Monitor 10/LE/VGA | 11 | 740 | 22 | 22 | 20 |
| 2459 | LaserPro 1200/8/BW | 12 | 699 | 23 | 23 | 21 |
| 1750 | DIMM – 2GB | 14 | 699 | 24 | 23 | 21 |
| 3071 | HD 12GB /S | 13 | 633 | 25 | 25 | 22 |
| 3334 | HD 12GB /R | 13 | 612 | 26 | 26 | 23 |
| 3054 | Plasma Monitor 10/XGA | 11 | 600 | 27 | 27 | 24 |
| 2384 | DIMM – 1GB | 14 | 599 | 28 | 28 | 25 |
| 3072 | HD 12GB /N | 13 | 567 | 29 | 29 | 26 |
| 3501 | C for SPNIX4.0 – Sys | 25 | 555 | 30 | 30 | 27 |
| 3354 | HD 12GB /I | 13 | 543 | 31 | 31 | 28 |
| 2245 | Monitor 19/SD/M | 11 | 512 | 32 | 32 | 29 |
| 2471 | SPNIX3.3 SU | 24 | 500 | 33 | 33 | 30 |
| 3061 | Monitor 19/SD | 11 | 499 | 34 | 34 | 31 |
| 3127 | LaserPro 600/6/BW | 12 | 498 | 35 | 35 | 32 |
| 1948 | Envoy IC/58 | 19 | 498 | 36 | 35 | 32 |
| 3353 | HD 10GB /R | 13 | 489 | 37 | 37 | 33 |
| 2879 | Router – ASR/3W | 19 | 456 | 38 | 38 | 34 |
| 1772 | HD 9.1GB @10000 | 13 | 456 | 39 | 38 | 34 |
| 2414 | HD 9.1GB @10000 /I | 13 | 454 | 40 | 40 | 35 |
| 2254 | HD 10GB /I | 13 | 453 | 41 | 41 | 36 |
| 1780 | C for SPNIX3.3 – Sys | 25 | 450 | 42 | 42 | 37 |
| 3020 | Envoy IC | 19 | 449 | 43 | 43 | 38 |
| 2400 | DIMM – 512 MB | 14 | 448 | 44 | 44 | 39 |
| 3069 | HD 10GB /S | 13 | 436 | 45 | 45 | 40 |
| 2497 | WSP DA-290 | 15 | 399 | 46 | 46 | 41 |
| 2253 | HD 10GB @5400 /SE | 13 | 399 | 47 | 46 | 41 |
| 2257 | HD 8GB /I | 13 | 399 | 48 | 46 | 41 |
| 3400 | HD 8GB /SE | 13 | 389 | 49 | 49 | 42 |
| 3057 | Monitor 17/SD | 11 | 369 | 50 | 50 | 43 |
| 2415 | HD 9.1GB @7200 | 13 | 359 | 51 | 51 | 44 |
| 2410 | HD 8.4GB @5400 | 13 | 357 | 52 | 52 | 45 |
| 2243 | Monitor 17/HR/F | 11 | 350 | 53 | 53 | 46 |
| 1797 | Inkjet C/8/HQ | 12 | 349 | 54 | 54 | 47 |
| 2878 | Router – ASR/2W | 19 | 345 | 55 | 55 | 48 |
| 1768 | HD 8.2GB @5400 | 13 | 345 | 56 | 55 | 48 |
| 1749 | DIMM – 256MB | 14 | 337 | 57 | 57 | 49 |
| 2266 | DVD 12x | 17 | 333 | 58 | 58 | 50 |
| 2378 | DIMM – 128 MB | 14 | 305 | 59 | 59 | 51 |
| 2337 | Business Cards – 1000/2L | 32 | 300 | 60 | 60 | 52 |
| 1739 | SDRAM – 128 MB | 14 | 299 | 61 | 61 | 53 |
| 3060 | Monitor 17/HR | 11 | 299 | 62 | 61 | 53 |
| 2496 | WSP DA-130 | 15 | 299 | 63 | 61 | 53 |
| 3091 | VRAM – 64 MB | 14 | 279 | 64 | 64 | 54 |
| 1791 | Industrial 700/HD | 12 | 275 | 65 | 65 | 55 |
| 3077 | DVD 8x | 17 | 274 | 66 | 66 | 56 |
| 2276 | SDRAM – 48 MB | 14 | 269 | 67 | 67 | 57 |
| 1726 | LCD Monitor 11/PM | 11 | 259 | 68 | 68 | 58 |
| 3088 | SDRAM – 32 MB | 14 | 258 | 69 | 69 | 59 |
| 2359 | LCD Monitor 9/PM | 11 | 249 | 70 | 70 | 60 |
| 1763 | DIMM – 64MB | 14 | 247 | 71 | 71 | 61 |
| 1781 | CDW 20/48/E | 17 | 233 | 72 | 72 | 62 |
| 2152 | Router – DTMF4 | 19 | 231 | 73 | 73 | 63 |
| 1792 | Industrial 600/DQ | 12 | 225 | 74 | 74 | 64 |
| 3073 | HD 6GB /I | 13 | 224 | 75 | 75 | 65 |
| 2406 | 64MB Cache /M | 14 | 223 | 76 | 76 | 66 |
| 2264 | CDW 20/48/I | 17 | 223 | 77 | 76 | 66 |
| 3248 | Smart Suite – S4.0/DE | 29 | 222 | 78 | 78 | 67 |
| 3246 | Smart Suite – S4.0/SP | 29 | 222 | 79 | 78 | 67 |
| 3253 | Smart Suite – S4.0/EN | 29 | 222 | 80 | 78 | 67 |
| 3245 | Smart Suite – S4.0/FR | 29 | 222 | 81 | 78 | 67 |
| 2404 | 64MB Cache /NM | 14 | 221 | 82 | 82 | 68 |
| 2424 | CDW 12/24 | 17 | 221 | 83 | 82 | 68 |
| 3086 | VRAM – 16 MB | 14 | 211 | 84 | 84 | 69 |
| 2409 | TD 7GB/8 | 17 | 210 | 85 | 85 | 70 |
| 2537 | Business Cards Box – 1000 | 32 | 200 | 86 | 86 | 71 |
| 2453 | Inkjet C/4 | 12 | 195 | 87 | 87 | 72 |
| 3090 | RAM – 48 MB | 14 | 193 | 88 | 88 | 73 |
| 2396 | EDO – 32MB | 14 | 179 | 89 | 89 | 74 |
| 1788 | CPU D600 | 15 | 178 | 90 | 90 | 75 |
| 2430 | Compact 400/LQ | 12 | 175 | 91 | 91 | 76 |
| 3170 | Smart Suite – V/SP | 29 | 161 | 92 | 92 | 77 |
| 2274 | RAM – 32 MB | 14 | 161 | 93 | 92 | 77 |
| 2422 | SPNIX4.0 – SAL | 24 | 150 | 94 | 94 | 78 |
| 2302 | Inkjet B/6 | 12 | 150 | 95 | 94 | 78 |
| 3171 | Smart Suite – S3.3/EN | 29 | 148 | 96 | 96 | 79 |
| 2371 | C for SPNIX4.0 – Doc | 25 | 146 | 97 | 97 | 80 |
| 2638 | Envoy DS/E | 19 | 137 | 98 | 98 | 81 |
| 2272 | RAM – 16 MB | 14 | 135 | 99 | 99 | 82 |
| 1740 | TD 12GB/DAT | 17 | 134 | 100 | 100 | 83 |
| 1761 | CD-ROM 600/I/32x | 17 | 134 | 101 | 100 | 83 |
| 2394 | DIMM – 32MB | 14 | 128 | 102 | 102 | 84 |
| 1794 | OSI 8-16/IL | 29 | 128 | 103 | 102 | 84 |
| 1779 | C for SPNIX3.3 – Doc | 25 | 128 | 104 | 102 | 84 |
| 2402 | CD-ROM 600/E/24x | 17 | 127 | 105 | 105 | 85 |
| 3204 | Envoy DS | 19 | 126 | 106 | 106 | 86 |
| 2986 | Manual – Vision OS/2x + | 33 | 125 | 107 | 107 | 87 |
| 1782 | Compact 400/DQ | 12 | 125 | 108 | 107 | 87 |
| 3087 | DIMM – 16 MB | 14 | 124 | 109 | 109 | 88 |
| 2395 | 32MB Cache /M | 14 | 123 | 110 | 110 | 89 |
| 2439 | CPU D400 | 15 | 123 | 111 | 110 | 89 |
| 1755 | 32MB Cache /NM | 14 | 121 | 112 | 112 | 90 |
| 3247 | Smart Suite – V/DE | 29 | 120 | 113 | 113 | 91 |
| 3176 | Smart Suite – V/EN | 29 | 120 | 114 | 113 | 91 |
| 3177 | Smart Suite – V/FR | 29 | 120 | 115 | 113 | 91 |
| 2403 | CD-ROM 600/I/24x | 17 | 117 | 116 | 116 | 92 |
| 2005 | IC Browser Doc – S | 29 | 115 | 117 | 117 | 93 |
| 2722 | PC Bag – L/D | 39 | 112 | 118 | 118 | 94 |
| 3359 | SDRAM – 16 MB | 14 | 111 | 119 | 119 | 95 |
| 1774 | Base ISO CP – BL | 29 | 110 | 120 | 120 | 96 |
| 3502 | C for SPNIX3.3 -Sys/U | 25 | 105 | 121 | 121 | 97 |
| 3114 | MB – S900/650+ | 15 | 101 | 122 | 122 | 98 |
| 1742 | CD-ROM 500/16x | 17 | 101 | 123 | 122 | 98 |
| 1787 | CPU D300 | 15 | 101 | 124 | 122 | 98 |
| 2452 | SPNIX4.0 – DL | 24 | 100 | 125 | 125 | 99 |
| 2335 | Mobile phone | 31 | 100 | 126 | 125 | 99 |
| 1801 | SPNIX3.3 – AL | 24 | 100 | 127 | 125 | 99 |
| 3362 | Web Browser – SB/S 4.0 | 29 | 99 | 128 | 128 | 100 |
| 2381 | CD-ROM 8x | 17 | 99 | 129 | 128 | 100 |
| 2293 | MB – S600 | 15 | 99 | 130 | 128 | 100 |
| 2262 | ZIP 100 | 17 | 98 | 131 | 131 | 101 |
| 2411 | GP 1280×1024 | 15 | 98 | 132 | 131 | 101 |
| 2412 | 8MB EDO Memory | 14 | 98 | 133 | 131 | 101 |
| 2377 | PS 110V HS/US | 19 | 97 | 134 | 134 | 102 |
| 2311 | PS 220V /L | 19 | 95 | 135 | 135 | 103 |
| 2370 | PS 220V /HS/FR | 19 | 91 | 136 | 136 | 104 |
| 2004 | IC Browser – S | 29 | 90 | 137 | 137 | 105 |
| 1733 | PS 220V /UK | 19 | 89 | 138 | 138 | 106 |
| 2752 | MB – S550 | 15 | 88 | 139 | 139 | 107 |
| 2721 | PC Bag – L/S | 39 | 87 | 140 | 140 | 108 |
| 1738 | PS 110V /US | 19 | 86 | 141 | 141 | 109 |
| 3173 | Graphics – SA | 29 | 86 | 142 | 141 | 109 |
| 3391 | PS 110/220 | 19 | 85 | 143 | 143 | 110 |
| 3124 | PS 110V /T | 19 | 84 | 144 | 144 | 111 |
| 2423 | C for SPNIX4.0 – 1 Seat | 25 | 84 | 145 | 144 | 111 |
| 2449 | OSI 1-4/IL | 29 | 83 | 146 | 146 | 112 |
| 2387 | PS 220V /FR | 19 | 83 | 147 | 146 | 112 |
| 1748 | PS 220V /EUR | 19 | 83 | 148 | 146 | 112 |
| 3082 | Modem – 56/90/E | 17 | 81 | 149 | 149 | 113 |
| 3123 | PS 220V /D | 19 | 81 | 150 | 149 | 113 |
| 3258 | Web Browser – SB/V 1.0 | 29 | 80 | 151 | 151 | 114 |
| 2536 | Business Cards – 250/2L | 32 | 80 | 152 | 151 | 114 |
| 2470 | SPNIX4.0 – NL | 24 | 80 | 153 | 151 | 114 |
| 2462 | SPNIX4.0 – UL/N | 24 | 80 | 154 | 151 | 114 |
| 2467 | SPNIX4.0 – UL/D | 24 | 80 | 155 | 151 | 114 |
| 1729 | Chemicals – RCP | 39 | 80 | 156 | 151 | 114 |
| 2365 | Chemicals – TCS | 39 | 78 | 157 | 157 | 115 |
| 3108 | KB E/EN | 16 | 78 | 158 | 157 | 115 |
| 2375 | GP 1024×768 | 15 | 78 | 159 | 157 | 115 |
| 3112 | MB – S500 | 15 | 77 | 160 | 160 | 116 |
| 2268 | Modem – 56/H/E | 17 | 77 | 161 | 160 | 116 |
| 2299 | PS 12V /P | 19 | 76 | 162 | 162 | 117 |
| 2468 | SPNIX4.0 – UL/C | 24 | 75 | 163 | 163 | 118 |
| 3101 | IC Browser – V | 29 | 75 | 164 | 163 | 118 |
| 2419 | Battery Backup (DA-290) | 19 | 72 | 165 | 165 | 119 |
| 2340 | Chemicals – SW | 39 | 72 | 166 | 165 | 119 |
| 2464 | SPNIX4.0 – UL/A | 24 | 70 | 167 | 167 | 120 |
| 2995 | SPNIX3.3 SAU | 24 | 70 | 168 | 167 | 120 |
| 3083 | Modem – 56/H/I | 17 | 67 | 169 | 169 | 121 |
| 2260 | DFD 1.44/3.5 | 17 | 67 | 170 | 169 | 121 |
| 3257 | Web Browser – SB/S 2.1 | 29 | 66 | 171 | 171 | 122 |
| 2751 | MB – S450 | 15 | 66 | 172 | 171 | 122 |
| 2270 | Modem – 56/90/I | 17 | 66 | 173 | 171 | 122 |
| 3290 | SPNIX3.3 DU | 24 | 65 | 174 | 174 | 123 |
| 2374 | Modem – C/100 | 17 | 65 | 175 | 174 | 123 |
| 1804 | SPNIX3.3 – UL/N | 24 | 65 | 176 | 174 | 123 |
| 3182 | Word Processing – SWP/V 4.5 | 22 | 65 | 177 | 174 | 123 |
| 2782 | PC Bag – C/S | 39 | 62 | 178 | 178 | 124 |
| 1778 | C for SPNIX3.3 – 1 Seat | 25 | 62 | 179 | 178 | 124 |
| 2418 | Battery Backup (DA-130) | 19 | 61 | 180 | 180 | 125 |
| 1803 | SPNIX3.3 – DL | 24 | 60 | 181 | 181 | 126 |
| 2308 | Video Card /E32 | 15 | 58 | 182 | 182 | 127 |
| 2336 | Business Cards Box – 250 | 32 | 55 | 183 | 183 | 128 |
| 3167 | Manual – Vision OS/2.x | 33 | 55 | 184 | 183 | 128 |
| 2049 | MB – S300 | 15 | 55 | 185 | 183 | 128 |
| 1820 | SPNIX3.3 – NL | 24 | 55 | 186 | 183 | 128 |
| 2278 | Battery – NiHM | 19 | 55 | 187 | 183 | 128 |
| 1808 | SPNIX3.3 – UL/D | 24 | 55 | 188 | 183 | 128 |
| 2976 | Drive Mount – D | 19 | 52 | 189 | 189 | 129 |
| 3179 | Spreadsheet – SSS/S 2.1 | 21 | 50 | 190 | 190 | 130 |
| 3183 | Word Processing – SWS/V 4.5 | 22 | 50 | 191 | 190 | 130 |
| 1806 | SPNIX3.3 – UL/C | 24 | 50 | 192 | 190 | 130 |
| 3262 | Spreadsheet – SSS/S 2.2 | 21 | 50 | 193 | 190 | 130 |
| 1805 | SPNIX3.3 – UL/A | 24 | 50 | 194 | 190 | 130 |
| 3260 | Word Processing – SWP/S 4.4 | 22 | 50 | 195 | 190 | 130 |
| 3155 | Monitor Hinge – HD | 11 | 49 | 196 | 196 | 131 |
| 1769 | GP 800×600 | 15 | 48 | 197 | 197 | 132 |
| 3133 | Video Card /32 | 15 | 48 | 198 | 197 | 132 |
| 3110 | KB 101/FR | 16 | 48 | 199 | 197 | 132 |
| 2289 | KB 101/ES | 16 | 48 | 200 | 197 | 132 |
| 3106 | KB 101/EN | 16 | 48 | 201 | 197 | 132 |
| 3225 | Card Organizer – 1000 | 32 | 47 | 202 | 202 | 133 |
| 3129 | Sound Card STD | 15 | 46 | 203 | 203 | 134 |
| 3220 | Manual – Vision OS/1.2 | 33 | 45 | 204 | 204 | 135 |
| 2492 | SPNIX3.3 AU | 24 | 45 | 205 | 204 | 135 |
| 3197 | Spreadsheet – SSS/V 2.1 | 21 | 45 | 206 | 204 | 135 |
| 3178 | Spreadsheet – SSP/V 2.0 | 21 | 45 | 207 | 204 | 135 |
| 2982 | Drive Mount – A | 19 | 44 | 208 | 208 | 136 |
| 2522 | Battery – EL | 19 | 44 | 209 | 208 | 136 |
| 2261 | FD 1.44/3.5/E | 17 | 42 | 210 | 210 | 137 |
| 3172 | Graphics – DIK+ | 29 | 42 | 211 | 210 | 137 |
| 3117 | Mouse C/E | 16 | 41 | 212 | 212 | 138 |
| 2416 | Client ISO CP – S | 29 | 41 | 213 | 212 | 138 |
| 3165 | Manual – Vision Tools2.0 | 33 | 40 | 214 | 214 | 139 |
| 3361 | Spreadsheet – SSP/S 1.5 | 21 | 40 | 215 | 214 | 139 |
| 3256 | Spreadsheet – SSS/V 2.0 | 21 | 40 | 216 | 214 | 139 |
| 2259 | FD 1.44/3.5 | 17 | 39 | 217 | 217 | 140 |
| 3234 | Monitor Hinge – STD | 11 | 39 | 218 | 217 | 140 |
| 3175 | Project Management – S4.0 | 29 | 37 | 219 | 219 | 141 |
| 3277 | Drive Mount – A/T | 19 | 36 | 220 | 220 | 142 |
| 3255 | Spreadsheet – SSS/CD 2.2B | 21 | 35 | 221 | 221 | 143 |
| 3163 | Manual – Vision Net6.3/US | 33 | 35 | 222 | 221 | 143 |
| 2417 | Client ISO CP – V | 29 | 33 | 223 | 223 | 144 |
| 3224 | Card Organizer – 250 | 32 | 32 | 224 | 224 | 145 |
| 3251 | Project Management – V | 29 | 31 | 225 | 225 | 146 |
| 3216 | Manual – Vision Net6.3 | 33 | 30 | 226 | 226 | 147 |
| 3250 | Graphics – DIK | 29 | 28 | 227 | 227 | 148 |
| 2761 | Mouse +WP/CL | 16 | 27 | 228 | 228 | 149 |
| 1775 | Client ISO CP – S | 29 | 27 | 229 | 228 | 149 |
| 3252 | Project Management – S3.3 | 29 | 26 | 230 | 230 | 150 |
| 2493 | SPNIX3.3 C/DU | 24 | 25 | 231 | 231 | 151 |
| 2494 | SPNIX3.3 NU | 24 | 25 | 232 | 231 | 151 |
| 2319 | Screws <Z.24.S> | 19 | 25 | 233 | 231 | 151 |
| 2339 | Paper – Std Printer | 32 | 25 | 234 | 231 | 151 |
| 1825 | X25 – 1 Line License | 29 | 25 | 235 | 231 | 151 |
| 3140 | Screws <Z.16.S> | 19 | 24 | 236 | 236 | 152 |
| 3300 | Screws <S.32.P> | 19 | 23 | 237 | 237 | 153 |
| 2058 | Mouse +WP | 16 | 23 | 238 | 237 | 153 |
| 2322 | Screws <Z.28.P> | 19 | 23 | 239 | 237 | 153 |
| 2316 | Screws <S.32.S> | 19 | 22 | 240 | 240 | 154 |
| 3503 | C for SPNIX3.3 – Seat/U | 25 | 22 | 241 | 240 | 154 |
| 3139 | Screws <S.16.S> | 19 | 21 | 242 | 242 | 155 |
| 2144 | Card Organizer Cover | 32 | 18 | 243 | 243 | 156 |
| 1940 | ESD Bracelet/Clip | 39 | 18 | 244 | 243 | 156 |
| 3150 | Card Holder – 25 | 32 | 18 | 245 | 243 | 156 |
| 3134 | Screws <B.32.S> | 19 | 18 | 246 | 243 | 156 |
| 2323 | Screws <B.32.P> | 19 | 18 | 247 | 243 | 156 |
| 3143 | Screws <B.28.S> | 19 | 16 | 248 | 248 | 157 |
| 1912 | SS Stock – 3mm | 39 | 15 | 249 | 249 | 158 |
| 2631 | ESD Bracelet/QR | 39 | 15 | 250 | 249 | 158 |
| 3301 | Screws <B.28.P> | 19 | 15 | 251 | 249 | 158 |
| 1910 | FG Stock – H | 39 | 14 | 252 | 252 | 159 |
| 3209 | Sharpener – Pencil | 32 | 13 | 253 | 253 | 160 |
| 2596 | SS Stock – 1mm | 39 | 12 | 254 | 254 | 161 |
| 2030 | Latex Gloves | 39 | 12 | 255 | 254 | 161 |
| 3051 | Pens – 10/MP | 32 | 12 | 256 | 254 | 161 |
| 2783 | Clips – Paper | 32 | 10 | 257 | 257 | 162 |
| 2594 | FG Stock – L | 39 | 9 | 258 | 258 | 163 |
| 3511 | Paper – HQ Printer | 32 | 9 | 259 | 258 | 163 |
| 1745 | Cable SCSI 20/WD->D | 19 | 9 | 260 | 258 | 163 |
| 2093 | Pens – 10/FP | 32 | 8 | 261 | 261 | 164 |
| 2056 | Mouse Pad /CL | 16 | 8 | 262 | 261 | 164 |
| 1737 | Cable SCSI 10/FW/ADS | 19 | 8 | 263 | 261 | 164 |
| 2373 | Cable RS232 10/AF | 19 | 6 | 264 | 264 | 165 |
| 2810 | Inkvisible Pens | 32 | 6 | 265 | 264 | 165 |
| 2380 | Cable PR/15/P | 19 | 6 | 266 | 264 | 165 |
| 1734 | Cable RS232 10/AM | 19 | 6 | 267 | 264 | 165 |
| 2870 | Pencil – Mech | 32 | 5 | 268 | 268 | 166 |
| 2457 | Cable PR/S/6 | 19 | 5 | 269 | 268 | 166 |
| 2725 | Machine Oil | 39 | 4 | 270 | 270 | 167 |
| 2408 | Cable PR/P/6 | 19 | 4 | 271 | 270 | 167 |
| 2334 | Resin | 39 | 4 | 272 | 270 | 167 |
| 3099 | Cable Harness | 19 | 4 | 273 | 270 | 167 |
| 2211 | Wrist Pad | 16 | 4 | 274 | 270 | 167 |
| 3097 | Cable Connector – 32R | 19 | 3 | 275 | 275 | 168 |
| 3193 | Plastic Stock – W/HD | 39 | 3 | 276 | 275 | 168 |
| 3187 | Plastic Stock – B/HD | 39 | 3 | 277 | 275 | 168 |
| 2944 | Wrist Pad /CL | 16 | 3 | 278 | 275 | 168 |
| 3515 | Lead Replacement | 32 | 2 | 279 | 279 | 169 |
| 3189 | Plastic Stock – G | 39 | 2 | 280 | 279 | 169 |
| 2330 | Plastic Stock – R | 39 | 2 | 281 | 279 | 169 |
| 2326 | Plastic Stock – Y | 39 | 2 | 282 | 279 | 169 |
| 3208 | Pencils – Wood | 32 | 2 | 283 | 279 | 169 |
| 3191 | Plastic Stock – O | 39 | 2 | 284 | 279 | 169 |
| 2091 | Paper Tablet LW 8 1/2 x 11 | 32 | 1 | 285 | 285 | 170 |
| 2808 | Paper Tablet LY 8 1/2 x 11 | 32 | 1 | 286 | 285 | 170 |
SELECT
oe.product_information.product_id,
oe.product_information.product_name,
oe.product_information.category_id,
oe.product_information.list_price,
ROW_NUMBER() OVER (
ORDER BY oe.product_information.list_price DESC
) AS row_number,
RANK() OVER (
ORDER BY oe.product_information.list_price DESC
) AS rank,
DENSE_RANK() OVER (
ORDER BY oe.product_information.list_price DESC
) AS dense_rank
FROM
oe.product_information
WHERE
oe.product_information.list_price IS NOT NULL
ORDER BY
list_price DESC;
Function Choice
The result set and corresponding SQL code above highlight, at a high level, the similarities and differences among the ranking functions; however, when solving real world problems, you will need to know more than the overlap in syntax and the differences in the rank calculations. You will need to choose the right function for the task at hand.
Is the function being used to assign integer values to each row for reference purposes (e.g., “The sale in question is in row 23.”) or to implement a ranking system (e.g., sales representatives ranked by total sales, descending)? If it is the former of the two, then use the ROW_NUMBER function. If it is the latter of the two, then a conscious choice between the use of RANK and DENSE_RANK is required.
When choosing between the two functions, it is important to consider the impact ties have on the intended message and the overall consequences. For example, imagine annual bonuses at a company are rewarded to product owners with products ranked within the top 5 in terms of distinct purchases. Given the data below, six products are in the top 5 when RANK is used while eight are in the top 5 when DENSE_RANK is used. Choosing one method over another will result in more or less product owners being rewarded an annual bonus.
| product_id | product_name | distinct_purchases | rank | dense_rank |
|---|---|---|---|---|
| 3106 | KB 101/EN | 21 | 1 | 1 |
| 3143 | Screws <B.28.S> | 18 | 2 | 2 |
| 3127 | LaserPro 600/6/BW | 17 | 3 | 3 |
| 3150 | Card Holder – 25 | 16 | 4 | 4 |
| 3155 | Monitor Hinge – HD | 16 | 4 | 4 |
| 3123 | PS 220V /D | 16 | 4 | 4 |
| 2289 | KB 101/ES | 15 | 7 | 5 |
| 3117 | Mouse C/E | 15 | 7 | 5 |
| 2311 | PS 220V /L | 14 | 9 | 6 |
| 3163 | Manual – Vision Net6.3/US | 14 | 9 | 6 |
| 3133 | Video Card /32 | 13 | 11 | 7 |
| 2299 | PS 12V /P | 12 | 12 | 8 |
| 3114 | MB – S900/650+ | 11 | 13 | 9 |
| 3139 | Screws <S.16.S> | 11 | 13 | 9 |
| 3165 | Manual – Vision Tools2.0 | 11 | 13 | 9 |
| 2326 | Plastic Stock – Y | 11 | 13 | 9 |
| 2316 | Screws <S.32.S> | 10 | 17 | 10 |
| 2308 | Video Card /E32 | 10 | 17 | 10 |
| 2293 | MB – S600 | 10 | 17 | 10 |
| 2330 | Plastic Stock – R | 9 | 20 | 11 |
| 2334 | Resin | 9 | 20 | 11 |
| 3129 | Sound Card STD | 9 | 20 | 11 |
| 3108 | KB E/EN | 9 | 20 | 11 |
| 3167 | Manual – Vision OS/2.x | 8 | 24 | 12 |
| 2359 | LCD Monitor 9/PM | 8 | 24 | 12 |
| 2302 | Inkjet B/6 | 8 | 24 | 12 |
| 3110 | KB 101/FR | 7 | 27 | 13 |
| 3140 | Screws <Z.16.S> | 7 | 27 | 13 |
| 2350 | Desk – W/48 | 7 | 27 | 13 |
| 3112 | MB – S500 | 6 | 30 | 14 |
| 3170 | Smart Suite – V/SP | 6 | 30 | 14 |
| 2339 | Paper – Std Printer | 6 | 30 | 14 |
| 3134 | Screws <B.32.S> | 6 | 30 | 14 |
| 2323 | Screws <B.32.P> | 6 | 30 | 14 |
| 2319 | Screws <Z.24.S> | 6 | 30 | 14 |
| 1910 | FG Stock – H | 5 | 36 | 15 |
| 2322 | Screws <Z.28.P> | 5 | 36 | 15 |
| 3124 | PS 110V /T | 5 | 36 | 15 |
| 2976 | Drive Mount – D | 5 | 36 | 15 |
| 3176 | Smart Suite – V/EN | 5 | 36 | 15 |
| 2522 | Battery – EL | 4 | 41 | 16 |
| 2496 | WSP DA-130 | 4 | 41 | 16 |
| 2430 | Compact 400/LQ | 4 | 41 | 16 |
| 2211 | Wrist Pad | 4 | 41 | 16 |
| 2264 | CDW 20/48/I | 4 | 41 | 16 |
| 3216 | Manual – Vision Net6.3 | 4 | 41 | 16 |
| 2252 | Monitor 21/HR/M | 4 | 41 | 16 |
| 2457 | Cable PR/S/6 | 4 | 41 | 16 |
| 2986 | Manual – Vision OS/2x + | 4 | 41 | 16 |
| 2268 | Modem – 56/H/E | 4 | 41 | 16 |
| 2335 | Mobile phone | 4 | 41 | 16 |
| 1797 | Inkjet C/8/HQ | 3 | 52 | 17 |
| 2245 | Monitor 19/SD/M | 3 | 52 | 17 |
| 3193 | Plastic Stock – W/HD | 3 | 52 | 17 |
| 2337 | Business Cards – 1000/2L | 3 | 52 | 17 |
| 3224 | Card Organizer – 250 | 3 | 52 | 17 |
| 2439 | CPU D400 | 3 | 52 | 17 |
| 3097 | Cable Connector – 32R | 3 | 52 | 17 |
| 2423 | C for SPNIX4.0 – 1 Seat | 3 | 52 | 17 |
| 2422 | SPNIX4.0 – SAL | 3 | 52 | 17 |
| 3082 | Modem – 56/90/E | 3 | 52 | 17 |
| 2365 | Chemicals – TCS | 3 | 52 | 17 |
| 2982 | Drive Mount – A | 3 | 52 | 17 |
| 3220 | Manual – Vision OS/1.2 | 3 | 52 | 17 |
| 2761 | Mouse +WP/CL | 3 | 52 | 17 |
| 1820 | SPNIX3.3 – NL | 3 | 52 | 17 |
| 2537 | Business Cards Box – 1000 | 3 | 52 | 17 |
| 1948 | Envoy IC/58 | 3 | 52 | 17 |
| 2270 | Modem – 56/90/I | 3 | 52 | 17 |
| 2721 | PC Bag – L/S | 3 | 52 | 17 |
| 2536 | Business Cards – 250/2L | 3 | 52 | 17 |
| 2999 | Laptop 16/8/110 | 3 | 52 | 17 |
| 3172 | Graphics – DIK+ | 3 | 52 | 17 |
| 3051 | Pens – 10/MP | 2 | 74 | 18 |
| 2995 | SPNIX3.3 SAU | 2 | 74 | 18 |
| 3350 | Plasma Monitor 10/LE/VGA | 2 | 74 | 18 |
| 2409 | TD 7GB/8 | 2 | 74 | 18 |
| 2394 | DIMM – 32MB | 2 | 74 | 18 |
| 3099 | Cable Harness | 2 | 74 | 18 |
| 3245 | Smart Suite – S4.0/FR | 2 | 74 | 18 |
| 2471 | SPNIX3.3 SU | 2 | 74 | 18 |
| 3173 | Graphics – SA | 2 | 74 | 18 |
| 2464 | SPNIX4.0 – UL/A | 2 | 74 | 18 |
| 2276 | SDRAM – 48 MB | 2 | 74 | 18 |
| 3000 | Laptop 32/10/56 | 2 | 74 | 18 |
| 2340 | Chemicals – SW | 2 | 74 | 18 |
| 3003 | Laptop 128/12/56/v90/110 | 2 | 74 | 18 |
| 2751 | MB – S450 | 2 | 74 | 18 |
| 2492 | SPNIX3.3 AU | 2 | 74 | 18 |
| 2418 | Battery Backup (DA-130) | 2 | 74 | 18 |
| 2462 | SPNIX4.0 – UL/N | 2 | 74 | 18 |
| 3182 | Word Processing – SWP/V 4.5 | 2 | 74 | 18 |
| 2243 | Monitor 17/HR/F | 2 | 74 | 18 |
| 2274 | RAM – 32 MB | 2 | 74 | 18 |
| 3187 | Plastic Stock – B/HD | 2 | 74 | 18 |
| 2370 | PS 220V /HS/FR | 2 | 74 | 18 |
| 3246 | Smart Suite – S4.0/SP | 2 | 74 | 18 |
| 3501 | C for SPNIX4.0 – Sys | 2 | 74 | 18 |
| 1822 | SPNIX4.0 – SL | 2 | 74 | 18 |
| 3171 | Smart Suite – S3.3/EN | 2 | 74 | 18 |
| 2272 | RAM – 16 MB | 2 | 74 | 18 |
| 2459 | LaserPro 1200/8/BW | 2 | 74 | 18 |
| 3290 | SPNIX3.3 DU | 2 | 74 | 18 |
| 2870 | Pencil – Mech | 2 | 74 | 18 |
| 2278 | Battery – NiHM | 2 | 74 | 18 |
| 2257 | HD 8GB /I | 2 | 74 | 18 |
| 3234 | Monitor Hinge – STD | 2 | 74 | 18 |
| 2373 | Cable RS232 10/AF | 2 | 74 | 18 |
| 3359 | SDRAM – 16 MB | 2 | 74 | 18 |
| 1808 | SPNIX3.3 – UL/D | 2 | 74 | 18 |
| 2467 | SPNIX4.0 – UL/D | 2 | 74 | 18 |
| 3208 | Pencils – Wood | 2 | 74 | 18 |
| 3086 | VRAM – 16 MB | 2 | 74 | 18 |
| 2414 | HD 9.1GB @10000 /I | 1 | 114 | 19 |
| 1791 | Industrial 700/HD | 1 | 114 | 19 |
| 3251 | Project Management – V | 1 | 114 | 19 |
| 2381 | CD-ROM 8x | 1 | 114 | 19 |
| 3253 | Smart Suite – S4.0/EN | 1 | 114 | 19 |
| 3252 | Project Management – S3.3 | 1 | 114 | 19 |
| 2400 | DIMM – 512 MB | 1 | 114 | 19 |
| 1912 | SS Stock – 3mm | 1 | 114 | 19 |
| 2395 | 32MB Cache /M | 1 | 114 | 19 |
| 3060 | Monitor 17/HR | 1 | 114 | 19 |
| 3353 | HD 10GB /R | 1 | 114 | 19 |
| 3362 | Web Browser – SB/S 4.0 | 1 | 114 | 19 |
| 3204 | Envoy DS | 1 | 114 | 19 |
| 3091 | VRAM – 64 MB | 1 | 114 | 19 |
| 1825 | X25 – 1 Line License | 1 | 114 | 19 |
| 3069 | HD 10GB /S | 1 | 114 | 19 |
| 3090 | RAM – 48 MB | 1 | 114 | 19 |
| 2470 | SPNIX4.0 – NL | 1 | 114 | 19 |
| 2375 | GP 1024×768 | 1 | 114 | 19 |
| 2419 | Battery Backup (DA-290) | 1 | 114 | 19 |
| 2424 | CDW 12/24 | 1 | 114 | 19 |
| 2266 | DVD 12x | 1 | 114 | 19 |
| 2412 | 8MB EDO Memory | 1 | 114 | 19 |
| 3209 | Sharpener – Pencil | 1 | 114 | 19 |
| 3248 | Smart Suite – S4.0/DE | 1 | 114 | 19 |
| 2402 | CD-ROM 600/E/24x | 1 | 114 | 19 |
| 3197 | Spreadsheet – SSS/V 2.1 | 1 | 114 | 19 |
| 2752 | MB – S550 | 1 | 114 | 19 |
| 2410 | HD 8.4GB @5400 | 1 | 114 | 19 |
| 3077 | DVD 8x | 1 | 114 | 19 |
| 3260 | Word Processing – SWP/S 4.4 | 1 | 114 | 19 |
| 2449 | OSI 1-4/IL | 1 | 114 | 19 |
| 2403 | CD-ROM 600/I/24x | 1 | 114 | 19 |
| 3256 | Spreadsheet – SSS/V 2.0 | 1 | 114 | 19 |
| 2415 | HD 9.1GB @7200 | 1 | 114 | 19 |
| 3183 | Word Processing – SWS/V 4.5 | 1 | 114 | 19 |
| 3101 | IC Browser – V | 1 | 114 | 19 |
| 2058 | Mouse +WP | 1 | 114 | 19 |
| 2406 | 64MB Cache /M | 1 | 114 | 19 |
| 3250 | Graphics – DIK | 1 | 114 | 19 |
| 2810 | Inkvisible Pens | 1 | 114 | 19 |
| 1806 | SPNIX3.3 – UL/C | 1 | 114 | 19 |
| 3515 | Lead Replacement | 1 | 114 | 19 |
| 2254 | HD 10GB /I | 1 | 114 | 19 |
| 2638 | Envoy DS/E | 1 | 114 | 19 |
| 2093 | Pens – 10/FP | 1 | 114 | 19 |
| 2377 | PS 110V HS/US | 1 | 114 | 19 |
| 2782 | PC Bag – C/S | 1 | 114 | 19 |
| 2262 | ZIP 100 | 1 | 114 | 19 |
| 2594 | FG Stock – L | 1 | 114 | 19 |
| 1781 | CDW 20/48/E | 1 | 114 | 19 |
| 3354 | HD 12GB /I | 1 | 114 | 19 |
| 1803 | SPNIX3.3 – DL | 1 | 114 | 19 |
| 2380 | Cable PR/15/P | 1 | 114 | 19 |
| 2725 | Machine Oil | 1 | 114 | 19 |
| 2878 | Router – ASR/2W | 1 | 114 | 19 |
| 3511 | Paper – HQ Printer | 1 | 114 | 19 |
| 2417 | Client ISO CP – V | 1 | 114 | 19 |
| 1799 | SPNIX3.3 – SL | 1 | 114 | 19 |
| 2382 | HD 18.2GB@10000 /I | 1 | 114 | 19 |
| 3064 | Monitor 21/SD | 1 | 114 | 19 |
| 2255 | HD 12GB @7200 /SE | 1 | 114 | 19 |
| 2783 | Clips – Paper | 1 | 114 | 19 |
| 2808 | Paper Tablet LY 8 1/2 x 11 | 1 | 114 | 19 |
| 1787 | CPU D300 | 1 | 114 | 19 |
| 2378 | DIMM – 128 MB | 1 | 114 | 19 |
| 3191 | Plastic Stock – O | 1 | 114 | 19 |
| 1782 | Compact 400/DQ | 1 | 114 | 19 |
| 3087 | DIMM – 16 MB | 1 | 114 | 19 |
| 2253 | HD 10GB @5400 /SE | 1 | 114 | 19 |
| 2236 | Plasma Monitor 10/TFT/XGA | 1 | 114 | 19 |
| 3258 | Web Browser – SB/V 1.0 | 1 | 114 | 19 |
SELECT
oe.product_information.product_id,
oe.product_information.product_name,
COUNT(DISTINCT oe.order_items.order_id) AS distinct_purchases,
RANK() OVER (
ORDER BY COUNT(DISTINCT oe.order_items.order_id) DESC
) AS rank,
DENSE_RANK() OVER (
ORDER BY COUNT(DISTINCT oe.order_items.order_id) DESC
) AS dense_rank
FROM
oe.order_items
INNER JOIN
oe.product_information
ON oe.order_items.product_id = oe.product_information.product_id
GROUP BY
oe.product_information.product_id,
oe.product_information.product_name
ORDER BY
distinct_purchases DESC;
If desired, each ranking function’s rules could be expanded to account for those instances where ties occur. For example, in the instances where multiple products have the same distinct purchases count, the total sales generated by each of the products could be used to resolve the ties. Given the data type of total sales, this will largely result in the functions assigning the same integer value to each row. In fact, there is only one tie, and it is inconsequential to our scenario here. See the result set and corresponding SQL code below.
| product_id | product_name | distinct_purchases | total_sales | rank | dense_rank |
|---|---|---|---|---|---|
| 3106 | KB 101/EN | 21 | 82490 | 1 | 1 |
| 3143 | Screws <B.28.S> | 18 | 15777 | 2 | 2 |
| 3127 | LaserPro 600/6/BW | 17 | 364351 | 3 | 3 |
| 3123 | PS 220V /D | 16 | 59428.5 | 4 | 4 |
| 3155 | Monitor Hinge – HD | 16 | 45054 | 5 | 5 |
| 3150 | Card Holder – 25 | 16 | 15171 | 6 | 6 |
| 2289 | KB 101/ES | 15 | 78099 | 7 | 7 |
| 3117 | Mouse C/E | 15 | 42487 | 8 | 8 |
| 2311 | PS 220V /L | 14 | 89411.7 | 9 | 9 |
| 3163 | Manual – Vision Net6.3/US | 14 | 27992 | 10 | 10 |
| 3133 | Video Card /32 | 13 | 25156 | 11 | 11 |
| 2299 | PS 12V /P | 12 | 41923 | 12 | 12 |
| 3114 | MB – S900/650+ | 11 | 33978.4 | 13 | 13 |
| 3165 | Manual – Vision Tools2.0 | 11 | 25466 | 14 | 14 |
| 3139 | Screws <S.16.S> | 11 | 11116 | 15 | 15 |
| 2326 | Plastic Stock – Y | 11 | 1036.2 | 16 | 16 |
| 2308 | Video Card /E32 | 10 | 37277 | 17 | 17 |
| 2293 | MB – S600 | 10 | 22896 | 18 | 18 |
| 2316 | Screws <S.32.S> | 10 | 13005 | 19 | 19 |
| 3108 | KB E/EN | 9 | 42865 | 20 | 20 |
| 3129 | Sound Card STD | 9 | 23442 | 21 | 21 |
| 2334 | Resin | 9 | 1752.3 | 22 | 22 |
| 2330 | Plastic Stock – R | 9 | 698.5 | 23 | 23 |
| 2359 | LCD Monitor 9/PM | 8 | 180872.8 | 24 | 24 |
| 2302 | Inkjet B/6 | 8 | 48189.4 | 25 | 25 |
| 3167 | Manual – Vision OS/2.x | 8 | 24656 | 26 | 26 |
| 2350 | Desk – W/48 | 7 | 922708.6 | 27 | 27 |
| 3110 | KB 101/FR | 7 | 12383 | 28 | 28 |
| 3140 | Screws <Z.16.S> | 7 | 7015 | 29 | 29 |
| 3170 | Smart Suite – V/SP | 6 | 31363.2 | 30 | 30 |
| 3112 | MB – S500 | 6 | 27471 | 31 | 31 |
| 2339 | Paper – Std Printer | 6 | 12475 | 32 | 32 |
| 2319 | Screws <Z.24.S> | 6 | 8831 | 33 | 33 |
| 3134 | Screws <B.32.S> | 6 | 6172 | 34 | 34 |
| 2323 | Screws <B.32.P> | 6 | 5744 | 35 | 35 |
| 3176 | Smart Suite – V/EN | 5 | 35513.4 | 36 | 36 |
| 3124 | PS 110V /T | 5 | 13357 | 37 | 37 |
| 2322 | Screws <Z.28.P> | 5 | 6907 | 38 | 38 |
| 2976 | Drive Mount – D | 5 | 2806 | 39 | 39 |
| 1910 | FG Stock – H | 5 | 461 | 40 | 40 |
| 2252 | Monitor 21/HR/M | 4 | 134079 | 41 | 41 |
| 2496 | WSP DA-130 | 4 | 23887.6 | 42 | 42 |
| 2335 | Mobile phone | 4 | 22273 | 43 | 43 |
| 2264 | CDW 20/48/I | 4 | 21901 | 44 | 44 |
| 2430 | Compact 400/LQ | 4 | 16249.2 | 45 | 45 |
| 2268 | Modem – 56/H/E | 4 | 15977 | 46 | 46 |
| 2986 | Manual – Vision OS/2x + | 4 | 2782 | 47 | 47 |
| 2522 | Battery – EL | 4 | 1386 | 48 | 48 |
| 2211 | Wrist Pad | 4 | 1263.9 | 49 | 49 |
| 3216 | Manual – Vision Net6.3 | 4 | 949 | 50 | 50 |
| 2457 | Cable PR/S/6 | 4 | 484 | 51 | 51 |
| 2245 | Monitor 19/SD/M | 3 | 61908 | 52 | 52 |
| 2999 | Laptop 16/8/110 | 3 | 37840 | 53 | 53 |
| 1948 | Envoy IC/58 | 3 | 27306.4 | 54 | 54 |
| 2365 | Chemicals – TCS | 3 | 20438 | 55 | 55 |
| 2337 | Business Cards – 1000/2L | 3 | 15424.2 | 56 | 56 |
| 2422 | SPNIX4.0 – SAL | 3 | 11816 | 57 | 57 |
| 2439 | CPU D400 | 3 | 9823 | 58 | 58 |
| 2537 | Business Cards Box – 1000 | 3 | 9486.4 | 59 | 59 |
| 1797 | Inkjet C/8/HQ | 3 | 9088.8 | 60 | 60 |
| 3172 | Graphics – DIK+ | 3 | 6504 | 61 | 61 |
| 2536 | Business Cards – 250/2L | 3 | 5370 | 62 | 62 |
| 2270 | Modem – 56/90/I | 3 | 3794 | 63 | 63 |
| 3082 | Modem – 56/90/E | 3 | 2067 | 64 | 64 |
| 2423 | C for SPNIX4.0 – 1 Seat | 3 | 2001 | 65 | 65 |
| 1820 | SPNIX3.3 – NL | 3 | 1814 | 66 | 66 |
| 2721 | PC Bag – L/S | 3 | 1360 | 67 | 67 |
| 3220 | Manual – Vision OS/1.2 | 3 | 1255 | 68 | 68 |
| 2761 | Mouse +WP/CL | 3 | 1066 | 69 | 69 |
| 3224 | Card Organizer – 250 | 3 | 800 | 70 | 70 |
| 2982 | Drive Mount – A | 3 | 327 | 71 | 71 |
| 3193 | Plastic Stock – W/HD | 3 | 81.4 | 72 | 72 |
| 3097 | Cable Connector – 32R | 3 | 48.4 | 73 | 73 |
| 3003 | Laptop 128/12/56/v90/110 | 2 | 97464.4 | 74 | 74 |
| 1822 | SPNIX4.0 – SL | 2 | 51598.8 | 75 | 75 |
| 2257 | HD 8GB /I | 2 | 45731.4 | 76 | 76 |
| 2459 | LaserPro 1200/8/BW | 2 | 40612 | 77 | 77 |
| 3000 | Laptop 32/10/56 | 2 | 32383.2 | 78 | 78 |
| 3501 | C for SPNIX4.0 – Sys | 2 | 22668.8 | 79 | 79 |
| 3171 | Smart Suite – S3.3/EN | 2 | 20328 | 80 | 80 |
| 2243 | Monitor 17/HR/F | 2 | 15613.4 | 81 | 81 |
| 3182 | Word Processing – SWP/V 4.5 | 2 | 11942 | 82 | 82 |
| 3350 | Plasma Monitor 10/LE/VGA | 2 | 11781 | 83 | 83 |
| 2409 | TD 7GB/8 | 2 | 11487.3 | 84 | 84 |
| 2276 | SDRAM – 48 MB | 2 | 9933 | 85 | 85 |
| 3246 | Smart Suite – S4.0/SP | 2 | 6793.6 | 86 | 86 |
| 3245 | Smart Suite – S4.0/FR | 2 | 6220.5 | 87 | 87 |
| 2462 | SPNIX4.0 – UL/N | 2 | 6169 | 88 | 88 |
| 2274 | RAM – 32 MB | 2 | 5991 | 89 | 89 |
| 2394 | DIMM – 32MB | 2 | 5480.2 | 90 | 90 |
| 2467 | SPNIX4.0 – UL/D | 2 | 5179 | 91 | 91 |
| 2340 | Chemicals – SW | 2 | 4882 | 92 | 92 |
| 3173 | Graphics – SA | 2 | 4248 | 93 | 93 |
| 2370 | PS 220V /HS/FR | 2 | 4067 | 94 | 94 |
| 2418 | Battery Backup (DA-130) | 2 | 3900 | 95 | 95 |
| 2471 | SPNIX3.3 SU | 2 | 3863.2 | 96 | 96 |
| 2272 | RAM – 16 MB | 2 | 3807 | 97 | 97 |
| 3290 | SPNIX3.3 DU | 2 | 3657 | 98 | 98 |
| 3086 | VRAM – 16 MB | 2 | 3159 | 99 | 99 |
| 3359 | SDRAM – 16 MB | 2 | 2442 | 100 | 100 |
| 2464 | SPNIX4.0 – UL/A | 2 | 1740 | 101 | 101 |
| 1808 | SPNIX3.3 – UL/D | 2 | 1595 | 102 | 102 |
| 2278 | Battery – NiHM | 2 | 1397 | 103 | 103 |
| 3234 | Monitor Hinge – STD | 2 | 1041 | 104 | 104 |
| 2995 | SPNIX3.3 SAU | 2 | 751 | 105 | 105 |
| 2751 | MB – S450 | 2 | 493 | 106 | 106 |
| 3051 | Pens – 10/MP | 2 | 372 | 107 | 107 |
| 2492 | SPNIX3.3 AU | 2 | 293 | 108 | 108 |
| 2373 | Cable RS232 10/AF | 2 | 144 | 109 | 109 |
| 3187 | Plastic Stock – B/HD | 2 | 140.8 | 110 | 110 |
| 3099 | Cable Harness | 2 | 85.8 | 111 | 111 |
| 2870 | Pencil – Mech | 2 | 83.6 | 112 | 112 |
| 3208 | Pencils – Wood | 2 | 17.6 | 113 | 113 |
| 2236 | Plasma Monitor 10/TFT/XGA | 1 | 79741.2 | 114 | 114 |
| 2254 | HD 10GB /I | 1 | 37545.2 | 115 | 115 |
| 2255 | HD 12GB @7200 /SE | 1 | 23487.2 | 116 | 116 |
| 2253 | HD 10GB @5400 /SE | 1 | 11334.4 | 117 | 117 |
| 1799 | SPNIX3.3 – SL | 1 | 9614 | 118 | 118 |
| 2378 | DIMM – 128 MB | 1 | 8966.1 | 119 | 119 |
| 2415 | HD 9.1GB @7200 | 1 | 8157.6 | 120 | 120 |
| 2382 | HD 18.2GB@10000 /I | 1 | 8041 | 121 | 121 |
| 2410 | HD 8.4GB @5400 | 1 | 7368.9 | 122 | 122 |
| 2266 | DVD 12x | 1 | 6831 | 123 | 123 |
| 2400 | DIMM – 512 MB | 1 | 6688 | 124 | 124 |
| 3248 | Smart Suite – S4.0/DE | 1 | 5519.8 | 125 | 125 |
| 3091 | VRAM – 64 MB | 1 | 5282 | 126 | 126 |
| 3064 | Monitor 21/SD | 1 | 5085 | 127 | 127 |
| 3354 | HD 12GB /I | 1 | 4869 | 128 | 128 |
| 3253 | Smart Suite – S4.0/EN | 1 | 4756.4 | 129 | 129 |
| 3204 | Envoy DS | 1 | 4182 | 130 | 130 |
| 2406 | 64MB Cache /M | 1 | 3916 | 131 | 131 |
| 2402 | CD-ROM 600/E/24x | 1 | 3302 | 132 | 132 |
| 2424 | CDW 12/24 | 1 | 3267 | 133 | 133 |
| 3069 | HD 10GB /S | 1 | 3080 | 134 | 134 |
| 2414 | HD 9.1GB @10000 /I | 1 | 3072.3 | 135 | 135 |
| 3183 | Word Processing – SWS/V 4.5 | 1 | 2961 | 136 | 136 |
| 2262 | ZIP 100 | 1 | 2755 | 137 | 137 |
| 2375 | GP 1024×768 | 1 | 2336 | 138 | 138 |
| 3353 | HD 10GB /R | 1 | 2271.5 | 139 | 139 |
| 2403 | CD-ROM 600/I/24x | 1 | 2266 | 140 | 140 |
| 3090 | RAM – 48 MB | 1 | 2244 | 141 | 141 |
| 3077 | DVD 8x | 1 | 2085.6 | 142 | 142 |
| 1781 | CDW 20/48/E | 1 | 2039.4 | 143 | 143 |
| 2782 | PC Bag – C/S | 1 | 1922 | 144 | 144 |
| 2419 | Battery Backup (DA-290) | 1 | 1656 | 145 | 145 |
| 2381 | CD-ROM 8x | 1 | 1649 | 146 | 146 |
| 3258 | Web Browser – SB/V 1.0 | 1 | 1638 | 147 | 147 |
| 2377 | PS 110V HS/US | 1 | 1632 | 148 | 148 |
| 2752 | MB – S550 | 1 | 1548 | 149 | 149 |
| 3087 | DIMM – 16 MB | 1 | 1524.6 | 150 | 150 |
| 2395 | 32MB Cache /M | 1 | 1440 | 151 | 151 |
| 2638 | Envoy DS/E | 1 | 1233 | 152 | 152 |
| 3260 | Word Processing – SWP/S 4.4 | 1 | 1200 | 153 | 153 |
| 2449 | OSI 1-4/IL | 1 | 1170 | 154 | 154 |
| 3362 | Web Browser – SB/S 4.0 | 1 | 940 | 155 | 155 |
| 1791 | Industrial 700/HD | 1 | 788.7 | 156 | 156 |
| 3252 | Project Management – S3.3 | 1 | 725 | 157 | 157 |
| 1803 | SPNIX3.3 – DL | 1 | 715 | 158 | 158 |
| 2058 | Mouse +WP | 1 | 667 | 159 | 159 |
| 3256 | Spreadsheet – SSS/V 2.0 | 1 | 648 | 160 | 160 |
| 3060 | Monitor 17/HR | 1 | 590 | 161 | 161 |
| 3101 | IC Browser – V | 1 | 584 | 162 | 162 |
| 1787 | CPU D300 | 1 | 505 | 163 | 163 |
| 1782 | Compact 400/DQ | 1 | 500 | 164 | 164 |
| 3250 | Graphics – DIK | 1 | 486 | 165 | 165 |
| 2470 | SPNIX4.0 – NL | 1 | 456 | 166 | 166 |
| 3251 | Project Management – V | 1 | 416 | 167 | 167 |
| 2878 | Router – ASR/2W | 1 | 340 | 168 | 168 |
| 2093 | Pens – 10/FP | 1 | 323.4 | 169 | 169 |
| 2417 | Client ISO CP – V | 1 | 297 | 170 | 170 |
| 2594 | FG Stock – L | 1 | 243 | 171 | 171 |
| 3197 | Spreadsheet – SSS/V 2.1 | 1 | 220 | 172 | 172 |
| 2412 | 8MB EDO Memory | 1 | 190 | 173 | 173 |
| 1806 | SPNIX3.3 – UL/C | 1 | 180 | 174 | 174 |
| 2783 | Clips – Paper | 1 | 100 | 175 | 175 |
| 2380 | Cable PR/15/P | 1 | 93.5 | 176 | 176 |
| 2810 | Inkvisible Pens | 1 | 48 | 177 | 177 |
| 1912 | SS Stock – 3mm | 1 | 28 | 178 | 178 |
| 3209 | Sharpener – Pencil | 1 | 26 | 179 | 179 |
| 1825 | X25 – 1 Line License | 1 | 24 | 180 | 180 |
| 3511 | Paper – HQ Printer | 1 | 18 | 181 | 181 |
| 2725 | Machine Oil | 1 | 13.2 | 182 | 182 |
| 3515 | Lead Replacement | 1 | 4.4 | 183 | 183 |
| 3191 | Plastic Stock – O | 1 | 4.4 | 183 | 183 |
| 2808 | Paper Tablet LY 8 1/2 x 11 | 1 | 0 | 185 | 184 |
SELECT
oe.product_information.product_id,
oe.product_information.product_name,
COUNT(DISTINCT oe.order_items.order_id) AS distinct_purchases,
SUM(oe.order_items.unit_price * oe.order_items.quantity) AS total_sales,
RANK() OVER (
ORDER BY
COUNT(DISTINCT oe.order_items.order_id) DESC,
SUM(oe.order_items.unit_price * oe.order_items.quantity) DESC
) AS rank,
DENSE_RANK() OVER (
ORDER BY
COUNT(DISTINCT oe.order_items.order_id) DESC,
SUM(oe.order_items.unit_price * oe.order_items.quantity) DESC
) AS dense_rank
FROM
oe.order_items
INNER JOIN
oe.product_information
ON oe.order_items.product_id = oe.product_information.product_id
GROUP BY
oe.product_information.product_id,
oe.product_information.product_name
ORDER BY
distinct_purchases DESC, total_sales DESC;
Provide the Context
Regardless of which ranking function you use, it is critical your audience understands the ranking system’s specification (i.e., how it works). The audience should be able to quickly discern the significance or insignificance of a particular row’s value. Additionally, messages surrounding ranking outcomes should contain supporting information, if necessary. For example, it would be misleading to state a sales representative’s performance, based on total sales, is in the top 10 when there are only 10 sales representatives or there are 10 other sales representatives with the same total sales value. Be transparent.
Wrap Up
In summary, ranking functions provide us a powerful way to quickly convey relative size, importance, and position within the context of our data. ROW_NUMBER is useful for assigning values to each row based on their position within the data while RANK and DENSE_RANK allow us to implement ranking systems based on specific instructions. When using ranking functions, it is important to provide all the necessary context and information to data consumers.
Knowledge Check
If you’d like to confirm your understanding on the use of ranking functions and other fundamental concepts, consider solving a handful of my practice problems here.