Sort Results Part 2
Coddy SQL 여정의 기초 섹션에 포함된 레슨 — 72개 중 14번째.
SQL에서 ORDER BY를 사용할 때 여러 열을 기준으로 데이터를 정렬할 수 있습니다.
예를 들어 다음 competition 테이블을 고려해 보세요:
| runner_id | age | avg_speed (m/s) |
|---|---|---|
| 1 | 47 | 3.65 |
| 2 | 62 | 3.07 |
| 3 | 57 | 6.82 |
| 4 | 56 | 4.34 |
| 5 | 25 | 4.93 |
| 6 | 40 | 3.94 |
| 7 | 23 | 6.58 |
| 8 | 40 | 3.43 |
SELECT *
FROM competition
WHERE age < 50
ORDER BY age DESC, avg_speed DESC이 쿼리는 다음과 같이 동작합니다:
- 먼저 모든 레코드를 `age` 기준으로 내림차순(가장 높은 값에서 가장 낮은 값 순) 정렬합니다.
- 두 개 이상의 레코드가 동일한 `age` 값을 가지는 경우, 해당 레코드들을 `avg_speed` 기준으로 내림차순(가장 높은 값에서 가장 낮은 값 순) 정렬합니다.
결과:
| runner_id | age | avg_speed (m/s) |
|---|---|---|
| 1 | 47 | 3.65 |
| 6 | 40 | 3.94 |
| 8 | 40 | 3.43 |
| 5 | 25 | 4.93 |
| 7 | 23 | 6.58 |
챌린지
쉬움사용 가능한 테이블 및 열:
feathers:id,weight,area
내림차순으로 area를 기준으로 정렬한 후 모든 id를 반환하세요. feathers의 area가 동일한 경우, 오름차순으로 weight를 기준으로 정렬하세요.
직접 해보기
이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.
기초의 모든 레슨
4More Keywords
The IN keywordThe BETWEEN keywordThe LIKE keywordThe AS keywordRecap - Cellphone Models2Conditions
Conditions BasicsThe AND keywordThe OR keywordThe NOT keywordMultiple Conditions CombinedParenthesisBooleans5Arithmetic Operations
Mathematical OperatorsMathematical ColumnsThe Modulo OperationThe ROUND() Function3Specific Return Format
Null valuesSort Results Part 1Sort Results Part 2Recap - Cyber Security FirmLimit number of recordsRecap - Vehicle Factory6Intro Challenges
Recap - Parliamentary ElectionRecap - Police Criminal ArrestRecap - Bar Beverage ContainerRecap - Engineer new columns