본문 바로가기
번역/MariaDB

[MariaDB Tutorial 번역] 2. Order By

by 촌쥐 2021. 6. 24.

[MariaDB Tutorial 번역 글]

  1. Select : https://rural-mouse.tistory.com/43
  2. Order By ←←
  3. Where : https://rural-mouse.tistory.com/45
  4. Distinct : https://rural-mouse.tistory.com/46
  5. Between : https://rural-mouse.tistory.com/48
  6. Like : https://rural-mouse.tistory.com/49
  7. In : https://rural-mouse.tistory.com/50
  8. Limit : https://rural-mouse.tistory.com/51
  9. Is Null : https://rural-mouse.tistory.com/52
  10. Joins : https://rural-mouse.tistory.com/53
  11. Inner Join : https://rural-mouse.tistory.com/54
  12. Left Join : https://rural-mouse.tistory.com/55
  13. Group By : https://rural-mouse.tistory.com/56
  14. Having : https://rural-mouse.tistory.com/59
  15. Subqueries : https://rural-mouse.tistory.com/60
  16. Insert : https://rural-mouse.tistory.com/61
  17. Insert Multiple Rows : https://rural-mouse.tistory.com/62
  18. Insert Into Select : https://rural-mouse.tistory.com/63
  19. Update : https://rural-mouse.tistory.com/64

원문 :  https://www.mariadbtutorial.com/mariadb-basics/mariadb-order-by/

 

MariaDB Order By

In this tutorial, you will learn how to use the MariaDB order by clause to sort a result set by values in one or more columns in ascending or descending order.

www.mariadbtutorial.com


여기에 있는 모든 예제는 MariaDB sample database인 nation을 가지고 진행됩니다.

만약 읽고 따라 해 보시는 게 목적이라면 다운로드를 받고 읽어보시길 추천합니다.

원본 링크 : https://www.mariadbtutorial.com/getting-started/mariadb-sample-database/

 

MariaDB Sample Database

In this tutorial, you will learn about a MariaDB sample database nation and how to load the sample database into the MariaDB server.

www.mariadbtutorial.com


MariaDB Order By 명령에 대한 소개

Select 명령은 각 행에 대해 특별한 정렬이 없이 Result Set을 반환합니다. Result Set의 하나 혹은 여러 Column들의 행을 값에 따라 정렬하기 위해서는 Order By를 사용합니다.

다음은 Select 명령에 Order By를 사용한 문법을 보여줍니다.

select 
    select_list
from 
    table_name
order by
    sort_expression1 [asc | desc],
    sort_expression2 [asc | desc],
    ...;

Order BySelect 명령 끝부분에서 나타납니다. 

  • 첫 번째로, 하나 혹은 그 이상의 표현식 sort_expression1, sort_expression2... 을 지정합니다. 이는 정렬하고 싶은 Column 또는 표현식이 될 수 있습니다. 
  • 그리고 asc 혹은 desc를 사용하여 Result Set의 행을 정렬합니다. asc는 오름차순, desc는 내림차순을 나타냅니다.

Order By 절의 기본은 asc가 사용됩니다.

MariaDB는 Select 구문에 대한 계산을 할 때 From, Select, Order By순으로 계산됩니다. 

쿼리에 대한 Evaluate 순서

 

MariaDB의 Order By 구문에 대한 예제

우리는 Sample db인 nation에 있는 countries Table을 가지고 시행해보겠습니다. 

Sample table

A) MariaDB Order By 명령을 이용(query)하여 하나의 Column으로 행을 정렬하는 예제

다음 select 명령은 countries에서 namesarea를 반환합니다.  

select 
    name, 
    area
from 
    countries
order by name;

결과의 일부

보이듯이 Result Set의 행들은 특별한 순서가 없습니다.

다음 Select 구문은 countriesname으로 정렬하기 위해 Order By 절을 사용합니다.

select 
    name, 
    area
from 
    countries
order by 
    name;

name 순으로 정렬된 결과

Order By 절은 기본 옵션인 asc를 사용하여 countriesname의 오름차순으로 정렬하였습니다.

countries를 name의 내림차순으로 정렬하기 위해서는 desc옵션을 사용합니다.

select 
    name, 
    area
from 
    countries
order by 
    name desc;

내림차순으로 정렬된 결과

B) MariaDB Order By 구문을 이용하여 두 개의 Column으로 행을 정렬하는 예제

다음 Order By 구문은 countries를 정렬하기 위하여 region_idname을 사용합니다.

select 
    name, 
    region_id
from 
    countries
order by 
    region_id, 
    name;

정렬된 결과

  • 첫 번째로 region_id의 값을 토대로 오름차순으로 countries를 정렬합니다.
  • 그다음, 한번 정렬된 countriesname의 알파벳순으로 정렬합니다.

MariaDB Order By 구문의 null 값

MariaDB에서 null 값은 특별한 값입니다. null은 해당하는 값이 적용되지 않았거나 사라졌다는 것을 보여줍니다.

MariaDB는 null을 가장 낮은 값으로 처리하므로 null 값을 가지고 있는 column을 기준으로 정렬하면 Result Set에 null 값이 먼저 표시됩니다.

다음은 Order By를 사용하여 national_dayscountries를 정렬합니다.

select 
    name, 
    national_day
from 
    countries
order by 
    national_day;

null 값이 포함된 column의 정렬

desc 옵션을 사용한 경우 null 값은 가장 마지막에 보일 것입니다.

select 
    name, 
    national_day
from 
    countries
order by 
    national_day desc;

desc 옵션 적용한 result set

 

이번 튜토리얼에서는 MariaDB의 Order By를 사용하여 하나 혹은 여러개의 Column의 값을 기준으로 오름차순 혹은 내림차순으로 정렬하는 방법을 배웠습니다.

다음에는 조건에 따른 행을 보여주는 Where에 대해 배워보겠습니다. 

 

'번역 > MariaDB' 카테고리의 다른 글

[MariaDB Tutorial 번역] 6. Like  (0) 2021.06.29
[MariaDB Tutorial 번역] 5. Between  (0) 2021.06.27
[MariaDB Tutorial 번역] 4. Distinct  (0) 2021.06.27
[MariaDB Tutorial 번역] 3. Where  (0) 2021.06.24
[MariaDB Tutorial 번역] 1. Select  (0) 2021.06.23

댓글