Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

(iv) Display the name, price, and rating of products in descending order of rating.

Answer

Answer by student

SELECT PName, UPrice, Rating

FROM PRODUCT

ORDER BY Rating DESC;

Detailed answer by teachoo

  • The question asks us to display the name, price, and rating of products in descending order of rating. The name, price, and rating of products are stored in the PName, UPrice, and Rating columns of the PRODUCT table respectively. We need to query only the PRODUCT table for this question.
  • To display the name, price, and rating of products in descending order of rating, we need to use the SELECT statement in SQL. The SELECT statement is used to query data from one or more tables in a database.
  • The syntax of the SELECT statement is:

SELECT column1, column2, …

FROM table1, table2, …

WHERE condition

ORDER BY column3, column4, ... ASC|DESC;

  • The SELECT clause specifies the columns to be displayed in the result. The FROM clause specifies the tables to be queried . The WHERE clause specifies the condition to filter the rows that match the criteria . The ORDER BY clause specifies the columns to sort the rows by a certain order . The ASC or DESC keywords specify whether to sort the rows in ascending or descending order respectively.
  • In this case, we want to display the PName, UPrice, and Rating columns from the PRODUCT table . We do not need to use a WHERE clause as we do not have any condition to filter the rows. We need to use an O RDER BY clause that sorts the rows by Rating in descending order.
  • The SQL query for this question is:

-- Display the name, price, and rating of products in descending order of rating

SELECT PName, UPrice, Rating -- Select the columns PName, UPrice and Rating

FROM PRODUCT -- Query only the PRODUCT table

ORDER BY Rating DESC; -- Sort the rows by Rating in descending order

  • This query will display the name, price, and rating of products in descending order of rating in a tabular format. The output will show all the products with their name, price and rating sorted from highest to lowest rating. For example:

PName

UPrice

Rating

Toothpaste

54

8

Soap

25

7

Shampoo

120

6

Shampoo

245

6

Soap

38

5

Toothpaste

65

4

So, this is how we can write an SQL query to display the name, price, and rating of products in descending order of rating.

Go Ad-free
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.