1.张表,学生表S,课程C,学生课程表SC,学生可以选修多门课程,一门课程可以被多个学生选修,通过SC表关联;(SQL)1)写出建表语句;答:建表语句如下(mysql数据库):create table s(id integer primary key, name varchar(20));create table c(id integer primary key, name varchar(20));create table sc(sid integer references s(id),cid integer references c(id),primary key(sid,cid));2)写出SQL语句,查询选修了所有选修课程的学生;答:SQL语句如下:select , from s stuwhere (select count(*) from sc where sid= =(select count(*) from c);3)写出SQL语句,查询选修了至少5门以上的课程的学生。
答:SQL语句如下:select , from s stuwhere (select count(*) from sc where sid=>=5;2.数据库表(Test)结构如下:(SQL)IDNAMEAGEMANAGER(所属主管人ID)106A30104109B19104104C20111107D35109112E25120119F45NULL要求:列出所有年龄比所属主管年龄大的人的ID和名字答:SQL语句如下:select from test employee where >(select from test manager where =;3.有3个表(15分钟):(SQL)Student 学生表 (学号,姓名,性别,年龄,组织部门)Course 课程表 (编号,课程名称)Sc 选课表 (学号,课程编号,成绩)表结构如下:1)写一个SQL语句,查询选修了’计算机原理’的学生学号和姓名(3分钟)答:SQL语句如下:select , from Student stuwhere (select count(*) from sc where sno= and cno =(select cno from Course where cname=’计算机原理’)) != 0;2)写一个SQL语句,查询’周星驰’同学选修了的课程名字(3分钟)答:SQL语句如下:select cname from Course where cno in (select cno from sc where sno=(select sno from Student where sname=’周星驰’));3)写一个SQL语句,查询选修了5门课程的学生学号和姓名(9分钟)答:SQL语句如下:select , from student stuwhere (select count(*) from sc where sno= = 5;小小+霸霸+王王=小霸王小=,霸=,王=用sql求证参考答案:declare @data int,@i int,@j int,@l int set @data=100while (@data<=999)beginset @i=@data/100set @j=@data/10 % 10set @l=@data % 10if((@i+@j+@l)*11=@data)beginSelect @data data,@i i,@j j,@l l breakendset @data=@data+1end;分析:II+JJ+LL=IJLI*10+I +J*10+J+L*10+L = I*100+J*10+L (I+J+L)*111. 用一条SQL 语句查询出每门课都大于80 分的学生姓名name kecheng fenshu张三语文 81张三数学 75李四语文 76李四数学 90王五语文 81王五数学 100王五英语 90A: select distinct name from table where name not in (select distinct name from table where fenshu<=80)B: select name from table group by name having min(fenshu)>802. 一个叫 depart 的表,里面只有一个字段team, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合.你先按你自己的想法做一下,看结果有我的这个简单吗create table depart(team varchar(50) not null)答:select , from depart a, depart b where <3. 请用SQL 语句实现:从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。
请注意:TestDB 中有很多科目,都有1 -12 月份的发生额。
AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额。
数据库名:JcyAudit ,数据集:Select * from TestDB答:select a.*from TestDB a,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) bwhere = and >4.说明:复制表( 只复制结构, 源表名:a 新表名:b)SQL: select * into b from a where 1<>1 (where1=1,拷贝表结构和数据内容) ORACLE:create table bAsSelect * from a where 1=2[<>(不等于)(SQL Server Compact)比较两个表达式。
当使用此运算符比较非空表达式时,如果左操作数不等于右操作数,则结果为 TRUE。
否则,结果为 FALSE。
]5. 说明:拷贝表( 拷贝数据, 源表名:a 目标表名:b)SQL: insert into b(a, b, c) select d,e,f from a;6.说明:两张关联表,删除主表中已经在副表中没有的信息SQL:Delete from info where not exists(select * from infobz where = )7.有两个表A 和B ,均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value 这道题的SQL 语句怎么写update b set =(select from a where = where in(select from b,a where =;8.为了便于阅读, 查询此表后的结果显式如下( 及格分数为60):courseid coursename score mark---------------------------------------------------1 java 70 pass2 oracle 90 pass3 xml 40 fail4 jsp 30 fail5 servlet 80 pass---------------------------------------------------写出此查询语句select courseid, coursename ,score ,decode (sign(score-60),-1,'fail','pass') as mark from course'decode'貌似在我的SQLSERVER2005里无法识别,可用以下方法替代select courseid,coursename,score,(case when (score-60)>=0 then 'pass' else 'fail' end) as markfrom course******************************************************************************* *****************sql面试题(2)有表A,结构如下:A: p_ID p_Num s_id1 10 011 12 022 8 013 11 013 8 03其中:p_ID为产品ID,p_Num为产品库存量,s_id为仓库ID。
请用SQL语句实现将上表中的数据合并,合并后的数据为:p_ID s1_id s2_id s3_id1 10 12 02 8 0 03 11 0 8其中:s1_id为仓库1的库存量,s2_id为仓库2的库存量,s3_id为仓库3的库存量。
如果该产品在某仓库中无库存量,那么就是0代替。
结果:select p_id ,sum(case when s_id=1 then p_num else 0 end) as s1_id,sum(case when s_id=2 then p_num else 0 end) as s2_id,sum(case when s_id=3 then p_num else 0 end) as s3_idfrom myPro group by p_id7。
为管理业务培训信息,建立3个表:S(S#,SN,SD,SA)S#,SN,SD,SA分别代表学号,学员姓名,所属单位,学员年龄C(C#,CN)C#,CN分别代表课程编号,课程名称SC(S#,C#,G) S#,C#,G分别代表学号,所选的课程编号,学习成绩(1)使用标准SQL嵌套语句查询选修课程名称为’税收基础’的学员学号和姓名答案:select s# ,sn from s where S# in(select S# from c,sc where #=# and cn=’税收基础’)(2) 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位答:select sn,sd from s,sc where #=# and #=’c2’(3) 使用标准SQL嵌套语句查询不选修课程编号为’C5’的学员姓名和所属单位答:select sn,sd from s where s# not in(select s# from sc where c#=’c5’)(4)查询选修了课程的学员人数答:select 学员人数=count(distinct s#) from sc(5) 查询选修课程超过5门的学员学号和所属单位答:select sn,sd from s where s# in(select s# from sc group by s# having count(distinct c#)>5)SQL面试题(4)1.查询A(ID,Name)表中第31至40条记录,ID作为主键可能是不是连续增长的列,完整的查询语句如下:select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A2.查询表A中存在ID重复三次以上的记录,完整的查询语句如下:select * from(select count(ID) as count from table group by ID)T where >3SQL面试题(3)1 .触发器的作用答:触发器是一中特殊的存储过程,主要是通过事件来触发而被执行的。