Very Easy
Order by multiple columns I
Select all columns and rows from the students
table, and sort the results by (1) first_name
in ascending order, and (2) student_id
in ascending order.
TABLES
students
+------------+------------+-----------+-----+------------+
| student_id | first_name | last_name | age | class_code |
+------------+------------+-----------+-----+------------+
| 1 | Mike | Chaplin | 12 | 7A |
| 2 | Emily | Jackson | 12 | 7A |
| 3 | Robert | Jackson | 12 | 7A |
| ... | ... | ... | ... | ... |
+------------+------------+-----------+-----+------------+
EXPECTED OUTPUT
+------------+------------+-----------+-----+------------+
| student_id | first_name | last_name | age | class_code |
+------------+------------+-----------+-----+------------+
| 1 | Mike | Chaplin | 12 | 7A |
| 2 | Emily | Jackson | 12 | 7A |
| 3 | Robert | Jackson | 12 | 7A |
| ... | ... | ... | ... | ... |
+------------+------------+-----------+-----+------------+
To sort a table table1
by two columns col1
and col2
, you can write:
SELECT *
FROM table1
ORDER BY
col1 ASC,
col2 ASC
SELECT *
FROM students
ORDER BY
first_name ASC,
student_id ASC