← Back to Lessons
↕️
Sorting with ORDER BY
ORDER BY
Let's Learn! 🎓
ORDER BY is like organizing your bookshelf! You can arrange things from A to Z, smallest to biggest, or newest to oldest. It makes finding things much easier!
Example Query:
SELECT name, age FROM users ORDER BY age DESCThis means: 'This means: "Show me names and ages, arranged from oldest to youngest"'
Sample Table USERS 📊
| id | age | city | name | country |
|---|---|---|---|---|
| 1 | 25 | New York | Alice | USA |
| 2 | 30 | London | Bob | UK |
| 3 | 22 | Paris | Charlie | France |
| 4 | 17 | Tokyo | Diana | Japan |
| 5 | 28 | New York | John | USA |
| 6 | 35 | London | Jane | UK |
| 7 | 19 | Paris | Jack | France |
Query Result 🖥️
| age | name |
|---|---|
| 35 | Jane |
| 30 | Bob |
| 28 | John |
| 25 | Alice |
| 22 | Charlie |
| 19 | Jack |
| 17 | Diana |
Understanding Each Part ⚙️
| SQL Part | Explanation |
|---|---|
SELECT name, age | Selecting two columns: name and age |
FROM users | From the users table |
ORDER BY | Time to sort the results! |
age | Sort by the age column |
DESC | DESC means descending (biggest first) |