sql题--总结

1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名
name kecheng fenshu
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90

1  select distinct name from table where name not in (select distinct name from table where fenshu<=80)
2 select name from table group by name having min(fenshu)>80

2、删除除了自动编号不同, 其他都相同的学生冗余信息
学生表 如下:
自动编号 学号 姓名 课程编号 课程名称 分数
1 2005001 张三 0001 数学 69
2 2005002 李四 0001 数学 89
3 2005001 张三 0001 数学 69

delete tablename where 自动编号 not in(select min( 自动编号) from tablename group by学号, 姓名, 课程编号, 课程名称, 分数)

3、一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,用一条sql 语句显示所有可能的比赛组合.
你先按你自己的想法做一下,看结果有我的这个简单吗?

select a.name, b.name
from team a, team b 
where a.name < b.name

4、请用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) b
where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur

5、面试题:怎么把这样一个表儿 (列转行)
year month amount
1991 1 1.1
1991 2 1.2
1991 3 1.3
1991 4 1.4
1992 1 2.1
1992 2 2.2
1992 3 2.3
1992 4 2.4
查成这样一个结果
year m1 m2 m3 m4
1991 1.1 1.2 1.3 1.4
1992 2.1 2.2 2.3 2.4

答案一、二

一、
select year, 
(select amount from   aaa m where month=1   and m.year=aaa.year) as m1,
(select amount from   aaa m where month=2   and m.year=aaa.year) as m2,
(select amount from   aaa m where month=3   and m.year=aaa.year) as m3,
(select amount from   aaa m where month=4   and m.year=aaa.year) as m4
from aaa   group by year
二、
select year,
sum(case when month = 1 then amount  else 0 end) as m1,
sum(case when month = 2 then amount  else 0 end) as m2,
sum(case when month = 3 then amount  else 0 end) as m3,
sum(case when month = 4 then amount  else 0 end) as m4
from aaa group by year

6、拷贝表( 拷贝数据, 源表名:year_amount 目标表名:year_amount_1)

insert into year_amount_1(year_amount_1.`year`, `month`,year_amount_1.amount ) select year_amount.`year`,year_amount.`month`,year_amount.amount from year_amount; 

7、11. 说明:两张关联表,删除主表中已经在副表中没有的信息

Delete from info where not exists (select * from infobz where info.infid=infobz.infid )

8、有两个表A 和B ,均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value
这道题的SQL 语句怎么写?

update b set b.value=(select a.value from a where a.key=b.key) where b.id in(select b.id from b,a where b.key=a.key);

9、部门表和员工表关联,查出员工所在部门
/*
用一条SQL语句,怎么显示如下结果
id dptID department name
1 1 设计 张三
2 1 设计 李四
3 2 市场 王五
4 3 售后 彭六
5 4 黑人 陈七
*/

create table testtable1
(
id int NOT NULL AUTO_INCREMENT,
department varchar(12) ,
PRIMARY KEY (id)
)

select * from testtable1
insert into testtable1 (department) values('设计');
insert into testtable1 (department) values('市场');
insert into testtable1 (department) values('售后');
/*
结果
id department
1   设计
2   市场
3   售后 
*/
create table testtable2
(
id int NOT NULL AUTO_INCREMENT,
dptID int,
name varchar(12),
PRIMARY KEY (id)
);
insert into testtable2 (dptID,name ) values(1,'张三');
insert into testtable2 (dptID,name  ) values(1,'李四');
insert into testtable2 (dptID,name  ) values(2,'王五');
insert into testtable2 (dptID,name  ) values(3,'彭六');
insert into testtable2 (dptID,name  ) values(4,'陈七');

答案:
select t2.*, coalesce(t1.department,'黑人') as department from testtable1 t1  right join testtable2 t2 on t1.id = t2.dptID

10、管理业务培训信息,建立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 c.c#=sc.c# and cn=’税收基础’)

(2) 使用标准SQL嵌套语句查询选修课程编号为’C2’的学员姓名和所属单位?

答:select sn,sd from s,sc where s.s#=sc.s# and sc.c#=’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)

11、求出每个部门的每个月哪个员工的薪水最高
部门表 department
字段 id department_id name month salary

select department_id,name,month max(salary) from department group by department_id,name,month  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值