SQLのdescについてまとめています。
目次
SQLのdescは、order byソートの並び替えの降順指定

以下はMySQLの例です。employeesは、MySQL用のサンプルデータベースです。employees(社員)テーブルから、hire_date(雇用日付)が新しい社員のデータを降順に10件取得しています。
mysql> select * from employees order by hire_date desc limit 10; +--------+------------+------------+------------+--------+------------+ | emp_no | birth_date | first_name | last_name | gender | hire_date | +--------+------------+------------+------------+--------+------------+ | 463807 | 1964-06-12 | Bikash | Covnot | M | 2000-01-28 | | 428377 | 1957-05-09 | Yucai | Gerlach | M | 2000-01-23 | | 499553 | 1954-05-06 | Hideyuki | Delgrande | F | 2000-01-22 | | 222965 | 1959-08-07 | Volkmar | Perko | F | 2000-01-13 | | 47291 | 1960-09-09 | Ulf | Flexer | M | 2000-01-12 | | 422990 | 1953-04-09 | Jaana | Verspoor | F | 2000-01-11 | | 227544 | 1954-11-17 | Shahab | Demeyer | M | 2000-01-08 | | 205048 | 1960-09-12 | Ennio | Alblas | F | 2000-01-06 | | 226633 | 1958-06-10 | Xuejun | Benzmuller | F | 2000-01-04 | | 424445 | 1953-04-27 | Jeong | Boreale | M | 2000-01-03 | +--------+------------+------------+------------+--------+------------+
【関連記事】
▶SQL order byでソート指定するサンプルコード集 指定のレコードだけ先頭に並べるには?
一部のデータベースでは、descはテーブルの定義情報を調べるSQLコマンド
同じdescでも、一部のデータベースではテーブル定義情報を調べるためのコマンドとして使われています。以下は、employees(社員)テーブルのカラム名、データ型、null許可、デフォルト値、キーなどを表示します。
mysql> desc employees;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| emp_no | int | NO | PRI | NULL | |
| birth_date | date | NO | | NULL | |
| first_name | varchar(14) | NO | | NULL | |
| last_name | varchar(16) | NO | | NULL | |
| gender | enum('M','F') | NO | | NULL | |
| hire_date | date | NO | | NULL | |
+------------+---------------+------+-----+---------+-------+
この場合、descはdescribeの省略系として使われています。
Oracleでのdesc実行例
select * from SCOTT.DEPT order by LOC desc; ---------- DEPTNO DNAME LOC 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 30 SALES CHICAGO 40 OPERATIONS BOSTON
OracleでもMySQL同様、テーブルのカラム詳細情報を表示するためにdescが用意されています。
SQL Serverでのdesc使用例
以下は、SQL Serverにテーブルを作成して、productNameを降順にソートして全件取得する例です。
CREATE TABLE ForgeRock
([productName] varchar(13), [description] varchar(57))
;
INSERT INTO ForgeRock
([productName], [description])
VALUES
('OpenIDM', 'Platform for building enterprise provisioning solutions'),
('OpenAM', 'Full-featured access management'),
('OpenDJ', 'Robust LDAP server for Java')
;
select * from ForgeRock order by productName desc;
productName description
----------------------------
OpenIDM Platform for building enterprise provisioning solutions
OpenDJ Robust LDAP server for Java
OpenAM Full-featured access management
PostgreSQLのdesc使用例
以下のSQLは、testtableテーブルを作成し、4件のデータをinsertしたあと、nameカラムを降順にソートして取得する例です。varchar型のdesc指定は、辞書順序の降順になります。
create table testtable (
id integer primary key,
name varchar(10)
);
insert into testtable (id, name ) values (1,'a');
insert into testtable (id, name ) values (2,'cat');
insert into testtable (id, name ) values (3,'apple');
insert into testtable (id, name ) values (4,'doc');
select * from testtable order by name desc;
id name
----------
4 doc
2 cat
3 apple
1 a
まとめ


- descは、order byのソートの降順指定をおこなう
- データベースによっては、desc テーブル名で、テーブルの詳細表示
- 文字型のdesc指定は、辞書順序の降順指定となる