0

If I have a table of products (> 10000 records), as the following example

[products]
+ ------------+---------------+
| product_no  | name          |
+ ------------+---------------+
| 84756289    | Screwdriver   |
| 7365298     | Hammer        |
| 49890986    | Nails, 3 inch |

   .... etc etc etc .......      

 ... finally the last row ...

| 846519846   | Staplers      |
+-------------+---------------+

If I run a MySQL query like SELECT * FROM products LIMIT 10 (or SELECT TOP 10 * ... in SQL Server) I will get the first 10 records from the table. But can I run a query that returns the 10 last records? Conceptually like this:

SELECT LAST 10 * FROM products
or
SELECT * FROM products LIMIT LAST 10

The table is not sorted. New products are simply added at the end... So I can't use ORDER BY to solve my question

Gowire
  • 1,046
  • 6
  • 27
  • 4
    `TOP 10` and `LIMIT 10` are not equivalent. LIMIT without ORDER BY is fairly meaningless. Just add a date column or an AI id to return rows in the order in which they were added. – Strawberry Oct 24 '20 at 11:41
  • possible duplicate https://stackoverflow.com/questions/4193705/sql-server-select-last-n-rows – Boudy hesham Oct 24 '20 at 11:51
  • 1
    the last 10 has to have some knd of order, that defiens what are the last 10, so if you on't have a column that defines it add one – nbk Oct 24 '20 at 11:54

0 Answers0