-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmostExpensive.mysql
More file actions
23 lines (20 loc) · 988 Bytes
/
mostExpensive.mysql
File metadata and controls
23 lines (20 loc) · 988 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
Mr. Cash wants to keep track of his expenses, so he has prepared a list of all the products he bought this month.
Now he is interested in finding the product on which he spent the largest amount of money.
If there are products that cost the same amount of money, he'd like to find the one with the lexicographically smallest name.
The list of expenses is stored in a table Products which has the following columns:
id: unique product id;
name: the unique name of the product;
price: the price for one item;
quantity: the number of items bought.
The resulting table should contain one row with a single column: the product with the lexicographically smallest name on which Mr. Cash spent the largest amount of money.
The total amount of money spent on a product is calculated as price * quantity.
*/
CREATE PROCEDURE mostExpensive()
BEGIN
SELECT name
FROM Products
WHERE price * quantity = (SELECT MAX(price*quantity) FROM Products)
ORDER BY name
LIMIT 1;
END