The ORDER BY keyword is used to sort the values in Ascending [ASC] or Descending [DESC] order. The default sorting in the ORDER BY keyword is in Ascending Order. To sort it in descending order, we need to add [DESC] with the keyword. With the keyword, we can add multiple columns
- Syntax of ORDER BY keyword –
SELECT column1,column2,column3,...,columnN FROM table_name
ORDER BY column1,column2,....ASC|DESC;
Here in the syntax, we have Order By with multiple columns and the sorting can be done either in Ascending or Descending Order. To manage the sorting, it needed to add the keyword for ascending and descending.
Demo table –
Product Table
Category | Region | City | Country | Customer_ID | Customer_name |
Office Supplies | Central | Chicago | United States | SM-20950 | Suzanne McNair |
Technology | East | New York City | United States | AH-10465 | Amy Hunt |
Office Supplies | East | New York City | United States | AH-10465 | Amy Hunt |
Office Supplies | East | Dover | United States | EP-13915 | Emily Phan |
Office Supplies | East | Dover | United States | EP-13915 | Emily Phan |
Technology | West | Aurora | United States | TP-21565 | Tracy Poddar |
Office Supplies | West | Aurora | United States | TP-21565 | Tracy Poddar |
Office Supplies | East | Long Beach | United States | AR-10825 | Anthony Rawles |
Office Supplies | West | Pasadena | United States | HA-14920 | Helen Andreada |
Furniture | West | Pasadena | United States | HA-14920 | Helen Andreada |
ORDER BY Single Column Example –
Consider the Product table of the Customer database and select Category, Region, City, Country, Customer ID, and Customer Name columns from the table, sort in DESCENDING by the “City” column.
SELECT Category,Region,City,
[Country/Region],[Customer ID],[Customer Name]
FROM [dbo].[Products]
ORDER BY City DESC ;
The output after executing the above statement is as shown –
Category | Region | City | Country | Customer_ID | Customer_name |
Office Supplies | West | Pasadena | United States | HA-14920 | Helen Andreada |
Furniture | West | Pasadena | United States | HA-14920 | Helen Andreada |
Technology | East | New York City | United States | AH-10465 | Amy Hunt |
Office Supplies | East | New York City | United States | AH-10465 | Amy Hunt |
Office Supplies | East | Long Beach | United States | AR-10825 | Anthony Rawles |
Office Supplies | East | Dover | United States | EP-13915 | Emily Phan |
Office Supplies | East | Dover | United States | EP-13915 | Emily Phan |
Office Supplies | Central | Chicago | United States | SM-20950 | Suzanne McNair |
Technology | West | Aurora | United States | TP-21565 | Tracy Poddar |
Office Supplies | West | Aurora | United States | TP-21565 | Tracy Poddar |
In the output, the columns in the SELECT statement have been sorted in descending alphabetic order of City.
ORDER BY Multiple Columns Example –
Consider the Product table of the Customer database and select the Customers Name, Category, City, and Country columns from the table, sort them in DESCENDING by the “City” column and ASCENDING by the “Country/Region” column.
SELECT Category,Region,City,
[Country/Region],[Customer ID],[Customer Name]
FROM [dbo].[Products$]
ORDER BY City DESC , [Country/Region] ASC;
The output after executing the above statement is as shown –
Category | Region | City | Country | Customer_ID | Customer_name |
Office Supplies | West | Pasadena | United States | HA-14920 | Helen Andreada |
Furniture | West | Pasadena | United States | HA-14920 | Helen Andreada |
Technology | East | New York City | United States | AH-10465 | Amy Hunt |
Office Supplies | East | New York City | United States | AH-10465 | Amy Hunt |
Office Supplies | East | Long Beach | United States | AR-10825 | Anthony Rawles |
Office Supplies | East | Dover | United States | EP-13915 | Emily Phan |
Office Supplies | East | Dover | United States | EP-13915 | Emily Phan |
Office Supplies | Central | Chicago | United States | SM-20950 | Suzanne McNair |
Technology | West | Aurora | United States | TP-21565 | Tracy Poddar |
Office Supplies | West | Aurora | United States | TP-21565 | Tracy Poddar |
In the output, the columns in the SELECT statement have been sorted in descending alphabetic order of City, and then all the cities are sorted in ascending order of Country.
Follow us on Twitter, Facebook, Linkedin, and Tableau Public to stay updated with our latest blog and what’s new in Tableau.
If you are looking forward to getting your data pipeline built and setting up the dashboard for business intelligence, book a call now from here.
#analytics #data #business #artificialintelligence #machinelearning #startup #deeplearning #deeplearning #datascience #ai #growth #dataanalytics #india #datascientist #powerbi #dataanalysis #tableau #SQL #businessanalytics #businessanalyst #businessandmanagement #dataanalyst #businessanalysis #analyst #analysis #powerbideveloper #powerbidesktop #letsviz