Saturday 17 November 2012

top query in sql server | select top 1*


How to use and write top query in sql server | My sql | Oracle
top query will be same in every database but  thing is that to know about table name and field of particular table.


select * from stud 

1 dev
2 deva
4 deva
5 deva
6 deva
8 "dsouza"
8 "d-souza"


Now I want to select one row using top .
select top 1* from stud

1 dev


Now I want to select last record using top.
select top 1* from stud order by name
8 "d-souza"

now its time to work something real means if i want to select 5th record in table where
there is  more than 100 or 8 record.
so , we have to use subquery for that and  create one  Instance table


SELECT TOP 1  FROM
(SELECT TOP 5 * FROM stud ORDER BY id ASC) AS LAST
ORDER BY id DESC

6 deva

what i did over here step first ..
i wrote top 5 * query so it will select top 5 records

SELECT TOP 5 * FROM stud ORDER BY id ASC
1 dev
2 deva
4 deva
5 deva
6 deva

i will get output like this.
now after that i want 5th record so i did top 1 * from table by creating  instance of table AS LAST.
using top you can select  5th record from  bottom or top.but you should know about syntax else it will give error .
Share This
Previous Post
Next Post

FYJC XI standard online admisson Process and declaraton of Merit list . Cut off List For prevous year also . 10 Th Results onlne declaraton Maharashtra Region .

0 comments: