学生S(S#,SNAME,AGE,SEX)
学习SC(S#,C#,GRADE)
课程C(C#,CNAME,TEACHER)
检索学号比王钢大,而年龄比他小的学生姓名。
这个sql怎么写

解决方案 »

  1.   

    --这样?
    create table s(s# int,sname varchar(10),age int,sex int)
    insert into s select 1,'aa',18,1
    insert into s select 2,'nn',22,1
    insert into s select 3,'王钢',19,1
    insert into s select 4,'bb',10,1
    insert into s select 5,'ee',30,1
    insert into s select 6,'ff',11,1
    insert into s select 7,'tt',22,1
    insert into s select 8,'bbf',23,1select sname from s a where exists(select 1 from s where s#<a.s# and age>a.age and sname='王钢') drop table s
      

  2.   

    select sname from s where s#>(select s# from s where sname='王钢') and age<(select age from s where sname='王钢')
      

  3.   

    学生S(S#,SNAME,AGE,SEX)
    学习SC(S#,C#,GRADE)
    课程C(C#,CNAME,TEACHER)检索学号比王钢大,而年龄比他小的学生姓名。
    select S.SName from S
    inner join SC
    on S.S#=SC.S#
    where S.S#>(select top 1 S# from S where SName='王钢')
    and SC.Grade<
    (select top 1 SC.Grade from S
    inner join SC
    on S.S#=SC.S# 
    where S.Sname='王钢'
    )
      

  4.   

    哈哈来个最土的
    declare @S# varchar(50)
    Declare @Age intselect @S#=S# ,@Age=age from 学生S where Sname='王刚'
    select * from 学生S where S#>@S# and age<@age