§ Oracle Compatibility - Syntax - Oracle ORDER BY Compatibility
In GreatSQL, after switching to ORACLE mode, Oracle-style sort rules are supported.
§ 1. Syntax
ORDER BY order_expr [, order_expr...]
order_expr :
expr [ ASC | DESC ] [ NULLS FIRST | NULLS LAST ]
1
2
3
4
2
3
4
§ 2. Oracle Compatibility Notes
GreatSQL and Oracle differ in the way NULL values are handled when sorting with ORDER BY.
GreatSQL behavior:
- When sorting by
ASC,NULLvalues are sorted first; - When sorting by
DESC,NULLvalues are sorted last.
Oracle behavior is the opposite of GreatSQL, that is:
- When sorting by
ASC,NULLvalues are sorted last; - When sorting by
DESC,NULLvalues are sorted first.
You can set SET sql_mode = ORACLE to switch to ORACLE mode and choose Oracle's sort mode.
In ORACLE mode, you can also add NULLS FIRST to explicitly place NULL values first in the final result when sorting.
You can also add NULLS LAST to explicitly place NULL values last in the final result.
§ 3. Examples
greatsql> SET sql_mode = DEFAULT;
greatsql> CREATE TABLE t1 (id INT, desc1 VARCHAR(2), desc2 VARCHAR(2));
greatsql> INSERT INTO t1 VALUES (1, NULL, 'A'), (2, 'B', 'B'), (3, 'C', 'C'), (4, 'D', 'D');
greatsql> SELECT * FROM t1;
+------+-------+-------+
| id | desc1 | desc2 |
+------+-------+-------+
| 1 | NULL | A |
| 2 | B | B |
| 3 | C | C |
| 4 | D | D |
+------+-------+-------+
greatsql> SELECT * FROM t1 ORDER BY desc1;
+------+-------+-------+
| id | desc1 | desc2 |
+------+-------+-------+
| 1 | NULL | A |
| 2 | B | B |
| 3 | C | C |
| 4 | D | D |
+------+-------+-------+
greatsql> SELECT * FROM t1 ORDER BY desc1 DESC;
+------+-------+-------+
| id | desc1 | desc2 |
+------+-------+-------+
| 4 | D | D |
| 3 | C | C |
| 2 | B | B |
| 1 | NULL | A |
+------+-------+-------+
greatsql> SELECT * FROM t1 ORDER BY desc1 NULLS FIRST;
+------+-------+-------+
| id | desc1 | desc2 |
+------+-------+-------+
| 1 | NULL | A |
| 2 | B | B |
| 3 | C | C |
| 4 | D | D |
+------+-------+-------+
greatsql> SELECT * FROM t1 ORDER BY desc1 NULLS LAST;
+--------+-------+-------+
| id | desc1 | desc2 |
+--------+-------+-------+
| 2 | B | B |
| 3 | C | C |
| 4 | D | D |
| 1 | NULL | A |
+--------+-------+-------+
greatsql> SELECT * FROM t1 ORDER BY desc1 DESC NULLS FIRST;
+------+-------+-------+
| id | desc1 | desc2 |
+------+-------+-------+
| 1 | NULL | A |
| 4 | D | D |
| 3 | C | C |
| 2 | B | B |
+------+-------+-------+
greatsql> SELECT * FROM t1 ORDER BY desc1 DESC NULLS LAST;
+------+-------+-------+
| id | desc1 | desc2 |
+------+-------+-------+
| 4 | D | D |
| 3 | C | C |
| 2 | B | B |
| 1 | NULL | A |
+------+-------+-------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
Scan the QR code to follow our WeChat official account
