■ はじめに
SQL の ORDER BY句 について、メモ。
目次
【1】ORDER BY句 【2】使用上の注意 1)パフォーマンスが悪くなる 2)NULLが入った時の挙動 【3】構文 1)並び順の指定 2)NULLS { FIRST | LAST } 【4】サンプル
【1】ORDER BY句
* 取り出した結果を並べ替える * ORDER BY句がないと、出力順は保障されないので注意。
【2】使用上の注意
1)パフォーマンスが悪くなる
* 使う必要がない場合、むやみに使わないようにすること
2)NULLが入った時の挙動
* なにも指定しないと、NULL値は最大値OR最小値として扱われる => DBによって異なるっぽい
https://qiita.com/SVC34/items/c23341c79325a0a95979
【3】構文
SELECT 【フィールド名】 FROM 【テーブル名】 ORDER BY 【フィールド名】 [ASC, DESC]
1)並び順の指定
昇順:ASC
* 小さいもの順 (Ascendant:昇る) * 指定がない場合、ASC
降順:DESC
* 大きいもの順 (desendant:降りる)
2)NULLS { FIRST | LAST }
* NULL値を非NULL値の前にするか後にするかを決定する * DB によって非対応なDBがあるらしい
https://qiita.com/SVC34/items/c23341c79325a0a95979
=> MySQL だと未サポートで独自の対応があるっぽい
https://qiita.com/koheiiwamura/items/6b6be9fae801f5a49eae
【4】サンプル
* 試すDBがなければ、以下のサイトで行うといいかも、、、 * PostgreSQL15を想定
SQL Fiddle
https://www.db-fiddle.com/
1)使用データ
CREATE TABLE IF NOT EXISTS sample_table ( id INT, name VARCHAR(50), age INT ); INSERT INTO sample_table (id,name, age) VALUES (1,'Mike',46), (2,'Sam', 19), (3,'Kevin', 28), (4,'John', NULL), (5,'Ken', 38) ;
2)サンプル
SELECT * FROM sample_table ORDER BY id; SELECT * FROM sample_table ORDER BY id ASC; SELECT * FROM sample_table ORDER BY id DESC; SELECT * FROM sample_table ORDER BY age; SELECT * FROM sample_table ORDER BY age ASC; SELECT * FROM sample_table ORDER BY age DESC; SELECT * FROM sample_table ORDER BY age ASC NULLS FIRST; SELECT * FROM sample_table ORDER BY age DESC NULLS FIRST; SELECT * FROM sample_table ORDER BY age ASC NULLS LAST; SELECT * FROM sample_table ORDER BY age DESC NULLS LAST;
SELECT * FROM sample_table ORDER BY id;
id | name | age |
---|---|---|
1 | Mike | 46 |
2 | Sam | 19 |
3 | Kevin | 28 |
4 | John | |
5 | Ken | 38 |
SELECT * FROM sample_table ORDER BY id ASC;
id | name | age |
---|---|---|
1 | Mike | 46 |
2 | Sam | 19 |
3 | Kevin | 28 |
4 | John | |
5 | Ken | 38 |
SELECT * FROM sample_table ORDER BY id DESC;
id | name | age |
---|---|---|
5 | Ken | 38 |
4 | John | |
3 | Kevin | 28 |
2 | Sam | 19 |
1 | Mike | 46 |
SELECT * FROM sample_table ORDER BY age;
id | name | age |
---|---|---|
2 | Sam | 19 |
3 | Kevin | 28 |
5 | Ken | 38 |
1 | Mike | 46 |
4 | John |
SELECT * FROM sample_table ORDER BY age ASC;
id | name | age |
---|---|---|
2 | Sam | 19 |
3 | Kevin | 28 |
5 | Ken | 38 |
1 | Mike | 46 |
4 | John |
SELECT * FROM sample_table ORDER BY age DESC;
id | name | age |
---|---|---|
4 | John | |
1 | Mike | 46 |
5 | Ken | 38 |
3 | Kevin | 28 |
2 | Sam | 19 |
SELECT * FROM sample_table ORDER BY age ASC NULLS FIRST;
id | name | age |
---|---|---|
4 | John | |
2 | Sam | 19 |
3 | Kevin | 28 |
5 | Ken | 38 |
1 | Mike | 46 |
SELECT * FROM sample_table ORDER BY age DESC NULLS FIRST;
id | name | age |
---|---|---|
4 | John | |
1 | Mike | 46 |
5 | Ken | 38 |
3 | Kevin | 28 |
2 | Sam | 19 |
SELECT * FROM sample_table ORDER BY age ASC NULLS LAST;
id | name | age |
---|---|---|
2 | Sam | 19 |
3 | Kevin | 28 |
5 | Ken | 38 |
1 | Mike | 46 |
4 | John |
SELECT * FROM sample_table ORDER BY age DESC NULLS LAST;
id | name | age |
---|---|---|
1 | Mike | 46 |
5 | Ken | 38 |
3 | Kevin | 28 |
2 | Sam | 19 |
4 | John |
関連記事
SQLを書くコツ
https://dk521123.hatenablog.com/entry/2016/01/11/173055
SQL ~ BETWEEN句 ~
https://dk521123.hatenablog.com/entry/2010/08/06/111640
SQL ~ HAVING句 ~
https://dk521123.hatenablog.com/entry/2010/07/13/154947
SQL ~ IN句 ~
https://dk521123.hatenablog.com/entry/2024/12/01/000000
SQL ~ ORDER BY句 ~
https://dk521123.hatenablog.com/entry/2010/03/18/220719
SQL ~ WITH句 / 共通テーブル式 ~
https://dk521123.hatenablog.com/entry/2012/07/26/013620
Window関数 ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2023/02/22/000000
Window関数 ~ ROW_NUMBER + PARTITION BY ~
https://dk521123.hatenablog.com/entry/2025/04/08/113011
Window関数 ~ RANK / DENSE_RANK + PARTITION BY ~
https://dk521123.hatenablog.com/entry/2021/03/11/210937
Window関数 ~ LAG / LEAD ~
https://dk521123.hatenablog.com/entry/2021/09/10/092850
Window関数 ~ RANK / DENSE_RANK ~
https://dk521123.hatenablog.com/entry/2012/08/15/225233