ByteBoard Notes Notes

Display the (student ID, marks) table in descending order of the marks

Informatics Practices XII · Experiment 29 of 38 · SQL

★ CBSE suggested12-IPSQL queries
Expt. No.29
Page No.58

Order by marks

Aim

To display the student ID and marks of every student in descending order of marks.

Concepts used

Queries

USE school;SELECT sid, marks FROM student ORDER BY marks DESC;

A marked line continues the line above it. In your file, write it on the same line.

Table used

Table: student
+-----+-------+-------+
| sid | name  | marks |
+-----+-------+-------+
|   1 | Asha  |    82 |
|   2 | Rohan |    67 |
|   3 | Ira   |    88 |
|   4 | Kabir |    91 |
|   5 | Diya  |    74 |
+-----+-------+-------+

Output

mysql> USE school; Database changed
mysql> SELECT sid, marks FROM student ORDER BY marks DESC; +-----+-------+ | sid | marks | +-----+-------+ | 4 | 91 | | 3 | 88 | | 1 | 82 | | 5 | 74 | | 2 | 67 | +-----+-------+ 5 rows in set

✓ Printed by running this query set, not typed by hand.

Result

The rows were displayed from the highest marks (sid 4, 91) to the lowest (sid 2, 67).

Viva voce: what the examiner may ask

01What is the default order of ORDER BY?

Ascending (ASC).

02Can you sort on a column that is not displayed?

Yes, e.g. SELECT name FROM student ORDER BY marks.

03How do you sort on two columns?

List both: ORDER BY class, marks DESC.

Variations for the lab test

  1. Display names in alphabetical order.
  2. Display the students who scored above 70 in ascending order of marks.
  3. Display name and marks with the lowest marks first.
! Common mistake Writing ORDER marks DESC without BY is a syntax error.
Back to the 12-IP index