SQL - ORDER_BY

김규린·2024년 8월 12일
0

Data Base

목록 보기
3/20
  • 쿼리 진행 순서
  1. From절
  2. Select
  3. 조회한 데이터 Order by로 정렬
  • 무조건 맨 마지막에 적혀야 함
  • 정렬의 기본은 오름차순
  • select문과 함께 사용하며 결과 집합을 특정 열이나 열들의 값에 따라 정렬하는데 사용함

MariaDB Ver

-- order_by: select문과 함께 사용하며 결과 집합을 특정 열이나 열들의 값에 따라 정렬하는데 사용함

-- 오름차순 ascending(asc)
SELECT
menu_code
, menu_name
, menu_price
FROM tbl_menu
ORDER BY menu_price asc; 

-- 내림차순 descending(desc)
SELECT
menu_code
, menu_name
, menu_price
FROM tbl_menu
ORDER BY menu_price desc;

 
-- 컬럼의 순번을 활용한 정렬(실제 테이블의 순서가 아닌 조회할 때 컬럼 순서 기준)
SELECT
menu_price -- 1 번째
, menu_name -- 2 번째
FROM tbl_menu
 ORDER BY 1 ASC;

-- 별칭을 활용한 정렬
SELECT
menu_price AS 'mp'
, menu_name AS 'mn'
, menu_code AS 'mc'
FROM tbl_menu
ORDER BY mp DESC; -- 별칭으로 정렬시 별칭명에  single quatation(')을 붙이면 안된다.

-- 컬럼의 복수개로 정렬
SELECT
menu_price
, menu_name
FROM tbl_menu
ORDER BY 1 DESC, 2 ASC; -- '가격'으로 내림차순 후 같은 가격일 시, '메뉴 이름'을 오름차순으로 조회
  1. null값에 대한 정렬
-- null 값에 대한 정렬
-- 1) 오름차순 시: null 값이 먼저 나옴
SELECT 
*
FROM tbl_category
ORDER BY ref_category_code asc;

-- 2) 내림차순 시: null 값이 나중에 나옴

SELECT 
*
FROM tbl_category
ORDER BY ref_category_code DESC; 

-- 3) 오름차순에서 null이 나중에 나오도록 바꿈
SELECT 
*
FROM tbl_category
ORDER BY -ref_category_code ASC; -- asc를 통해 null을 처음으로  보냄 -> -로 null이 아닌 값을 desc로 바꿈

-- 4) 내림차순에서 null이 처음에 오도록 바꿈
SELECT 
*
FROM tbl_category
ORDER BY -ref_category_code DESC; -- desc를 통해 null을 나중에 보냄 -> -로 null이 아닌 값을 asc로 바꿈

-- '-'는 null에 대한 정렬기준을 뒤집는 것
  1. Field와 ORDER_BY에 Field 대입하기

2-1. Field

  • ‘a’는 조회 대상자
  • ‘b’, ‘z’, ‘a’는 조회할 목록들
  • 조회할 목록들 중에서 조회 대상자와 같은 것에 대한 위치를 출력
  • 결과값 = 3
  • 맨 왼쪽의 값이 3번째 인자 이후의 값과 일치하면 해당 위치 값을 반환함
    (이렇게 반환된 값은 정렬 우선순위를 나타낸다.)

2-2. ORDER_BY에 Field 대입

  • 쿼리 진행 순서
  1. From절
  2. Select
  3. 조회한 데이터 Order by로 정렬
-- Field

SELECT FIELD('a', 'b', 'z', 'a'); -- 2번째 이후 인자 중에 1번째 인자의 값이 위치하는 값이 나옴(=3) / 검색해서 결과가 나오지 않으면 0이라는 값이 나옴

-- table에서 조회 시 field 활용
SELECT 
FIELD(orderable_status, 'N', 'Y')
, orderable_status
, menu_name
, menu_code
FROM tbl_menu;

-- field를 활용한 order by
SELECT
menu_name
, orderable_status 
FROM tbl_menu
ORDER BY FIELD(orderable_status, 'N', 'Y') DESC;

ORACLE Ver

Order By와 Null 정렬

-- 오름차순 ascending(asc)
SELECT
    menu_code,
    menu_name,
    menu_price
FROM tbl_menu
ORDER BY menu_price ASC;

-- 내림차순 descending(desc)
SELECT
    menu_code,
    menu_name,
    menu_price
FROM tbl_menu
ORDER BY menu_price DESC;

-- 컬럼의 순번을 활용한 정렬
SELECT
    menu_price, -- 1 번째
    menu_name  -- 2 번째
FROM tbl_menu
ORDER BY 1 ASC;

-- 별칭을 활용한 정렬
SELECT
    menu_price AS mp,
    menu_name AS mn,
    menu_code AS mc
FROM tbl_menu
ORDER BY mp DESC;

-- 컬럼의 복수개로 정렬
SELECT
    menu_price,
    menu_name
FROM tbl_menu
ORDER BY 1 DESC, 2 ASC;

-- null 값에 대한 정렬
-- 1) 오름차순 시: null 값이 먼저 나옴
SELECT 
    *
FROM tbl_category
ORDER BY ref_category_code ASC NULLS FIRST;

-- 2) 내림차순 시: null 값이 나중에 나옴
SELECT 
    *
FROM tbl_category
ORDER BY ref_category_code DESC NULLS LAST;

-- 3) 오름차순에서 null이 나중에 나오도록 바꿈
SELECT 
    *
FROM tbl_category
ORDER BY ref_category_code ASC NULLS LAST;

-- 4) 내림차순에서 null이 처음에 오도록 바꿈
SELECT 
    *
FROM tbl_category
ORDER BY ref_category_code DESC NULLS FIRST;

Oracle에서 NULL 처리 관련하여 NULLS FIRST 또는 NULLS LAST를 사용하여 NULL 값이 정렬에서 나타나는 위치를 제어할 수 있다.

FIELD와 ORDER BY
Oracle에는 MySQL의 FIELD 함수에 직접 대응되는 함수가 없지만, 대신 CASE 문을 활용하여 같은 기능을 구현할 수 있다.

-- CASE를 이용한 FIELD와 같은 기능 구현
SELECT 
    CASE orderable_status
        WHEN 'N' THEN 1
        WHEN 'Y' THEN 2
        ELSE 3
    END AS sort_order,
    orderable_status,
    menu_name,
    menu_code
FROM tbl_menu;

-- CASE를 활용한 ORDER BY
SELECT
    menu_name,
    orderable_status
FROM tbl_menu
ORDER BY 
    CASE orderable_status
        WHEN 'N' THEN 1
        WHEN 'Y' THEN 2
        ELSE 3
    END DESC;
profile
나는 할 수 있다...!

0개의 댓글