Post

SELECT from Nobel

This is the third exercise from SQLZOO. First half of the third exercise is just a review on previous exercises. I don’t plan to repeat them. Second half is where it gets harder, and even harder in the next post. This exercise will teach us new SQL tricks, I am going to list some of them below.

New Keywords to learn

ORDER BY
Sort results in order

ASC | DESC
When using ORDER BY, can sort in either ascending or descending

EXERCISE 3 BEGINS!

Database “nobel” we are using looks like this:

yrsubjectwinner
1960ChemistryWillard F. Libby
1960LiteratureSaint-John Perse
1960MedicineSir Frank Macfarlane Burnet
1960MedicinePeter Madawar
  

Skipping the first half and starting with the second part. Supposed we want to see all of the details of these presidential winners below:

  • Theodore Roosevelt
  • Jimmy character
  • Barack Obama
1
2
3
SELECT *
FROM nobel
WHERE winner IN ('Theodore Roosevelt', 'Jimmy Carter', 'Barack Obama')

The asterisk * means all. SELECT * to gather ALL information/data or on every columns. WHERE to filter or search for multiple winners using IN and (...).

Another challenge, supposed want to see Physics winners for 1980 together with Chemistry winners in 1984? It is probably easy, but good to understand how it works.

1
2
3
SELECT yr, subject, winner
FROM nobel
WHERE subject = 'Physics' AND yr = '1980' OR subject = 'Chemistry' AND yr = '1984'

SQL likes to be long and specific. Filling in can get really long. OR is not like XOR’s “Either you are with us or against us!”. OR is in a sense ‘AND’, but without confusing the computer. If we used AND instead of OR then it would be overlapping data. Nobody can say “apple” and “orange” at the same time.

Another great example of how SQL likes to be long! Show the winners for 1980 excluding Chemistry and Medicine.

1
2
3
SELECT yr, subject, winner
FROM nobel
WHERE yr = 1980 AND subject NOT LIKE 'Chemistry' AND subject NOT LIKE 'Medicine'

See! Filters for 1980 AND subject NOT LIKEAND subject NOT LIKEwhew! I wish could be simple like AND subject NOT LIKE subject1 OR subject2. It doesn’t work that way.

Wait a minute, isn’t that contradicting?! NEED A UPDATE LATER ON!

Why did AND works this time when have two different same type of data this time? I guess it’s when we use NOT LIKE. I will need to return here to clarify when I understand SQL better.

Different challenges

Find all the details of the prize won by Eugene O’Neill. If you typed Eugene 0'Neill. It won’t be recognized. This is where SQL gets weird.

1
2
3
SELECT yr, subject, winner
FROM nobel
WHERE winner = 'Eugene O''Neill'

That’s right! " in the between O and Neill. It is to escape the single quotes. I wonder why not "Eugene O'Neill", but it doesn’t work like that.

This problem want to find details of winners that has name start with ‘Sir’. Show the most recent first the name in order. This is where we get to use ORDER BY and DESC.

1
2
3
4
SELECT winner, yr, subject
FROM nobel
WHERE winner LIKE 'Sir%'
ORDER BY yr DESC

Last different challenge, and this one I had hard time with at first. I now understand it. The problem states:

The expression subject IN (‘Chemistry’,’Physics’) can be used as a value - it will be 0 or 1.

Show the 1984 winners and subject ordered by subject and winner name; but list Chemistry and Physics last.

That is the challenge, and the clue it had left to us is this below:

1
2
3
4
SELECT winner, subject, subject IN ('Physics','Chemistry')
FROM nobel
WHERE yr=1984
ORDER BY subject,winner

This is what RESULT will look like.

winnersubjectsubject IN (‘..
Bruce MerrifieldChemistry1
Richard StoneEconomics0
Jaroslav SeifertLiterature0
César MilsteinMedicine0
Georges J.F. KöhlerMedicine0
Niels K. JerneMedicine0
Desmond TutuPeace0
Carlos RubbiaPhysics1
Simon van der MeerPhysics1

Notice how 0 and 1 are not ordered. Subject is ordered alphabetically. This challenge teaches us how to set an order by 0 and 1. Let’s see that works.

1
2
3
4
SELECT winner, subject, subject IN ('Physics','Chemistry')
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'), subject,winner

We arranged data in order by 0 and 1 and then subjects and winners alphabetically.

winnersubjectsubject IN (‘..
Richard StoneEconomics0
Jaroslav SeifertLiterature0
César MilsteinMedicine0
Georges J.F. KöhlerMedicine0
Niels K. JerneMedicine0
Desmond TutuPeace0
Bruce MerrifieldChemistry1
Carlos RubbiaPhysics1
Simon van der MeerPhysics1

This challenge doesn’t want to see the value in index manner. Taking that subject IN ('...') off from SELECT.

1
2
3
4
SELECT winner, subject
FROM nobel
WHERE yr=1984
ORDER BY subject IN ('Physics','Chemistry'), subject,winner

RESULT:

winnersubject
Richard StoneEconomics
Jaroslav SeifertLiterature
César MilsteinMedicine
Georges J.F. KöhlerMedicine
Niels K. JerneMedicine
Desmond TutuPeace
Bruce MerrifieldChemistry
Carlos RubbiaPhysics
Simon van der MeerPhysics

That’s the answer.

Next post will be EXERCISE 3 still but covering the quiz. New tricks coming up!

This post is licensed under CC BY 4.0 by the author.

Comments powered by Disqus.