Skip to content
Advertisement

SQL: How to select 1st, 3rd, 11th and nth row from a table?

How to select 1st, 3rd, 11th and nth row from a table?

Advertisement

Answer

If there is a primary key defined for the table that is an integer based data type–both MySQL and SQLite have auto_increment for example–then you can use:

SELECT t.*
  FROM TABLE t
 WHERE t.id IN (1,3, 11)

…where id is the auto_increment column.

There’s very little detail to go on, but MySQL and SQLite do not have analytic query support, making queries rather complicated:

SELECT y.*
  FROM (SELECT t.*,
                        (SELECT COUNT(*)
                            FROM TABLE x
                           WHERE x.col <= t.col) AS rank
               FROM TABLE t) y
 WHERE y.rank IN (1, 3, 11)
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement