SQL Exercise: Physiology prize before 71 with a Peace prize after 74
SQL Basic Select Statement: Exercise-21 with Solution.
From the following table, write a SQL query to combine the winners in 'Physiology' before 1971 and winners in 'Peace' on or after 1974. 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
-- Combines the results of two SELECT statements using the UNION operator.
-- The first SELECT statement retrieves rows where the 'subject' is 'Physiology' and the 'year' is less than 1971.
WHERE (subject = 'Physiology' AND year < 1971)
-- The UNION operator combines the results of the first SELECT statement with the results of the second SELECT statement.
UNION
-- The second SELECT statement retrieves rows where the 'subject' is 'Peace' and the 'year' is greater than or equal to 1974.
(SELECT * FROM nobel_win WHERE (subject = 'Peace' AND year >= 1974));
Output of the Query:
year subject winner country category 1970 Physiology Bernard Katz Germany Scientist 1970 Physiology Julius Axelrod USA Scientist 1970 Physiology Ulf von Euler Sweden Scientist 1978 Peace Anwar al-Sadat Egypt President 1978 Peace Menachem Begin Israel Prime Minister 1994 Peace Yitzhak Rabin Israel Prime Minister
Code Explanation:
The said SQL query retrieves all columns from the 'nobel_win' table and combines them with the results from another query. The first query selects all columns from the table where the subject is 'Physiology' and the year is less than 1971. The second query selects all columns from the table where the subject is 'Peace' and the year is greater than or equal to 1974. The UNION operator combines the results of both queries, returning all rows from the first query and the second query that are unique. The output will be a list of all unique nobel prize awarded before 1971 for physiology and after 1974 for peace
Relational Algebra Expression:
Relational Algebra Tree:
Practice Online
Query Visualization:
Duration:
Rows:
Cost:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: The 1970 Nobel prize winners except for given subjects.
Next SQL Exercise: Details of the prize won by Johannes Georg Bednorz.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/sql-exercises/sql-retrieve-exercise-21.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics