文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
数据库基础知识测试
第一题:表名: Score1, 具体记录如下:
PERSON | OUT |
---|---|
A | 胜 |
A | 胜 |
A | 负 |
A | 负 |
B | 胜 |
B | 负 |
B | 负 |
(1) 请用SQL语句统计A,B胜,负次数
第1种方法
select PERSON,
count(case OUT
when '胜' then
1
end) as 胜的次数,
count(case OUT
when '负' then
1
end) as 负的次数
from Score1
group by PERSON;
第2种方法
select PERSON,
count(case OUT
when '胜' then
1
end) as 胜的次数,
count(case OUT
when '负' then
1
end) as 负的次数
from Score1
group by PERSON;
(2)请用SQL语句找出A,B胜,负次数大于1的记录
select PERSON,OUT from Score1 group by PERSON,OUT having count(*)>1;
第二题: 表名:Team,储存了四个球队,分别为a,b,c,d 现在四个球队进行比赛,用一条SQL语句显示所有可能的比赛组合
NAME |
---|
a |
b |
c |
d |
select a.Name
, b.Name
from Team a, Team b
where a.Name < b.Name;
第三题:为丰富员工生活,公司在每周分别开设文艺班和健身班,以下是上周两个班开设课程及员工上课情况:
文艺班:表名: tablename_RX
时间(time) | 课程(Class) | 上课员工(Empno) |
---|---|---|
星期一 | A | 001 |
星期二 | B | 002 |
星期三 | C | 003 |
星期四 | D | 004 |
健身班:表名: tablename_HX
时间(time) | 课程(Class) | 上课员工(Empno) |
---|---|---|
星期一 | E | 002 |
星期二 | F | 005 |
星期三 | G | 001 |
星期四 | H | 003 |
(1) 用SQL语句统计上周参加两个班的员工
第1种方法
select rx.Empno
from tablename_RX rx, tablename_HX hx
where rx.Empno = hx.Empno;
第2种方法
select rx.Empno
from tablename_RX rx inner join tablename_HX hx
on rx.Empno = hx.Empno;
第3种方法
select rx.Empno
from tablename_Rx rx
where Exists
(select * from tablename_HX hx
where rx.Empno = hx.Empno)
(2) 用SQL语句统计上周只参加一个班员工
第1种方法
(select Empno from tablename_RX
union
select Empno from tablename_HX)
minus
(select tablename_RX.Empno
from tablename_RX, tablename_HX
where tablename_RX.Empno = tablename_HX.Empno);
第2种方法
select rx.Empno
from tablename_Rx rx
where not Exists
(select * from tablename_HX hx
where rx.Empno = hx.Empno)
union
select hx.Empno
from tablename_HX hx
where not Exists
(select * from tablename_Rx rx
where rx.Empno = hx.Empno)
(3)按照员工统计上周上课次数
第1种方法
select EMPNO, count(*)
from (select EMPNO, count(*)
from tablename_Rx
group by EMPNO
union all
select EMPNO, count(*) from tablename_HX group by EMPNO)
group by EMPNO order by EMPNO;
第2种方法
select a.empno, count(a.empno)
from (select *
from tablename_RX
union
select * from tablename_HX) a
group by EMPNO
order by EMPNO;
第四题:表名:Score,记录如下:
ST_NO | Chinese | Math | English |
---|---|---|---|
001 | 70 | 80 | 58 |
(1)用SQL语句输出如下结果(大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格):
ST_NO | Chinese | Math | English |
---|---|---|---|
001 | 及格 | 优秀 | 不及格 |
select ST_NO,(case
when Chinese >= 80 then
'优秀'
when Chinese >= 60 then
'及格'
else
'不及格'
end) as Chinese,
(case
when Math >= 80 then
'优秀'
when Math >= 60 then
'及格'
else
'不及格'
end) as Math,
(case
when English >= 80 then
'优秀'
when English >= 60 then
'及格'
else
'不及格'
end) as English
from tablename_Score;
(2) 针对001学生输出Chinese,Math,English中分数最高的课程名称以及分数
select 课程, 成绩
from (select ST_NO, 'Chinese' 课程, Chinese 成绩
from tablename_Score
union all
select ST_NO, 'Math', Math 成绩
from tablename_Score
union all
select ST_NO, 'English' 课程, English 成绩
from tablename_Score)
where 成绩=
(select max(成绩)from (
select ST_NO, 'Chinese' 课程, Chinese 成绩
from tablename_Score
union all
select ST_NO, 'Math', Math 成绩
from tablename_Score
union all
select ST_NO, 'English' 课程, English 成绩
from tablename_Score
));
第五题:用SQL语句写出查询某年某月的最后一天的日期
//select last_day(to_date('202207','yyyymm' ))from dual;
select last_day(to_date(sysdate))from dual;