Last answered:

02 Dec 2021

Posted on:

02 Dec 2021

4

What does the minus sign ( – ) mean in ORDER BY –a.emp_no DESC;

What does the minus sign ( – ) mean in ORDER BY –a.emp_no DESC;

1 answers ( 0 marked as helpful)
Posted on:

02 Dec 2021

11
  1. ORDER BY a.emp_no DESC;

If you end the relevant query this way, you will obtain an output ordered with the highest employee number on top, the lowest employee number down the list, and the null values at the end.

image
2) ORDER BY a.emp_no ASC;

This ending of the query will do the opposite - the null values will be on top, and then the employee numbers will grow from the lowest to the highest.
image
For options 3) and 4), remember that using ORDER BY followed by a minus sign works for numeric data only!

  1. ORDER BY -a.emp_no DESC;

Using this code, you will first order the employees from the lowest to the highest number, and then leave the null values at the end.
image
4) ORDER BY -a.emp_no ASC;

Following the logic explained so far, this ending would list the null value first, and will then order all employees from the highest to the lowest number.
image
Depending on the situation, you may choose between 1), 2), 3), and 4). We think 3) suits our example best.

That's why we have ended the query with ORDER BY -a.emp_no DESC; .

Why would using a minus sign in such a situation be useful at all?

Specifically, the combination used in 3) is a frequently used technique because it allows the user to sort their output in
ascending order, without starting with a (sometimes large) number of null values. In other words, if the user prefers to see the null values at the end of the output, using ORDER BY -a.emp_no DESC; is a very convenient choice.

Why does it work that way?

This is simply a peculiarity of the SQL syntax, and we must always comply with it.

Submit an answer