-4

I have written sql query

   Select * from Products WHERE Products.Category=="Fruits";

the returned answer is

  1-Banana
  2-Mango
  3-Apple
  4-Oranges
  5-Grapes

now i want to select fruit at index 3rd only that is Apple using sql indexes the problem arises every where how should i select the element at 3rd place using its index in sql query?

3
  • Syntax error. Which dbms product are you using? Both MySQL and SQL Server??? Commented Jun 4, 2015 at 8:34
  • SQL SERVER 2012 but i want only 3rd number limit shall give me 2 values Commented Jun 4, 2015 at 8:40
  • Welcome to stackoverflow. Please read How to Ask. I've edited your question to remove irrelevant tags. Commented Jun 4, 2015 at 8:46

2 Answers 2

2

Sql server 2012 supports offset and fetch, so your query should look like this:

SELECT * 
FROM Products 
WHERE Products.Category ='Fruits'
ORDER BY Products.Category -- or whatever column you need the sort on
OFFSET 3 ROWS FETCH NEXT 1 ROW ONLY; 
Sign up to request clarification or add additional context in comments.

2 Comments

May be OFFSET 2 ROWS FETCH NEXT 1 ROW ONLY ?
Oops, correct. fixed it.
1

You can use CTE for finding nth place record

 with cte as
   (
     select ROW_NUMBER() over (order by Salary desc) as r, * 
     from Products 
     WHERE Products.Category=="Fruits" e
   )
  select * from cte where r=3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.