SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2001
    Location
    Kansas City, MO
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SQL Question from a beginner

    I know that the following will return the highest price in the 'price' column:

    Code:
    SELECT MAX(price)
    FROM items_ordered;
    How can I also get it to show the corresponding item with the max price?

    I have played around with it but can't figure it out.

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Assuming that your items table is called "items" and that the item name field is called "name"

    Code SQL:
    SELECT
          MAX(orders.price) AS highest_price
        , items.name AS item
    FROM
        items_ordered AS orders
    INNER JOIN
        items
    ON
        items.item_id = orders.item_id
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,247
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by Andwise View Post
    How can I also get it to show the corresponding item with the max price?
    here's a quick way:
    Code:
    SELECT item, price
    FROM items_ordered
    ORDER BY price DESC LIMIT 1
    spacephoenix, i think there might have been only one table involved in this question
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,014
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    here's a quick way:
    Code:
    SELECT item, price
    FROM items_ordered
    ORDER BY price DESC LIMIT 1
    spacephoenix, i think there might have been only one table involved in this question
    I did wonder if it was just one table. Andwise, is it one table or is it two tables?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2001
    Location
    Kansas City, MO
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    I did wonder if it was just one table. Andwise, is it one table or is it two tables?
    It is one table. Sorry for not specifying. I never considered using the limit function.

    Thanks for your help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •