w3resource

SQL Exercise: The 1970 Nobel prize winners except for given subjects

SQL Basic Select Statement: Exercise-20 with Solution.

From the following table, write a SQL query to find the Nobel Prize winners in 1970 excluding the subjects of Physiology and Economics. Return year, subject, winner, country, and category.

Sample table: nobel_win

YEAR SUBJECT                   WINNER                                        COUNTRY                CATEGORY
---- ------------------------- --------------------------------------------- ------------------------- ------------
1970 Physics                   Hannes Alfven                                 Sweden                 Scientist
1970 Physics                   Louis Neel                                    France                 Scientist
1970 Chemistry                 Luis Federico Leloir                          France                 Scientist
1970 Physiology                Ulf von Euler                                 Sweden                 Scientist
1970 Physiology                Bernard Katz                                  Germany                Scientist
1970 Literature                Aleksandr Solzhenitsyn                        Russia                 Linguist
1970 Economics                 Paul Samuelson                                USA                    Economist
1970 Physiology                Julius Axelrod                                USA                    Scientist
1971 Physics                   Dennis Gabor                                  Hungary                Scientist
1971 Chemistry                 Gerhard Herzberg                              Germany                Scientist
1971 Peace                     Willy Brandt                                  Germany                Chancellor
1971 Literature                Pablo Neruda                                  Chile                  Linguist
1971 Economics                 Simon Kuznets                                 Russia                 Economist
1978 Peace                     Anwar al-Sadat                                Egypt                  President
1978 Peace                     Menachem Begin                                Israel                 Prime Minister
1987 Chemistry                 Donald J. Cram                                USA                    Scientist
1987 Chemistry                 Jean-Marie Lehn                               France                 Scientist
1987 Physiology                Susumu Tonegawa                               Japan                  Scientist
1994 Economics                 Reinhard Selten                               Germany                Economist
1994 Peace                     Yitzhak Rabin                                 Israel                 Prime Minister
1987 Physics                   Johannes Georg Bednorz                        Germany                Scientist
1987 Literature                Joseph Brodsky                                Russia                 Linguist
1987 Economics                 Robert Solow                                  USA                    Economist
1994 Literature                Kenzaburo Oe                                  Japan                  Linguist

Sample Solution:

-- This query selects all columns from the 'nobel_win' table.
SELECT *
-- Specifies the table from which to retrieve the data (in this case, 'nobel_win').
FROM nobel_win
-- Filters the rows to only include those where the 'year' column has the value 1970
-- and the 'subject' column is not 'Physiology' or 'Economics'.
WHERE year = 1970
  AND subject NOT IN ('Physiology', 'Economics');

Output of the Query:

year   subject	  winner	        country	    category
1970   Physics	  Hannes Alfven         Sweden	    Scientist
1970   Physics	  Louis  Neel	        France	    Scientist
1970   Chemistry  Luis Federico Leloir  France	    Scientist
1970   Literature Aleksandr Solzhenitsyn Russia     Linguist

Code Explanation:

The above SQL query retrieves all columns from the 'nobel_win' table where the year is 1970 and subject is not 'Physiology' or 'Economics' . The query will return all rows that match this criteria, resulting in a list of all Nobel Prizes awarded in 1970 which is not in the subjects 'Physiology' or 'Economics'. The NOT IN operator allows to check if a value is not within a set of values, in this case the query will filter for rows where the subject column is not 'Physiology' or 'Economics'.

Relational Algebra Expression:

Relational Algebra Expression: Show all the winners of nobel prize in the year 1970 except the subject Physiology and Economics.

Relational Algebra Tree:

Relational Algebra Tree: Show all the winners of nobel prize in the year 1970 except the subject Physiology and Economics.

Practice Online


Query Visualization:

Duration:

Query visualization of Show all the winners of nobel prize in the year 1970 except the subject Physiology and Economics - Duration

Rows:

Query visualization of Show all the winners of nobel prize in the year 1970 except the subject Physiology and Economics - Rows

Cost:

Query visualization of Show all the winners of nobel prize in the year 1970 except the subject Physiology and Economics - Cost

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous Python Exercise: All winners in Physics for 1970 and Economics for 1971.
Next Python Exercise: Physiology prize before 71 with a Peace prize after 74.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.