sql a 表 若包含b表 则a 表 列显示_几道常见的SQL面试题,看你能答对几道?

acaa8a781336b4f6ae1007e35d29bba5.png

分享几道比较常见的SQL面试题,在不看底部参考答案的情况下,看自己能做对几道。

1.用一条SQL 语句 查询出每门课都大于80 分的学生姓名

c743fcc454b1ac9830625c3829c763ad.png

2. 学生表 如下:

ba88502386d892d3c83f63959455b4ee.png

删除除了自动编号不同, 其他都相同的学生冗余信息

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

4.请用SQL 语句实现:从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目。请注意:TestDB 中有很多科目,都有1 -12 月份的发生额。
AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额。
数据库名:JcyAudit ,数据集:Select * from TestDB

5.面试题:怎么把这样一个表

ef47a8b0935ef46dc392457e83288f92.png

查成这样一个结果

e4b76239ba3ce89df428b4456c5cffc9.png

6. 说明:复制表( 只复制结构, 源表名:a新表名:b)

7. 说明:拷贝表( 拷贝数据, 源表名:a目标表名:b)

8. 说明:显示文章、提交人和最后回复时间

9. 说明:外连接查询( 表名1 :a表名2 :b)

10. 说明:日程安排提前五分钟提醒

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

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

13.表tab内容:

7a6f8dc7c5045808e8821bf37f091f67.png

如果要生成下列结果, 该如何写sql语句?

0530ceb2304844f2df840eaad2ad9b69.png

14.表中有A B C三列,用SQL语句实现:当A列大于B列时选择A列否则选择B列,当B列大于C列时选择B列否则选择C列。

15.请取出tb_send表中日期(SendTime字段)为当天的所有记录?

(SendTime字段为datetime型,包含日期与时间)

16.有一张表table,里面有3个字段:语文,数学,英语。其中有3条记录

05b1edceab31aa63b06a8371bb9905bc.png

请用一条sql语句查询出这三条记录并按以下条件显示出来(并写出您的思路): 大于或等于80表示优秀,大于或等于60表示及格,小于60分表示不及格。 显示格式:

3114a723f5feb0ec2337318d3ead7057.png

17.从table1,table2中取出如table3所列格式数据,其中table1只展示了部分数据,还有4到12月份的数据未完全展示,table3也只展示了部分数据,还有4到12月份的数据列未完全展示。

table1

2ef5a0cf5fb05a5c6cbbdfd034af1eb3.png

table2

086dfe21f98a4ce4d5e60bf0c07b1c55.png

结果如下:

table3

18cd9be3ac4a0d1a1bce0c83cf68b857.png

18.一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数。

19.表形式如下:

dd6c27120c0066dcf9b7f9998acc6b31.png

想得到如下形式的查询结果

33f572fc87769b617bd46e0d307d355d.png

sql语句怎么写?


参考答案

1、

--方法一:
select distinct name 
from table 
where name not in (
select distinct name f
rom table where fenshu<=80
)
--方法二:
select name from table 
group by name 
having min(fenshu)>80

2、

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

3、

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

4、

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、

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

6、

--SQL:
select * into b from a where 1<>1

--ORACLE:
create table b
As
Select * from a where 1=2

注:<>(不等于)(SQL Server Compact)

比较两个表达式。当使用此运算符比较非空表达式时,如果左操作数不等于右操作数,则结果为 TRUE。否则,结果为 FALSE。

7、

insert into b(a, b, c)
select d,e,f from a;

8、

select a.title,a.username,b.adddate
from table a,(
select max(adddate) adddate
from table where table.title=a.title
) b

9、

--SQL Server:
select a.a, a.b, a.c, b.c, b.d, b.f
from a LEFT OUTER JOIN b ON a.a = b.c

--ORACLE:
select a.a, a.b, a.c, b.c, b.d, b.f from a ,b
where a.a = b.c(+)

10、

--SQL Server
select * from 日程安排
where datediff('minute',开始时间,getdate())>5

11、

--SQL Server:
Delete from info
where not exists (
select * from infobz
where info.infid=infobz.infid
)

12、

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);

13

create table tmp(Tdate varchar(10),Tresulte nchar(1));

insert into tmp values('2019-05-09','胜');
insert into tmp values('2019-05-09','胜');
insert into tmp values('2019-05-09','负');
insert into tmp values('2019-05-09','负');
insert into tmp values('2019-05-10','胜');
insert into tmp values('2019-05-10','负');
insert into tmp values('2019-05-10','负');

--方法一
select Tdate '日期',
sum(case when Tresulte ='胜' then 1 else 0 end)'胜',
sum(case when Tresulte ='负' then 1 else 0 end)'负' 
from tmp group by Tdate ;
--方法二
select N.Tdate '日期',N.勝,M.負
from (
select Tdate ,count(*) '胜' from tmp where Tresulte ='胜' group by Tdate ) N
inner join
(
select Tdate ,count(*) '负' from tmp where Tresulte ='负' group by Tdate ) M
on N.Tdate =M.Tdate ;

14

select 
(case when a>b then a else b end ),
(case when b>c then b esle c end)
from table_name

15

select * from tb
where datediff(dd,SendTime,getdate())=0

16

select
(case when 语文>=80 then '优秀'
      when 语文>=60 then '及格'
      else '不及格') as 语文,
(case when 数学>=80 then '优秀'
      when 数学>=60 then '及格'
      else '不及格') as 数学,
(case when 英语>=80 then '优秀'
      when 英语>=60 then '及格'
      else '不及格') as 英语,
from table

17

select a.dep,
sum(case when b.mon=1 then b.yj else 0 end) as '一月份',
sum(case when b.mon=2 then b.yj else 0 end) as '二月份',
sum(case when b.mon=3 then b.yj else 0 end) as '三月份',
sum(case when b.mon=4 then b.yj else 0 end) as '四月份',
sum(case when b.mon=5 then b.yj else 0 end) as '五月份',
sum(case when b.mon=6 then b.yj else 0 end) as '六月份',
sum(case when b.mon=7 then b.yj else 0 end) as '七月份',
sum(case when b.mon=8 then b.yj else 0 end) as '八月份',
sum(case when b.mon=9 then b.yj else 0 end) as '九月份',
sum(case when b.mon=10 then b.yj else 0 end) as '十月份',
sum(case when b.mon=11 then b.yj else 0 end) as '十一月份',
sum(case when b.mon=12 then b.yj else 0 end) as '十二月份',
from table2 a left join table1 b on a.dep=b.dep

18

--方法一
select id,Count(*)
from tb
group by id 
having count(*)>1;
--方法二
select * from
(select count(ID) as count 
from table 
group by ID)T
where T.count>1

19

--连接查询
SELECT b.YEAR, SUM(a.salary) salary
FROM hello a, hello b
WHERE a.YEAR <= b.YEAR GROUP BY b.YEAR

--子查询
select year ,
(select sum(salary)
from hello as B
where B.year<=A.year )
from hello as A

d43973566ca4caacc1cac9e74c543793.png

觉得不错,记得帮忙点个赞,谢谢啦~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值