开发者

sql query for fetching 10 rows with near values

开发者 https://www.devze.com 2022-12-24 17:51 出处:网络
Please help me with writing开发者_开发技巧 a sql query - I have a table with id, name and marks columns.

Please help me with writing开发者_开发技巧 a sql query - I have a table with id, name and marks columns.

If I arrange the table in ascending order of marks, how can I fetch 5 names whose marks are close to a particular name.


Something like this should do it:

select id, name, marks
from Marks
where name <> 'User1'
order by abs(marks - (select marks from Marks where name = 'User1')) 
limit 5


Microsoft SQL Server

SELECT TOP 10 column FROM table

PostgreSQL and MySQL

SELECT column FROM table 
LIMIT 10

Oracle

SELECT column FROM table
WHERE ROWNUM >= 3 AND ROWNUM < 10

DB2

SELECT column FROM table
FETCH FIRST 10 ROWS


Something like this:

select * 
from marks m 
order by abs(m.mark - (select m2.mark from marks m2 where m2.name = "John Doe"))
limit 5; 
0

精彩评论

暂无评论...
验证码 换一张
取 消