0

Been practising some SQL here: http://sqlzoo.net/wiki/SELECT_from_Nobel_Tutorial

For the last bit (14.), we have to sort by a certain column but have certain strings in the column go last. This is my query:

SELECT winner, subject 
FROM nobel 
WHERE yr = 1984 
ORDER BY CASE WHEN subject IN ('Chemistry', 'Physics') THEN 2 ELSE 1 END, subject, winner

Is there a more efficient way of querying that without using cases?

Jeremy Yeo
  • 493
  • 4
  • 9
  • Efficiency depends on the RDBMS and table structures,including indexes, Neither of which you state. – Martin Smith Oct 15 '15 at 05:22
  • Also, you don't really need to worry about efficiency for a simple query on a table with 816 rows. Sure, you may want to learn best practices, but as @MartinSmith points out, you can't [optimise](http://stackoverflow.com/questions/385506/when-is-optimisation-premature) without the specific details. – Turophile Oct 15 '15 at 05:37

1 Answers1

0

Here another efficient way:

SELECT winner, subject 
  FROM nobel
  WHERE yr=1984
 ORDER BY  subject IN ('Physics','Chemistry'), subject , winner
Chhun Panharath
  • 136
  • 1
  • 10