강좌
클라우드/리눅스에 관한 강좌입니다.
데이터베이스 분류

기본 SQL문 - SELECT

작성자 정보

  • 웹관리자 작성
  • 작성일

컨텐츠 정보

본문

sub_title_icon.gif 기본 SQL문 - SELECT
1. SELECT
SELECT 절은절지정된 테이블에서 조회를 원하는 컬럼을 지정한다.
1) 구문
SELECT [ qualifie ] select_expression [ { TO | INTO }
variable [ {, variable }_ ] ]

qualifier :
ALL
DISTINCT
UNIQUE

select_expression :
*
table_name. *
expression [ {, expression}...]

variable :
[:] identifier
icon_sub02.gifqualifier : 한정어. 생략이 가능하며 지정하지 않을 경우에는 ALL로 지정된다.
- ALL : 테이블의 모든 인스턴스를 조회한다.
- DISTINCT : 중복을 허용하지 않고 유일한 값을 갖는 인스턴스에 대해서만 조회한다.
- UNIQUE : DISTINCT와 동일하게 중복을 허용하지 않고 유일한 값을 갖는 인스턴스에 대해서만 조회한다.
icon_sub02.gifselect_expression :
- * : SELECT * 구문을 사용하면 FROM 절에서 명시한 테이블에 대한 모든 컬럼을 조회할 수 있다.
- table_name. * : 테이블 이름을 지정하고 *을 사용하면 명시한 테이블에 대한 모든 컬럼을 지정하는 것과 같다.
- expression [ {, expression}...] : expression은 컬럼 이름이나 경로 표현식, 변수, 테이블 이름이 될 수 있으며 산술 연산을 포함하는 일반적인 표현식도 모두 사용될 수 있다. 쉼표(,)는 리스트에서 개별 표현식을 구분하는데 사용된다.
또한, expression에는 SELECT SUM(gold)과 같이 조회된 데이터를 조작하는 집계 함수가 사용될 수 있다. 만약 expression으로 집계 함수가 사용될 때 GROUP BY 절과 함께 사용하지 않는다면 조회 리스트에 있는 모든 요소가 집계 함수가 되어야 한다.
- UNIQUE : DISTINCT와 동일하게 중복을 허용하지 않고 유일한 값을 갖는 인스턴스에 대해서만 조회한다.
icon_sub02.gifvariable : select_expression이 조회하는 데이터는 하나 이상의 변수에 저장될 수 있다.
- [:]identifier : TO(또는 INTO) 다음에 :식별자를 쓰면 조회하는 데이터를 ':식별자'의 변수에 저장할 수 있다.
2) 예제
icon_sub02.gif 다음은 역대 올림픽의 모든 정보를 조회하는 예제이다. 컬럼 전체를 조회하기 위해서는 컬럼 이름 리스트 대신 별표(*)가 사용될 수 있다. 조회 결과는 테이블 내에서 정의된 순서에 따라 컬럼 값이 표시된다.
csql> SELECT * FROM olympic;
csql> ;x

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

