ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 一道sql面试题的解答

一道sql面试题的解答

原创 Linux操作系统 作者:iSQlServer 时间:2010-06-25 09:03:26 0 删除 编辑

题目:

  写出一条Sql语句:

    取出表A中第31到第40记录(SQLServer, 以自动增长的ID作为主键,  注意:ID可能不是连续的。)

 

解答(已测试):

  1、假设ID是连续的:

    select top 10 * from A where ID not in (select top 30 ID from A)

  或

    select  *  from A where ID between 31 and 40

 

  2、假设ID是不连续的:

     select top 40 * from A except select top 30 * from A

  或

    select top 10 * from A where ID > (select max(ID) from A where ID in (select top 30 ID from A))

     或

    select top 10 * from A where ID not in (select top 30 ID from A) 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/16436858/viewspace-666201/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-10-17

  • 博文量
    1319
  • 访问量
    2342867