The SQL ORDER BY #

The ORDER BY keyword is used to sort the result-set in ascending or descending order.

ORDER BY түлхүүр үг нь гарч ирэх мэдээллийг өсөх эсвэл буурах дараалалд эрэмбэлэхэд ашиглагддаг.

Example – Жишээ #

Sort the products by price:

Бүтээгдэхүүнүүдийг үнийн дагуу эрэмбэлэх: 

SELECT * FROM Products
ORDER BY Price;

Syntax – Дүрэм #

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


Demo Database – Жишээ мэдээллийн сан #

Below is a selection from the Products table used in the examples:

Доорх Бүтээгдэхүүнүүд хүснэгтийг жишээ болгож ашиглав:

ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 bags 18
2 Chang 1 1 24 – 12 oz bottles 19
3 Aniseed Syrup 1 2 12 – 550 ml bottles 10
4 Chef Anton’s Cajun Seasoning 2 2 48 – 6 oz jars 22
5 Chef Anton’s Gumbo Mix 2 2 36 boxes 21.35

DESC – Буурах эрэмбэ #

The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

ORDER BY түлхүүр үг нь өсөх дарааллаар эрэмбэлдэг. Мэдээллийг буурах дарааллаар эрэмбэлэхийн тулд DESC түлхүүр үгийг ашиглана.

Example – Жишээ #

Sort the products from highest to lowest price:

Бүтээгдэхүүнүүдийг ихээс бага луу эрэмбэлэх:

SELECT * FROM Products
ORDER BY Price DESC;

Order Alphabetically – Цагаан толгойн дарааллаар харах #

For string values the ORDER BY keyword will order alphabetically:

ORDER BY түлхүүр үг ашиглаж тэмдэгт мэдээллүүдийг цагаан толгойгоор харах:

Example – Жишээ #

Sort the products alphabetically by ProductName:

ProductName(БүтээгдэхүүнийНэр): цагаан толгойн дарааллаар эрэмбэлэх:

SELECT * FROM Products
ORDER BY ProductName;

Alphabetically DESC – Цагаан толгойн ЭСРЭГ дараалал #

To sort the table reverse alphabetically, use the DESC keyword:

Хүснэгтийг цагаан толгойн эсрэг дарааллаар эрэмбэлэхийн тулд DESC түлхүүр үгийг ашиглана:

Example – Жишээ #

Sort the products by ProductName in reverse order:

ProductName(БүтээгдэхүүнийНэр): цагаан толгойн эсрэг дарааллаар эрэмбэлэх:

SELECT * FROM Products
ORDER BY ProductName DESC;

ORDER BY Several Columns – Олон баганаар эрэмбэлэх #

The following SQL statement selects all customers from the “Customers” table, sorted by the “Country” and the “CustomerName” column. This means that it orders by Country, but if some rows have the same Country, it orders them by CustomerName:

Доорх жишээ нь бүх “Customers” хүснэгтийн мэдээлэл гаргахаас гадна, “Country” болон “CustomerName” баганын мэдээллээр эрэмбэлнэ. Энэ юу гэсэн үг вэ гэхээр Country(Улс)-аар эрэмбэлнэ, гэхдээ Country мөр доторх утга ижил байвал, CustomerName(ХэрэглэгчийнНэр)-ээр дахин эрэмбэлнэ.

Example – Жишээ #

SELECT * FROM Customers
ORDER BY Country, CustomerName;

Using Both ASC and DESC – Өсөх болон буурах эрэмбэ хамт ашиглах #

The following SQL statement selects all customers from the “Customers” table, sorted ascending by the “Country” and descending by the “CustomerName” column:

Доорх SQL жишээ нь “Хэрэглэгчид(Customers)” хүснэгтээс бүх хэрэглэгчдийг сонгон авч, “Улс(Country)” өсөх дарааллаар, “ХэрэглэгчийнНэр(CustomName)” буурах дарааллаар багануудыг эрэмбэлнэ:  

Example #

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

Powered by BetterDocs

Leave a Reply