-1

How can I use offset clause to get all rows except the first row. I am using query like

SELECT * FROM EMPLOYEES ORDER BY SALARY OFFSET 1;

But this is not working in MySQL. what am I doing wrong here?

Thom A
  • 88,727
  • 11
  • 45
  • 75
Himanshu
  • 13
  • 1

3 Answers3

1

Sadly in MySQL OFFSET only works together with the LIMIT clause. So you need to use

SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 18446744073709551615 OFFSET 1;

or

SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 1, 18446744073709551615;

I have chosen that limit number from a different question

jonii
  • 173
  • 1
  • 6
1

MySQL 8.0 can use a window function to do this:

SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER (ORDER BY SALARY) AS rownum
  FROM EMPLOYEES
) AS t
WHERE rownum > 1
ORDER BY SALARY;
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
0

You have to use LIMIT in addition to OFFSET

SELECT * FROM EMPLOYEES ORDER BY SALARY LIMIT 9999 OFFSET 1;

Chris B
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 14 '22 at 01:23