• Home
  • About
    • Che1's Blog photo

      Che1's Blog

      Che1's Dev Blog

    • Learn More
    • Facebook
    • Instagram
    • Github
    • Steam
    • Youtube
  • Posts
    • All Posts
    • Django
    • Python
    • Front-end
    • Algorithm
    • etc
    • All Tags
  • Projects

[SQL] SELECT

07 Oct 2017

Reading time ~3 minutes

예제 테이블

Customers
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglun Berguvsvägen 8 Luleå S-958 22 Sweden

SELECT 문은 데이터베이스에서 데이터를 선택할 때 사용한다. 결과로 리턴된 데이터는 result-set 이라는 결과 테이블에 저장된다.

SELECT 필드이름1, 필드이름2... 
FROM 테이블이름; 

아래 명령은 테이블의 모든 데이터를 가져온다.

SELECT * FROM 테이블이름; 

아래의 명령은 Customers 테이블에서 CustomerName, City 필드를 가져온다.

SELECT CustomerName, City 
FROM Customers;

Result
CustomerName City
Alfreds Futterkiste Berlin
Ana Trujillo Emparedados y helados México D.F.
Antonio Moreno Taquería México D.F.
Around the Horn London
Berglunds snabbköp Luleå

위로


DISTINCT

DISTINCT 절은 고유한 값만을 가져올 때 사용한다. 필드에 포함된 데이터들 중 동일한 값이 여러개 있을 경우 하나만 대표로 가져온다.

SELECT DISTINCT 필드이름1, 필드이름2 ...
FROM 테이블이름;

아래 명령은 Customers 테이블에서 Country 필드를 가져온다.

SELECT Country
FROM Customers;

Result
Country
Germany
Mexico
Mexico
UK
Sweden

Mexico 라는 데이터가 두 개인 것을 볼 수 있다.


아래 명령은 Customers 테이블의 Country 필드에서 고유한 값만 가져온다.

SELECT DISTINCT Country
FROM Customers;

Result
Country
Germany
Mexico
UK
Sweden

Mexico 라는 데이터가 하나만 출력되었다.


Country 필드에 고유한 값이 몇 개인지를 보려면 아래와 같이 실행한다.

SELECT COUNT(DISTINCT Country)
FROM Customers;

Result
COUNT(Distinct Country)
4

Country 필드에는 총 4개(Germany, Mexico, UK, Sweden)의 고유한 값이 있다.

위로


ORDER BY

ORDER BY 는 result-set 테이블을 주어진 필드를 기준으로 내림차순 또는 오름차순 정렬을 할 때 사용한다. 기본적으로 오름차순 정렬을 하며, DESC 를 붙이면 내림차순으로 정렬한다.

SELECT 필드이름1, 필드이름2, ...
FROM 테이블이름
ORDER BY 필드이름1, 필드이름2, ... ASC|DESC;

아래 명령은 Customers 테이블의 전체 레코드를 ContactName 를 기준으로 오름차순 정렬한다.

SELECT * 
FROM Customers
ORDER BY ContactName;

Result
CustomerID CustomerName ContactName Address City PostalCode Country
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

아래 명령은 Customers 테이블의 전체 레코드를 ContactName 을 기준으로 내림차순 정렬한다.

SELECT * 
FROM Customers
ORDER BY ContactName DESC;

Result
CustomerID CustomerName ContactName Address City PostalCode Country
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 05021 Mexico

여러 개의 필드를 기준으로 정렬할 수 있으며, 각각의 필드에 옵션을 별개로 적용할 수 있다.

SELECT 필드이름1, 필드이름2, ...
FROM 테이블이름
ORDER BY 필드이름1 ASC|DESC, 필드이름2 ASC|DESC, ...;

위로


SQL 목차
  • [SQL] JOIN, UNION
  • [SQL] DELETE
  • [SQL] UPDATE
  • [SQL] INSERT INTO
  • [SQL] SELECT를 꾸며주는 옵션 모음
  • [SQL] WHERE
  • [SQL] SELECT
  • [SQL] SQL 이란?

[SQL] SELECT 목차
  • SELECT
  • DISTINCT
  • ORDER BY
  • 참고 문헌

Reference

W3School: https://www.w3schools.com/sql/



SQL Share Tweet +1