ORDER BY

ORDER BY kalit so'zi natijalar to'plamini o'sish (ASC) yoki kamayish (DESC) tartibida saralash uchun ishlatiladi.

SU

SQL ORDER BY

ORDER BY kalit so'zi natijalar to'plamini o'sish (ASC) yoki kamayish (DESC) tartibida saralash uchun ishlatiladi.

Misol O'zingizning SQL serveringizni oling Mahsulotlarni narx bo'yicha saralang:

SELECT * FROM Products
ORDER BY Price;

Sintaksis

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;

Demo Ma'lumotlar Bazasi

Quyida misollarda ishlatiladigan Products jadvalidan tanlov keltirilgan:

ProductIDProductNameSupplierIDCategoryIDUnitPrice
1Chais1110 boxes x 20 bags18
2Chang1124 - 12 oz bottles19
3Aniseed Syrup1212 - 550 ml bottles10
4Chef Anton's Cajun Seasoning2248 - 6 oz jars22
5Chef Anton's Gumbo Mix2236 boxes21.35

DESC

ORDER BY kalit so'zi bo'yicha yozuvlar odatda o'sish tartibida saralanadi. Yozuvlarni kamayish tartibida saralash uchun DESC kalit so'zidan foydalaning.

Misol Mahsulotlarni narx bo'yicha yuqoridan pastga saralang:

SELECT * FROM Products
ORDER BY Price DESC;

Alfavit bo'yicha tartiblash

Matnli qiymatlar uchun ORDER BY kalit so'zi qiymatlarni alfavit bo'yicha tartiblaydi:

Misol Mahsulotlarni alfavit bo'yicha tartiblang:

SELECT * FROM Products
ORDER BY ProductName;

Teskari alfavit bo'yicha tartiblash

Jadvalni teskari alfavit bo'yicha tartiblash uchun DESC kalit so'zidan foydalaning:

Misol Mahsulotlarni teskari alfavit bo'yicha tartiblang:

SELECT * FROM Products
ORDER BY ProductName DESC;

Bir nechta ustun bo'yicha saralash

Quyidagi SQL buyrug'i Customers jadvalidan barcha mijozlarni saralaydi: avval Country, keyin CustomerName ustunlariga ko'ra saralaydi. Bu shuni anglatadiki, yozuvlar avval mamlakat bo'yicha tartiblanadi, agar bir xil mamlakatlar bo'lsa, ular mijoz ismi bo'yicha saralanadi:

Misol Mamlakat va mijoz ismi bo'yicha saralang:

SELECT * FROM Customers
ORDER BY Country, CustomerName;

ASC va DESC dan birgalikda foydalanish

Quyidagi SQL buyrug‘i barcha mijozlarni mamlakat bo‘yicha o‘sish tartibida, mijoz ismi bo‘yicha esa kamayish tartibida saralaydi:

Misol Mamlakat bo‘yicha o‘sish tartibida, mijoz ismi bo‘yicha kamayish tartibida saralang:

SELECT * FROM Customers
ORDER BY Country ASC, CustomerName DESC;

Ushbu sahifada

Xato haqida xabar berish