SQL Exercises: Items with a price over 250 in ASC order by price,name
SQL Basic Select Statement: Exercise-28 with Solution.
From the following table, write a SQL query to find the items whose prices are higher than or equal to $250. Order the result by product price in descending, then product name in ascending. Return pro_name and pro_price.
Sample table: item_mast
PRO_ID PRO_NAME PRO_PRICE PRO_COM ------- ------------------------- -------------- ---------- 101 Mother Board 3200.00 15 102 Key Board 450.00 16 103 ZIP drive 250.00 14 104 Speaker 550.00 16 105 Monitor 5000.00 11 106 DVD drive 900.00 12 107 CD drive 800.00 12 108 Printer 2600.00 13 109 Refill cartridge 350.00 13 110 Mouse 250.00 12
Sample Solution:
-- This query selects specific columns 'pro_name' and 'pro_price' from the 'item_mast' table.
SELECT pro_name, pro_price
-- Specifies the table from which to retrieve the data (in this case, 'item_mast').
FROM item_mast
-- Filters the rows to only include those where the 'pro_price' column is greater than or equal to 250.
WHERE pro_price >= 250
-- Orders the result set first by 'pro_price' in descending order and then by 'pro_name' in ascending order.
ORDER BY pro_price DESC, pro_name;
Output of the Query:
pro_name pro_price Monitor 5000.00 Mother Board 3200.00 Printer 2600.00 DVD drive 900.00 CD drive 800.00 Speaker 550.00 Key Board 450.00 Refill cartridge 350.00 Mouse 250.00 ZIP drive 250.00
Code Explanation:
The said SQL query that selects the product name (pro_name) and price (pro_price) from the table item_mast, where the price is greater than or equal to 250. The results are then ordered by price in descending order, and then by product name in ascending order.
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 Python Exercise: Display the item name and price
Next Python Exercise: Display the average price of the items for each company.
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-28.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics