Home > database >  SQL Why put winner at the end while order by year?
SQL Why put winner at the end while order by year?

Time:10-21

From the following table, write a SQL query to find the Nobel Prize winners for the subject not started with the letter 'P'. Return year, subject, winner, country, and category. Order the result by year, descending.

Sample table : nobel_win

enter image description here

Solution:

SELECT * 
 FROM nobel_win 
  WHERE subject NOT LIKE 'P%' 
   ORDER BY year DESC, winner;

I don't understand why the solution put winner at the end. It seems that winner is not required.

CodePudding user response:

Most likely, the winner column was tagged to the end of the ORDER BY clause to break the tie should 2 or more records happen to have the same year. If that be the case, then simply ordering by year descending might leave all records belonging to a given year appearing unsorted. The query you posted above resolves this by ordering all record within a given year by the winner name.

  •  Tags:  
  • sql
  • Related