阿里baba的面试题
有三个表
学生表 S
SID SNAME
教师课表 T
TID TNAME TCL
成绩表 SC
SID TCL SCR
各字段的含义不用我标明了吧,大侠哥哥么!呵呵
现在要求写SQL查询
问1、选修了A、B课程,并且A课程的成绩大于B成绩的学生姓名?
问2、没有选修‘li’老师的课程的学生,要求不能用in,exists 等词?
有三个表
学生表 S
SID SNAME
教师课表 T
TID TNAME TCL
成绩表 SC
SID TCL SCR
各字段的含义不用我标明了吧,大侠哥哥么!呵呵
现在要求写SQL查询
问1、选修了A、B课程,并且A课程的成绩大于B成绩的学生姓名?
问2、没有选修‘li’老师的课程的学生,要求不能用in,exists 等词?
select a.sname
from
s a,
(select * from sc where tcl='A') b,
(select * from sc where tcl='B') c
where
a.sid=b.sid and b.sid=c.sid
and
b.scr>c.scr
from
s,
(select sc.sid from sc,t where sc.tcl=t.tcl and t.tname='li') b
where
s.sid!=b.sid
select s.sname
from
s,
(select sc.sid from sc,t where sc.tcl=t.tcl and t.tname='li') b
where
s.sid!=b.sid
第二题这样行吗? 第一次用APPLY, 学习INGSELECT S.SNAME
FROM S INNER JOIN SC ON S.SID = SC.SID
CROSS APPLY (
SELECT TNAME FROM T WHERE T.TCL = SC.TCL AND TNAME<>'LI'
) P
-- Author: T.O.P
-- Create date: 2009/11/28
-- Version: SQL SERVER 2005
-- =============================================
if object_id('[S]') is not null drop table [S]
go
create table [S]([SID] int,[SNAME] varchar(6))
insert [S]
select 1,'张三' union all
select 2,'草泥马' union all
select 3,'奥巴马' union all
select 4,'涛哥'if object_id('[T]') is not null drop table [T]
go
create table [T]([TID] int,[TNAME] varchar(4),[TCL] varchar(4))
insert [T]([TID], [TCL], [TNAME])
select 1,'数学','li' union all
select 2,'语文','wang' union all
select 3,'英语','kay' union all
select 4,'医学','lion' union all
select 5,'化学','woo'if object_id('[SC]') is not null drop table [SC]
go
create table [SC]([SID] int,[TCL] varchar(4),[SCR] int)
insert [SC]
select 1,'数学',15 union all
select 2,'语文',30 union all
select 3,'医学',20 union all
select 4,'语文',99 union all
select 4,'化学',89 union all
select 3,'语文',79 union all
select 3,'化学',89--选修了A、B课程,并且语文课程的成绩大于化学成绩的学生姓名?
SELECT S.SNAME
FROM S INNER JOIN SC A ON S.SID = A.SID AND A.[TCL]='语文'
INNER JOIN SC B ON S.SID = B.SID AND B.[TCL]='化学'
WHERE A.SCR>B.SCR--测试结果:
/*
SNAME
------
涛哥*/--没有选修‘li’老师的课程的学生,要求不能用in,exists 等词
SELECT distinct S.SNAME
FROM S INNER JOIN SC ON S.SID = SC.SID
CROSS APPLY (
SELECT TNAME FROM T WHERE T.TCL = SC.TCL AND TNAME<>'LI'
) P--测试结果:
/*
SNAME
------
奥巴马
草泥马
涛哥*/drop table s
drop table [T]
drop table [SC]