host_year   host_nation   host_city   opening_date   closing_date   mascot   slogan   introduction
==================================================================================================
2004   'Greece'   'Athens'   08/13/2004   08/29/2004   'Athena Phevos'   'Welcome Home'   'In 2004 the Olympic Games returned to Greece, the home of both the ancient Olympics and the first modern Olympics. For the first time ever a record 201 National Olympic Committees (NOCs) participated in the Olympic Games. The overall tally for events on the programme was 301 (one more than in Sydney 2000). Popularity in the Games soared to new highs as 3.9 billion people had access to the television coverage compared to 3.6 billion for Sydney 2000. Women''s wrestling was included in the program for the first time. Swimmer Michael Phelps won 6 gold medals and set a single-Games record with 8 total medals. Leontien Ziljaard-van Moorsel became the first female cyclist to earn 4 career gold medals and 6 total medals, while canoeist Birgit Fischer became the first athlete in any sport to win two medals in each of 5 Olympics. Runner Hicham El Guerrouj won both the 1,500m and the 5,000m, while on the women''s side Kelly Holmes triumphed in both the 800m and the 1,500m. In team play, Argentina won the men''s football tournament without giving up a goal, and the U.S. softball team won by outscoring their opponents 51-1.'
2000   'Australia'   'Sydney'   09/15/2000   10/01/2000   'Olly Syd Millie'   'Share the Spirit'   'The Sydney 2000 Games are the biggest to date with a total of 199 nations and more than 10,500 athletes participating. IOC President Juan Antonio Samaranch announced that the Games of the XXII Olympiad were the best Olympics ever held.'
1996   'USA'   'Atlanta'   07/19/1996   08/09/1996   'Izzy'   'The Celebration of the Century'   'The 1996 Atlanta Games celebrated 100 years of the Modern Olympic Games. In 100 years the Games had grown from thirteen to 197 competing nations, from 300 male athletes to more than 10,000 male and female athletes and from 43 to 271 events. During these Games a terrorist bomb at a concert killed one person and injured more than 100 people.'
1992   'Spain'   'Barcelona'   07/25/1992   08/09/1992   'Cobi'   'Friends Forever'   'For the first time in decades, no nations boycotted the 1992 Barcelona Games. The Olympic Truce was recited for the first time with 169 countries and nearly 10,000 athletes participating in the Olympics.'
1988   'Korea'   'Seoul'   09/17/1988   10/02/1988   'HODORI'   'Harmony and progress'   'The 1988 Seoul Games were the first Olympics to allow professional athletes to compete in certain events. The IOC refused to give North Korea the title of co-hosts so they boycotted the Games. This resulted in a couple of other nations boycotting the event in support of North Korea.'
1984   'USA'   'Los Angeles'   07/28/1984   08/12/1984   'Sam'   'Play part in History'   'For the first time since 1952, China sent athletes to the Games. The Soviet Union and some of its allies withdrew their athletes citing that security was not good enough. 140 countries competed at the 1984 Los Angeles Games with over 1500 women, 5000 men and nearly 10,000 media attending the Games.'
1980   'USSR'   'Moscow'   07/19/1980   08/03/1980   'Misha'   ''   'These were the first Games to be held in a socialist country and due to the 1979 Soviet invasion of Afghanistan, dozens of nations boycotted the 1980 Moscow Games. The number of competing nations had not been so low since the 1956 Melbourne Games. That said, there were more than 1000 women and 4000 men competing at these Games.'
1976   'Canada'   'Montreal'   07/17/1976   08/01/1976   'Amik'   ''   'Due to an earlier tour of apartheid South Africa by a New Zealand rugby union team, the 1976 Montreal Games were plagued with African countries boycotting the competition due to New Zealand's Olympic presence. Taiwan also boycotted as it was not acknowledged as part of the Republic of China bt the IOC.'
······
25 rows selected.
Current transaction has been committed.
1 command(s) successfully processed.
icon_sub02.gif 다음은 국가의 모든 정보를 조회하는 예제이다. 컬럼 이름 사용하여 필요한 항목만 조회 할 수 있다.
csql> SELECT code, name, continent, capital FROM nation;
csql> ;x

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

code   name   continent   capital
===================================================================
'SRB'   'Serbia'   'Europe'   'Beograd'
'KIR'   'Kiribati'   'Oceania'   'Tarawa'
'SCG'   'Serbia and Montenegro'   'Europe'   'Beograd'
'TLS'   'Timor-Leste'   'Asia'   'Dili'
'ZIM'   'Zimbabwe'   'Africa'   'Harare'
'ZAM'   'Zambia'   'Africa'   'Lusaka'
'ZAI'   'Zaire'   'Africa'   'Kinshasa'
'YUG'   'Yugoslavia'   'Europe'   'Belgrade'
'YMD'   'Yemen Democratic Republic'   'Asia'   'Sana'a'
'YEM'   'Yemen'   'Asia'   'Sana'a'
'YAR'   'Yemen Arabian Republic'   'Asia'   'Sana'a'
'VIN'   'Saint Vincent & the Grenadines'   'Americas'   'Kingstown'
'VIE'   'Vietnam'   'Asia'   'Hanoi'
'VEN'   'Venezuela'   'Americas'   'Caracas'
······
'KOR'   'Korea'   'Asia'   'Seoul'
······
215 rows selected.
Current transaction has been committed.
1 command(s) successfully processed.

관련자료

댓글 0
등록된 댓글이 없습니다.

공지사항


뉴스광장


  • 현재 회원수 :  60,037 명
  • 현재 강좌수 :  35,810 개
  • 현재 접속자 :  87 명