SQL Exercises: Items with a price over 250 in ASC order by price,name
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
For more Practice: Solve these Related Problems:
- Write a SQL query to find products priced higher than or equal to $500, ordered by price in descending order.
- Write a SQL query to retrieve items with prices greater than $1000, sorted by product name in ascending order.
- Write a SQL query to display products costing at least $300, ordered by price in descending and then by name in ascending order.
- Write a SQL query to list all items priced above $200, grouped by manufacturer code and ordered by price.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous SQL Exercise: Display the item name and price
Next SQL 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.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics