Products

   

Write an SQL query to fetch all products with an odd-numbered ID and which is not a vegetable. The result should be ordered in descending order of the price of the product.



Input :-

Products table :-
ID is the Primary Key column for the Products table.

ID

Name

Type

Price

1

Orange

Fruit

100

2

Cheeku

Fruit

85

3

Cucumber

Vegetable

25

4

Bell Peppers

Vegetable

40

5

Mango

Fruit

130


Output :-

ID

Name

Type

Price

5

Mango

Fruit

130

1

Orange

Fruit

100



Query :-
select * 
from Products
where ID%2=1 and Type!='Vegetable'
order by Price desc;

Explanation : - 

This query is used to retrieve data from a table named "Products". Here's an explanation of each part of the query:

  • ‘Select *’ selects all the columns from the "Products" table that should be included in the result set. The asterisk symbol ‘*’ is a shorthand way of selecting all columns.
  • ‘Where ID%2=1’ is a condition that filters the odd numbered rows based on the ID column. The modulus operator (%) is used to divide the ID by 2 and check if the remainder is equal to 1 (odd).
  • Type!='Vegetable' :- This condition further filters the rows by excluding rows where the Type column is equal to ‘Vegetable’. The exclamation mark (!) is used for the not equal to comparison.
  • ‘Order by Price desc’ specifies the ordering of the result set based on the 'Price' column in descending order. 

In summary, the query selects all columns from the "Products" table where the ID is an odd number and the Type is not equal to ‘Vegetable’. The result set is then sorted in descending order based on the ‘Price’ column.






No comments

darkmode