The rows come back in no particular order. Add ORDER BY to sort them by a column:
SELECT name, score FROM student ORDER BY score DESC;
ASCsorts ascending (smallest first) — this is the default.DESCsorts descending (largest first).
Sort text columns alphabetically:
SELECT name FROM student ORDER BY name;
You can sort by several columns: ORDER BY form ASC, score DESC sorts by form first, then by score within each form. ORDER BY always comes last in the query.
Question. List each student's form, name and score, grouped by form (A to Z), with the highest score first inside each form. If two students in a form tie, list them alphabetically.
Answer. Each extra column in ORDER BY only breaks ties left by the ones before it:
SELECT form, name, score FROM student
ORDER BY form, score DESC, name;
form | name | score
11A | Sara | 95
11A | Lin | 88
11A | Mei | 88
11B | Jamal | 72
11B | Tom | 64
Lin and Mei both scored 88, so the third key, name, puts Lin first.
DESCbelongs to one column. InORDER BY form, score DESConlyscoreis descending;formis still ascending.WHEREcomes beforeORDER BY.SELECT name FROM student ORDER BY score WHERE form = '11A'is a syntax error.- Without
ORDER BY, never rely on the order the rows come back in.
ORDER BY는 행을 정렬합니다
ORDER BY는 결과를 정렬된 순서로 배치하여, 마치 정렬하는 것과 같습니다.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
모든 학생의 name과 score을 최고 점수 순으로 표시하십시오. 88에서 동점인 경우, name의 알파벳 순서로 동점을 나열하십시오(두 번째 정렬 열).
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
모든 학생의 name을 표시하되, 영문자 순서(A-Z) 로 정렬하십시오.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
form, name, score를 역순으로 표시하시오 (11B 먼저), 각 형식 내에서 최저 점수를 가장 앞에 배치하고, 동점일 경우 알파벳 순서로 정렬하시오.
출력을 보려면 '실행'을 클릭하세요.
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
형식 11B의 학생에 대한 name과 score만 표시하시오, 최고 점수부터 시작합니다. WHERE이 ORDER BY보다 먼저 오게 하십시오.
출력을 보려면 '실행'을 클릭하세요.