개요

설명

SELECT 문은 지정된 테이블에서 원하는 컬럼을 조회한다.

구문

SELECT [ <qualifier> ] <select_expressions>

    [ { TO | INTO } <variable_comma_list> ]

    [ FROM <extended_table_specification_comma_list> ]

    [ WHERE <search_condition> ]

    [ GROUP BY {col_name | expr} [ ASC | DESC ],...[ WITH ROLLUP ] ]

    [ HAVING  <search_condition> ]

    [ ORDER BY {col_name | expr} [ ASC | DESC ],... [ FOR <orderby_for_condition> ] ]

    [ LIMIT [offset,] row_count ]

    [ USING INDEX { index name [,index_name,...] | NONE }]

 

<qualifier> ::= ALL | DISTINCT | DISTINCTROW | UNIQUE

 

<select_expressions> ::= * | <expression_comma_list>

 

<extended_table_specification_comma_list> ::=

<table specification> [ {, <table specification> | <join table specification> }... ]

 

<table_specification> ::=

 <single_table_spec> [ <correlation> ] [ WITH (lock_hint) ]|

 <metaclass_specification> [ <correlation> ] |

 <subquery> <correlation> |

 TABLE ( <expression> ) <correlation>

 

<correlation> ::= [ AS ] <identifier> [ ( <identifier_comma_list> ) ]

 

<single_table_spec> ::= [ ONLY ] <table_name> |

                      ALL <table_name> [ EXCEPT <table_name> ]

 

<metaclass_specification> ::= CLASS <class_name>

 

<join_table_specification> ::=

[ INNER | [ LEFT | RIGHT [ OUTER ] ] JOIN <table specification> ON <search condition>

 

lock_hint :

READ UNCOMMITTED

 

<orderby_for_condition> ::=

ORDERBY_NUM() { BETWEEN int AND int } |

    { { = | =< | < | > | >= } int } |

    IN ( int, ...)

예제 1

다음은 역대 올림픽이 개최된 국가를 중복 없이 조회한 예제이다. 이 예제는 demodb의 olympic 테이블을 대상으로 수행하였다. DISTINCT 또는 UNIQUE 키워드는 질의 결과가 유일한 값만을 갖도록 만든다. 예를 들어 host_nation 값이 'Greece'인 olympic 인스턴스가 여러 개일 때 질의 결과에는 하나의 값만 나타나도록 할 경우에 사용된다.

SELECT DISTINCT host_nation FROM olympic;

 

=== <Result of SELECT Command in Line 1> ===

  host_nation

======================

  'Australia'

  'Belgium'

  'Canada'

  'Finland'

  'France'

...

18 rows selected.

예제 2

다음은 조회하고자 하는 컬럼에 컬럼 별칭을 부여하고, ORDER BY 절에서 컬럼 별칭을 이용하여 결과 레코드를 정렬하는 예제이다. 이때, LIMIT 절 및 FOR ORDERBY_NUM 함수를 사용하여 결과 레코드 수를 5개로 제한한다.

SELECT host_year as col1, host_nation as col2 FROM olympic ORDER BY col2 LIMIT 5;

 

=== <Result of SELECT Command in Line 1> ===

 

         col1  col2

===================================

         2000  'Australia'

         1956  'Australia'

         1920  'Belgium'

         1976  'Canada'

         1948  'England'

 

5 rows selected.

 

SELECT CONCAT(host_nation, ', ', host_city) AS host_place FROM olympic

ORDER BY host_place FOR ORDERBY_NUM() BETWEEN 1 AND 5;

;xr

 

=== <Result of SELECT Command in Line 1> ===

 

  host_place

======================

  'Australia,  Melbourne'

  'Australia,  Sydney'

  'Belgium,  Antwerp'

  'Canada,  Montreal'

  'England,  London'

 

 

5 rows selected.