w3resource

SQL Exercises: Subjects not start with P, sorted by most recent, name

SQL Basic Select Statement: Exercise-23 with Solution.

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

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 'subject' column does not start with 'P'.
WHERE subject NOT LIKE 'P%'
-- Orders the result set first by 'year' in descending order and then by 'winner' in ascending order.
ORDER BY year DESC, winner;

Output of the Query:

year	subject	   winner			country	   	category
1994	Literature Kenzaburo Oe	      		Japan	  	Linguist
1994	Economics  Reinhard Selten		Germany	  	Economist
1987	Chemistry  Donald J. Cram		USA		Scientist
1987	Chemistry  Jean-Marie Lehn		France	  	Scientist
1987	Literature Joseph Brodsky		Russia	  	Linguist
1987	Economics  Robert Solow			USA		Economist
1971	Chemistry  Gerhard Herzberg    		Germany 	Scientist
1971	Literature Pablo Neruda			Chile	  	Linguist
1971	Economics  Simon Kuznets		Russia	  	Economist
1970	Literature Aleksandr Solzhenitsyn 	Russia 		Linguist
1970	Chemistry  Luis Federico Leloir 	 France		Scientist
1970	Economics  Paul Samuelson		USA		Economist

Code Explanation:

The given SQL query retrieves all columns from the 'nobel_win' table where the subject does not start with 'P'. The query will return all rows that match this criteria. The output will be sorted in descending order by year, and for the rows with the same year it will be sorted in ascending order by the winner's name.
The NOT LIKE operator is used to match a specific pattern, in this case it will match any subject that does not start with 'P' and the % sign is a wildcard character that matches any sequence of characters.
The ORDER BY clause sorts the result set in either ascending or descending order of specified column(s).

Relational Algebra Expression:

Relational Algebra Expression: Find all the details of the nobel winners for the subject not started with the letter 'P' and arranged the list as the  most recent comes first, then by name in order.

Relational Algebra Tree:

Relational Algebra Tree: Find all the details of the nobel winners for the subject not started with the letter 'P' and arranged the list as the  most recent comes first, then by name in order.

Practice Online


Query Visualization:

Duration:

Query visualization of Find all the details of the nobel winners for the subject not started with the letter 'P' and arranged the list as the most recent comes first, then by name in order - Duration

Rows:

Query visualization of Find all the details of the nobel winners for the subject not started with the letter 'P' and arranged the list as the most recent comes first, then by name in order - Rows

Cost:

Query visualization of Find all the details of the nobel winners for the subject not started with the letter 'P' and arranged the list as the most recent comes first, then by name in order - Cost

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

Previous Python Exercise: Details of the prize won by Johannes Georg Bednorz.
Next Python Exercise: Economics, Chemistry listed at end of 1970 as winners.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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://w3resource.com/sql-exercises/sql-retrieve-exercise-23.php