Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find most expensive items in sql with more than one most expensive item?

Tags:

sqlite

mysql

max

I have more than one items with the highest price on my database and I would like to know how to return the most expensive items with the highest price on my database, without using LIMIT. I have tried using

SELECT MAX(price) FROM items

but it only returns one item with the highest price (I have two items with the highest price).

The schema of my database is

items (itemID: integer, description: string, price: integer)
like image 327
mar Avatar asked Nov 15 '25 10:11

mar


1 Answers

This will work.

SET @p1 := (SELECT MAX(price) FROM items);
SELECT * FROM items WHERE price = @p1;

Using variables, p1 stores the maximum price from the table items and then uses the variable p1 in the following query to return all records which have that maximum price without limiting the number of records as you desired.

like image 116
mseifert Avatar answered Nov 17 '25 06:11

mseifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!