How to select the last 3 rows but the last one

115 Views Asked by At
SELECT doesntmatterwhat
FROM whatever
OFFSET 3 LIMIT 2

How would I reproduce this, but WITHOUT using LIMIT or OFFSET to select the last 3 rows but the last one

2

There are 2 best solutions below

2
On BEST ANSWER

This was the solution I was looking for. Thanks for helping though.

SELECT  doesntmatterwhat
FROM    whatever
ORDER BY 1
OFFSET  3 ROWS
FETCH   FIRST 2 ROWS ONLY;
1
On
SELECT TOP 2 FROM (
SELECT TOP 3 doesntmatterwhat
FROM whatever
) a
ORDER BY doesntmatterwhat

Not using TOP

SELECT n.doesntmatterwhat
FROM (SELECT n.doesntmatterwhat, row_number() OVER (ORDER BY date DESC) AS sequence
  FROM whatever n
 ) n
WHERE n.sequence>= 2 AND n.sequence<= 3;