常见SQL笔试题.docx
- 文档编号:15491313
- 上传时间:2023-07-05
- 格式:DOCX
- 页数:12
- 大小:18.25KB
常见SQL笔试题.docx
《常见SQL笔试题.docx》由会员分享,可在线阅读,更多相关《常见SQL笔试题.docx(12页珍藏版)》请在冰点文库上搜索。
常见SQL笔试题
SQL笔试题
1.统计查询SQL练习
数据库中表结构如下,字段分别任rg(日期),shengfu(胜负),考察groupby语句的使用:
2005-05-09胜
2005-05-09胜
2005-05-09负
2005-05-09负
2005-05-10胜
2005-05-10负
2005-05-10负
如果要生成下列结果,该如何写sql语句?
胜负
2005-05-0922
2005-05-1012
答案:
1)selectrq,sum(casewhenshengfu='胜'then1else0end)'胜',sum(casewhenshengfu='负'
then1else0end)'负'from#tmpgroupbyrq
2)selectN.rq,N.胜,M.负from(
selectrq,胜=count(*)from#tmpwhereshengfu='胜'groupbyrq)Ninnerjoin
(selectrq,负=count(*)from#tmpwhereshengfu='负'groupbyrq)MonN.rq=M.rq
3)selecta.col001,a.a1胜,b.b1负from
(selectcol001,count(col001)a1fromtemp1wherecol002='胜'groupbycol001)a,
(selectcol001,count(col001)b1fromtemp1wherecol002='负'groupbycol001)b
wherea.col001=b.col001
2.条件判断SQL练习
表中有ABC三列,用SQL语句实现:
当A列大于B列时选择A列否则选择B列,
当B列大于C列时选择B列否则选择C列
答案:
select(casewhena>bthenaelsebend),
(casewhenb>cthenbeslecend)
fromtable_name
3.日期统计SQL练习
请取出tb_send表中日期(SendTime字段)为当天的所有记录?
(SendTime字段为
datetime型,包含日期与时间)
答案:
select*fromtbwheredatediff(dd,SendTime,getdate())=0
4.统计查询SQL练习
有一张表,里面有3个字段:
语文,数学,英语。
其中有3条记录分别表示语文70
分,数学80分,英语58分,请用一条sql语句查询出这三条记录并按以下条件显示
出来(并写出您的思路):
大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。
显示格式:
语文数学英语
及格优秀不及格
答案:
select
(casewhen语文>=80then'优秀'
when语文>=60then'及格'
else'不及格')as语文,
(casewhen数学>=80then'优秀'
when数学>=60then'及格'
else'不及格')as数学,
(casewhen英语>=80then'优秀'
when英语>=60then'及格'
else'不及格')as英语,
fromtable
7.请用一个sql语句得出结果,从table1,table2中取出如table3所列格式数据
table1
月份mon部门dep业绩yj
答案:
-------------
一月份0110
一月份0210
一月份035
二月份028
二月份049
三月份038
table2
部门dep部门名称dname
答案:
--------------
01国内业务一部
02国内业务二部
03国内业务三部
04国际业务部
table3(result)
部门dep一月份二月份三月份
答案:
答案:
--
0110nullnull
02108null
03null58
04nullnull9
答案:
1)
selecta.部门名称dname,b.业绩yjas'一月份',c.业绩yjas'二月份',d.业绩yjas'三月份'
fromtable1a,table2b,table2c,table2d
wherea.部门dep=b.部门depandb.月份mon='一月份'and
a.部门dep=c.部门depandc.月份mon='二月份'and
a.部门dep=d.部门depandd.月份mon='三月份'and
2)
selecta.dep,
sum(casewhenb.mon=1thenb.yjelse0end)as'一月份',
sum(casewhenb.mon=2thenb.yjelse0end)as'二月份',
sum(casewhenb.mon=3thenb.yjelse0end)as'三月份',
sum(casewhenb.mon=4thenb.yjelse0end)as'四月份',
sum(casewhenb.mon=5thenb.yjelse0end)as'五月份',
sum(casewhenb.mon=6thenb.yjelse0end)as'六月份',
sum(casewhenb.mon=7thenb.yjelse0end)as'七月份',
sum(casewhenb.mon=8thenb.yjelse0end)as'八月份',
sum(casewhenb.mon=9thenb.yjelse0end)as'九月份',
sum(casewhenb.mon=10thenb.yjelse0end)as'十月份',
sum(casewhenb.mon=11thenb.yjelse0end)as'十一月份',
sum(casewhenb.mon=12thenb.yjelse0end)as'十二月份',
fromtable2aleftjointable1bona.dep=b.dep
8.华为一道面试题
一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。
答案:
selectid,Count(*)fromtbgroupbyidhavingcount(*)>1
select*from(selectcount(ID)ascountfromtablegroupbyID)TwhereT.count>1
9.统计查询SQL练习
用一条SQL语句查询出每门课都大于80分的学生姓名
namekechengfenshu
张三语文81
张三数学75
李四语文76
李四数学90
王五语文81
王五数学100
王五英语90
A:
selectdistinctnamefromtablewherenamenotin(selectdistinctnamefromtable
wherefenshu<=80)
10.常规删除查询SQL练习
表中数据如下:
12005001张三0001数学69
22005002李四0001数学89
32005001张三0001数学69
删除除了自动编号不同,其他都相同的学生冗余信息
A:
deletetablenamewhere自动编号notin(selectmin(自动编号)fromtablenamegroup
by学号,姓名,课程编号,课程名称,分数)
11.行列转换问题
yearmonthamount
199111.1
199121.2
199131.3
199141.4
199212.1
199222.2
199232.3
199242.4
查成这样一个结果
yearm1m2m3m4
19911.11.21.31.4
19922.12.22.32.4
答案一、
selectyear,
(selectamountfromaaamwheremonth=1andm.year=aaa.year)asm1,
(selectamountfromaaamwheremonth=2andm.year=aaa.year)asm2,
(selectamountfromaaamwheremonth=3andm.year=aaa.year)asm3,
(selectamountfromaaamwheremonth=4andm.year=aaa.year)asm4
fromaaagroupbyyear
这个是ORACLE中做的:
select*from(selectname,yearb1,lead(year)over
(partitionbynameorderbyyear)b2,lead(m,2)over(partitionbynameorderbyyear)
b3,rank()over(
partitionbynameorderbyyear)rkfromt)whererk=1;
12.行列转换SQL考核
原表:
courseidcoursenamescore
1java70
2oracle90
3xml40
4jsp30
5servlet80
答案:
为了便于阅读,查询此表后的结果显式如下(及格分数为60):
courseidcoursenamescoremark
答案:
1java70pass
2oracle90pass
3xml40fail
4jsp30fail
5servlet80pass
答案:
答案:
selectcourseid,coursename,score,decode(sign(score-60),-1,'fail','pass')asmarkfromcourse;
13.SQL练习
(1)表名:
购物信息
购物人?
?
?
?
商品名称?
?
?
?
数量
A?
?
?
?
?
?
?
?
甲?
?
?
?
?
?
2
B?
?
?
?
?
?
?
?
乙?
?
?
?
?
?
?
4
C?
?
?
?
?
?
?
?
丙?
?
?
?
?
?
?
1
A?
?
?
?
?
?
?
?
丁?
?
?
?
?
?
2
B?
?
?
?
?
?
?
?
丙?
?
?
?
?
?
5
给出所有购入商品为两种或两种以上的购物人记录
答:
select*from?
购物信息?
where?
购物人?
in(select?
购物人?
from?
购物信息?
groupby?
购物人?
havingcount(*)>=2);
(2)表名:
成绩表
姓名?
?
课程?
?
?
?
?
分数?
张三?
?
?
?
语文?
?
?
?
81
张三?
?
?
?
数学?
?
?
?
75
李四?
?
?
?
语文?
?
?
?
56
李四?
?
?
?
数学?
?
?
?
90
王五?
?
?
?
语文?
?
?
?
81
王五?
?
?
?
数学?
?
?
?
100
王五?
?
?
?
英语?
?
?
?
49
给出成绩全部合格的学生信息(包含姓名、课程、分数),注:
分数在60以上评为合格
答:
select*from?
成绩表?
where?
姓名?
notin(selectdistinct?
姓名?
from?
成绩表?
where?
分数?
<60)
或者:
select*from?
成绩表?
where?
姓名?
in(select?
姓名?
from?
成绩表?
groupby?
姓名?
havingmin(分数)>=60)
(3)表名:
商品表
名称?
?
产地?
?
?
?
?
?
?
?
?
进价
苹果?
?
烟台?
?
?
?
?
?
?
?
2.5
苹果?
?
云南?
?
?
?
?
?
?
?
1.9
苹果?
?
四川?
?
?
?
?
?
?
?
3
西瓜?
?
江西?
?
?
?
?
?
?
?
1.5
西瓜?
?
北京?
?
?
?
?
?
?
?
2.4
给出平均进价在2元以下的商品名称
答:
select?
名称?
from?
商品表?
groupby?
名称?
havingavg(进价)<2
(4)表名:
高考信息表
准考证号?
?
科目?
?
?
?
?
成绩?
2006001?
?
?
?
语文?
?
?
?
119
2006001?
?
?
?
数学?
?
?
?
108
2006002?
?
?
?
物理?
?
?
?
142
2006001?
?
?
?
化学?
?
?
?
136
2006001?
?
?
?
物理?
?
?
?
127
2006002?
?
?
?
数学?
?
?
?
149
2006002?
?
?
?
英语?
?
?
?
110
2006002?
?
?
?
?
?
?
?
语文?
?
?
?
105
2006001?
?
?
?
?
?
?
?
英语?
?
?
?
?
?
98
2006002?
?
?
?
化学?
?
?
?
129
给出高考总分在600以上的学生准考证号
答:
select?
准考证号?
from?
高考信息表?
groupby?
准考证号?
havingsum(成绩)>600
(5)表名:
高考信息表
准考证号?
?
?
?
?
数学?
?
?
?
?
语文?
?
?
?
?
英语?
?
?
?
?
物理?
?
?
?
?
化学
2006001?
?
?
?
?
?
?
?
?
?
108?
?
?
?
?
?
119?
?
?
?
?
?
98?
?
?
?
127?
?
?
?
?
?
136
2006002?
?
?
?
?
?
?
?
?
?
149?
?
?
?
?
?
105?
?
?
?
110?
?
?
?
142?
?
?
?
?
?
129
给出高考总分在600以上的学生准考证号
答:
select?
准考证号?
from?
高考信息表?
where(数学+语文+英语+物理+化学)>600
(6)表名:
club
idgenderage
67M?
?
?
?
19
68F?
?
?
?
30
69F?
?
?
?
27
70F?
?
?
?
16
71M?
?
?
?
32
查询出该俱乐部里男性会员和女性会员的总数
答:
selectgender,count(id)fromclubgroupbygender
(7)表名:
team
ID(number型)Name(varchar2型)
1?
?
?
?
?
?
?
?
?
?
a
2?
?
?
?
?
?
?
?
?
?
b
3?
?
?
?
?
?
?
?
?
?
b
4?
?
?
?
?
?
?
?
?
?
a
5?
?
?
?
?
?
?
?
?
?
c
6?
?
?
?
?
?
?
?
?
?
c
要求:
执行一个删除语句,当Name列上有相同时,只保留ID这列上值小的
例如:
删除后的结果应如下:
ID(number型)Name(varchar2型)
1?
?
?
?
?
?
?
?
?
?
a
2?
?
?
?
?
?
?
?
?
?
b
5?
?
?
?
?
?
?
?
?
?
c
请写出SQL语句。
deletefromteamwhereidnotin(selectmin(id)fromteamgroupbyname)
(8)表名:
student
namecoursescore
张青语文?
?
?
?
72
王华数学?
?
?
?
72
张华英语?
?
?
?
81
张青物理?
?
?
?
67
李立化学?
?
?
?
98
张燕物理?
?
?
?
70
张青化学?
?
?
?
76
查询出“张”姓学生中平均成绩大于75分的学生信息
答:
select*fromstudentwherenamein(selectnamefromstudent
wherenamelike'张%'groupbynamehavingavg(score)>75)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 常见 SQL 笔试