728x90

ORDER BY 절

order by절을 사용하여 정렬하기

select first_name
        ,salary
from employees
order by salary desc; 

-- desc 내림차순
-- asc 오름차순

select first_name
        ,salary
from employees
where salary >= 9000
order by salary desc; 
  • 기본 값 오름차순(작은 거 → 큰 거)
    • 한글 : 가, 나, 다, 라
    • 영어 : A, B, C D
    • 숫자 : 1, 2, 3, 4
    • 날짜 : 예전 날짜에서 최근 날짜로 정렬
  • 정렬 조건이 복수일 때는 ‘,’(콤마)로 구분하여 나열한다.
-- 부서번호를 오름차순으로 정렬하고 부서번호, 급여, 이름을 출력하세요
select department_id
        ,salary
        ,first_name
from employees
order by department_id asc;

-- 급여가 10000 이상인 직원의 이름 급여를 급여가 큰직원부터 출력하세요
select first_name
        ,salary
from employees
where salary >= 10000
order by salary desc;

-- 부서번호를 오름차순으로 정렬하고 부서번호가 같으면 
-- 급여가 높은 사람부터 부서번호 급여 이름을 출력하세요

select  department_id
        ,salary
        ,first_name
from employees
order by department_id asc, salary desc;
-- (정렬 조건이 복수) 좌측부터 우선 순위 적용
반응형
코드플리