0

I have table containing data for users, I need to get only last 2 users after first user ex

id-username
1-john
2-fredrek
3-sara
4-sarah

I need to get fredrek, sara - how do I do this in SQL Server ??

I know to do that with MySQL I use LIMIT1,2 but with SQL Server I can't

1

5 Answers 5

2

To avoid 2 x TOP you can use ROW_NUMBER (note: you can't use ROW_NUMBER output directly in the WHERE clause of a single statement )

;WITH cRN AS
(
  SELECT
     ROW_NUMBER() OVER (ORDER BY id) AS rn,
     username
  FROM
     mytable
)
SELECT username
FROM cRN
WHERE rn BETWEEN 2 AND 3
Sign up to request clarification or add additional context in comments.

2 Comments

This is the easy solution. There is no LIMIT feature in SQL Server (until next version).
and i can use this way with php mssql_query();
1

You can use TOP, restricting in the WEHRE clause, not to take the first register

SELECT TOP 2 *
FROM MyTable
WHERE Id > 1

Comments

0

With SQL Server, you can use the TOP command.

Comments

0

You have to make a dual query:

   select * from ( select top 10 * from (
    select top 20 * from table_name order by 1 asc 
    ) as tb_name order by 1 desc
    ) as tb_name_last 
    order by whathever;

Comments

0

You can use TOP in T-SQL:

SELECT TOP 2 FROM table_name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.