KwickAcademy Databases and SQL · 5 min · free
Sorting Results with ORDER BY
ORDER BY sorts the result. ASC is the default, DESC sorts from big to small, and a second column breaks ties. Clause order is fixed: SELECT, FROM, WHERE, ORDER BY.
Follows the syllabus of: CBSE Class 12 Computer Science (083), CBSE Class 12 Informatics Practices (065), CBSE Class 11 Information Technology (802), CBSE Class 12 Information Technology (802)
On screen in this lesson
ASC and DESC
| Keyword | Order | Numbers |
|---|---|---|
| ASC | small to big | 65, 72, 88 |
| DESC | big to small | 88, 72, 65 |
| (nothing) | same as ASC | 65, 72, 88 |
Why sort by two columns?
| Many rows can share the same first value |
| The second column breaks the tie |
| Example: a class list sorted by section, then marks |
Each column has its own order
| ORDER BY sec, marks DESC: DESC is only for marks |
| sec here is still ascending |
| Write DESC after every column that needs it |
Clause order is fixed
| SELECT columns |
| FROM table |
| WHERE condition |
| ORDER BY column |
Quick recap
| ORDER BY sorts the result; ASC is the default |
| DESC sorts from big to small |
| Two columns: the second breaks ties |
| DESC applies only to the column before it |
| Order: SELECT, FROM, WHERE, ORDER BY |
Quick answers
What is the default sort order?
Ascending, small to big.
In ORDER BY sec, marks DESC, which column is descending?
Only marks.
KwickClips from this lesson
Short clips, one idea each. Good for revision the night before.
What is the default order?39 sec
Why sort by two columns?42 sec
Why the error when ORDER BY comes first?39 secThe full lesson, in text
Hello students, welcome to Kwickprep. A merit list shows the topper first. But a table has no fixed order, so how does SQL build that list? Today we will sort results with order by, sort on two columns, and combine where with order by.
All our examples use one table called student. It has five columns: roll, name, section, marks and city. Kabir was absent for the test, so his marks are NULL. NULL means no value at all. It is not zero, and it is not a blank space.
Order by sorts the rows in the result. Ascending means from small to big, and it is the default. So order by marks puts the lowest marks first. In MySQL, NULL counts as smaller than every value, so Kabir comes first in ascending order.
Descending means from big to small, and we write DESC after the column. Now Meera, the topper, comes first. This is our merit list. Kabir with NULL marks moves to the end in descending order in MySQL.
Let us compare the two keywords. ASC means ascending, from small to big. DESC means descending, from big to small. If you write nothing, SQL uses ascending.
Order by also works on text. Text is sorted in dictionary order, from A to Z. So Aman comes first and Riya comes last. With DESC, text goes from Z to A. Dates can be sorted too, from the oldest to the newest.
Sometimes the first column has repeated values. For example, three students are in section A. The second column decides the order inside each group, which is called breaking the tie. A teacher may want the list by section, and toppers first within each section.
Separate the columns with a comma. First, rows are sorted by section, so A comes before B. Inside section A, rows are sorted by marks in descending order. So Meera, Riya, then Neha. Section B follows the same rule.
Here is a trap that exams love. Each column gets its own direction. In sec, marks DESC, the DESC belongs only to marks. Section is still sorted ascending. If you want both descending, write DESC after each column.
Now we combine where and order by. Where first picks the rows from Surat, which are Riya and Neha. Then order by sorts only those rows, with high marks first. So Riya comes before Neha.
The clauses must be written in a fixed order. Select comes first with the columns. From comes next with the table. Where comes after from. Order by always comes last. Writing order by before where gives an error.
Pause and predict before you look at the output. Which students have marks above seventy? Riya, Aman and Meera. Kabir is dropped, because NULL is never greater than seventy. Now sort the names from Z to A. The answer is Riya, Meera, then Aman.
Some exams write table and field names with capital first letters, and ask you to complete such a query. The pattern is the same. Select the fields, from the table, where the condition, and order by the field last.
Let us revise what we learned today. Order by sorts the result, and ascending is the default. DESC sorts from big to small. With two columns, the second one breaks ties in the first. DESC applies only to the column written just before it. And the clause order is select, from, where, then order by. Make a merit list of your own class marks for practice.
Courses that teach this
| Course | Unit |
|---|---|
| CBSE Class 12 Computer Science (083) | Database Management |
| CBSE Class 12 Informatics Practices (065) | Database Query using SQL |
| CBSE Class 11 Information Technology (802) | Part B, Unit 4: RDBMS |
| CBSE Class 12 Information Technology (802) | Database Concepts - RDBMS Tool |
| GSEB Std 11 Computer Studies | Introduction to Layers |
| Cambridge IGCSE Grade 10 Computer Science (0478) | 9. Databases |
Voice-over in this lesson is AI-generated. The script is written and checked by Kajal Ma'am. Boards can revise a syllabus mid-year, so confirm anything you plan around against the official board circular. Keep your passwords, OTPs and ID numbers to yourself — we never ask for them. To reach Kajal Ma'am, use the WhatsApp button; sharing your number there is how we call you back.
Free to watch, no sign-up. Live classes with Kajal Ma'am are the paid course; these lessons stay free either way.

