Search This Blog

Showing posts with label ms sql. Show all posts
Showing posts with label ms sql. Show all posts

Friday, January 7, 2011

Select a range from oracle, mysql

I talked about how to select first/top n rows from oracle, mysql, and ms sql server. How do we get the range, say from m to n, where m < n?

Oracle
select id, age from (select id, age, rownum as rn from customer order by age) where rn between :m and :n
MySql
select * from customer order by age limit :m, :n - :m
MS SQL

I don't know how to do it with MS sql server yet. I don't have ms sql server installed. If you happen to know it, please post your solution in the comment.

Tuesday, December 28, 2010

Select first n rows from oracle, MySql and MS Sql

Assume we have table customer with columns id and age. Find first n customers with youngest ages

Oracle
select * from (select * from customer order by age) where rownum < :n
Mysql
select * from customer order by age limit :n
MS Sql
select top :n * from customer order by age