1. We have moved to a new forum! There may be a few things not working properly so please let us know if you find a bug. Remember to use the bbCode [ sql ] tag for SQL statements.

How do I select the highest 20 to 30 values?

Discussion in 'SQL - Questions and Answers' started by ben, Sep 2, 2006.

  1. ben Administrator

    Dialect: MySQL
  2. ben Administrator

    Dialect: MySQL
    The idea is always the same and can be adjusted to select any position. First you have to order the query and select the first 19 values (Let's say we want the 20th to 30th highest score in a competition). Then exclude the first 19 rows, and return the next 10 rows. This will give you the 20th to 30th highest score.

    SQL:

    SELECT * FROM competition WHERE id NOT IN (SELECT id FROM competition ORDER BY score DESC LIMIT 19) ORDER BY score DESC LIMIT 10
  3. Dimitar Guest

    Dialect: MySQL
    Wouldn't this also do the trick?
    SQL:

    select * from competition order by score desc limit 19,10;
  4. ben Administrator

    Dialect: MySQL
    Dimitar, Good point :)
  5. Unregistered Guest

    SELECT TOP (10) ShippingId
    FROM Shipping
    WHERE (ShippingId NOT IN
    (SELECT TOP (20) ShippingId
    FROM Shipping AS Shipping_1
    ORDER BY ShippingId DESC))
    ORDER BY ShippingId DESC

Share This Page