MySQL finding the most expensive in each zip code

前端 未结 4 456
盖世英雄少女心
盖世英雄少女心 2021-01-22 06:36

I have a table called Products with the schema (name, city, state, zip_code price).

And I want to find the most expensive products\' name for a given state\'s each zip_c

4条回答
  •  渐次进展
    2021-01-22 07:41

    This should work, though I can't vouch for it's efficiency. Per comment, here's an update that pulls back all records with price equal to the max price per zip code.

    SELECT *
      FROM products p1
     WHERE p1.state = 'NJ'
       AND p1.price = (select max(price) from products p2
                       where p1.zip_code = p2.zip_code)
    

    http://www.sqlfiddle.com/#!2/98f6d/2

提交回复
热议问题