Sorting with ORDER BY · Sắp xếp với ORDER BY
Sorting the result
The rows come back in no particular order. Add ORDER BY to sort them by a column:
ASCsorts ascending (smallest first) — this is the default.DESCsorts descending (largest first).
Sắp xếp kết quả
Các dòng trả về theo thứ tự ngẫu nhiên. Thêm ORDER BY để sắp xếp chúng theo một cột:
SELECT name, score FROM student ORDER BY score DESC;
ASCsắp xếp tăng dần (nhỏ nhất trước) — đây là mặc định.DESCsắp xếp giảm dần (lớn nhất trước).
Sorting by more than one column
Sort text columns alphabetically:
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.
Sắp xếp theo nhiều cột
Sắp xếp các cột văn bản theo bảng chữ cái:
SELECT name FROM student ORDER BY name;
Bạn có thể sắp xếp theo nhiều cột: ORDER BY form ASC, score DESC sắp xếp theo lớp trước, sau đó sắp xếp theo điểm số trong từng lớp. ORDER BY luôn đứng cuối trong truy vấn.
Common mistakes
- The default sort is ascending; add
DESCfor descending. ORDER BYcomes near the end of the query.
Những lỗi thường gặp
- Thứ tự sắp xếp mặc định là tăng dần; thêm
DESCđể giảm dần. ORDER BYnằm gần cuối của truy vấn.
ORDER BY sorts the rows · ORDER BY sắp xếp các hàng
ORDER BY puts the result rows in order, like a sort. · ORDER BY đặt các hàng kết quả theo thứ tự, giống như một phép sắp xếp.
Show every student's name and score, highest score first. · Hiển thị name và score của mọi học sinh, điểm cao nhất lên đầu.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
Show every student's name, sorted alphabetically (A to Z). · Hiển thị name của mọi học sinh, sắp xếp theo bảng chữ cái (A đến Z).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.