Skip to content

Commit f9bb7a8

Browse files
committed
Fix #26 - Created rule G-3185 Never use ROWNUM at the same query level as ORDER BY.
1 parent 25d069f commit f9bb7a8

File tree

1 file changed

+40
-0
lines changed
  • docs/4-language-usage/3-dml-and-sql/1-general

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# G-3185: Never use ROWNUM at the same query level as ORDER BY.
2+
3+
!!! warning "Major"
4+
Reliability, Testability
5+
6+
## Reason
7+
8+
The `rownum` pseudo-column is assigned before the `order by` clause is used, so using `rownum` on the same query level as `order by` will not assign numbers in the desired ordering. Instead you should move the `order by` into an inline view and use `rownum` in the outer query.
9+
10+
## Example (bad)
11+
12+
``` sql
13+
select first_name
14+
,last_name
15+
,salary
16+
,hire_date
17+
,rownum as salary_rank
18+
from employees
19+
where rownum <= 5
20+
order by salary desc;
21+
```
22+
23+
## Example (good)
24+
25+
``` sql
26+
select first_name
27+
,last_name
28+
,salary
29+
,hire_date
30+
,rownum as salary_rank
31+
from (
32+
select first_name
33+
,last_name
34+
,salary
35+
,hire_date
36+
from employees
37+
order by salary desc
38+
)
39+
where rownum <= 5;
40+
```

0 commit comments

Comments
 (0)