ORDER BY — Complete Guide
ORDER BY — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of SQL Server Tutorial on Toolliyo Academy.
On this page
SQL Server Tutorial · Lesson 13 of 100
ORDER BY
SQL basics → Queries → Advanced
SQL basics · 1 — SELECT · ~6 min · SQL — SQL Queries & Clauses
What is this?
ORDER BY sorts the result set. ASC is default; DESC reverses. You can sort by column name, alias, or position.
Why should you care?
Users expect newest orders first or cheapest products first. Unordered SELECT has no guaranteed order.
See it live — copy this example
Run in SQL Server Management Studio (SSMS) or Azure Data Studio.
USE DataVerse;
SELECT ProductId, Name, PriceInr
FROM dbo.Products
ORDER BY PriceInr ASC, Name ASC;
What happened?
- Rows sort by price low to high.
- Ties on price break alphabetically by Name.
- Stable, predictable grids for UI.
Practice next
- Run ORDER BY PriceInr DESC.
- Sort by Name only and compare.
- Use ORDER BY 2 to sort by the second selected column.
- ORDER BY LEN(Name) DESC.
- Sort InStock DESC, PriceInr ASC.
Remember
ORDER BY defines sort order. ASC / DESC control direction. Multiple keys break ties.
Price-low-to-high product grid
DataVerse storefront sorts catalog by PriceInr ASC.
Outcome: Shoppers see budget options first when they choose that sort.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!