数据库经典笔试题
一、分享背景:? ? ? ??
????????由于最近測試這個(gè)行業(yè),內(nèi)卷的非常嚴(yán)重,不得不再得好好的學(xué)習(xí)一下,測試周邊的一些知識,今天為大家整理一些數(shù)據(jù)庫經(jīng)典的筆試題,可以好好的備戰(zhàn)一下測試在數(shù)據(jù)庫方面的知識。
二、分享內(nèi)容:
1.1 本題目的表結(jié)構(gòu)
Student(S#,Sname,Sage,Ssex)學(xué)生表
Course(C#,Cname,T#)課程表
SC(S#,C#,score)成績表
Teacher(T#,Tname)教師表
1.2本題目的建表及測試數(shù)據(jù)
1建表
create table Student(
? ?S# int,
? ?Sname varchar(32),
? ?Sage int,
? ?Ssex varchar(8)
)
create table Course(
? ?C# int ,
? ?Cname varchar(32),
? ?T# int?
)
create table Sc(
? ?S# int ,
? ?C# int ,
? ?score int ,
)
create table Teacher(
? ? T# int ,
? ? Tname varchar(32)
)?? ?
(1)查詢“001”課程比002課程成績高的所有學(xué)生的學(xué)號;
select a.S# from 2 (select S#,Score from SC where C#='001') a, 3 (select S#,Score?
from SC where C#='002') b 4 where a.S#=b.S# and a.Score>b.Score?
??
(2)?查詢平均成績大于60分的同學(xué)的學(xué)號和平均成績;?
?1 select S#,AVG(Score) as?
AvgScore ?2 from SC 3 group by S# 4 having AVG(Score)>60 ??
(3)查詢所有同學(xué)的學(xué)號、姓名、選課數(shù)、總成績;?
?1 select s.S#,s.Sname,COUNT(sc.C#) as CourseCount,SUM(sc.Score) as ScoreSum 2 from?
Student s left outer join SC sc 3 on s.S# = sc.S# 4 group by s.S#,s.Sname 5?
order by s.S# ??
(4)查詢姓“李”的老師的個(gè)數(shù);?
?1 select COUNT(distinct Tname) as?
count 2 from Teacher 3 where Tname like '李%' ??
(5)查詢沒學(xué)過“葉平”老師課的同學(xué)的學(xué)號、姓名; ?
1 select s.S#,s.Sname 2 from?
Student s 3 where s.S# not in 4 ( 5 ? ? select distinct(sc.S#) from SC sc,Course?
c,Teacher t 6 ? ? where sc.C#=c.C# and c.T#=t.T# and t.Tname='葉平' 7 )
? ?
(6)查詢學(xué)過“001”并且也學(xué)過編號“002”課程的同學(xué)的學(xué)號、姓名;??
1 --解法一:求交集 ?2 select s.S#,s.Sname ?3 from Student s,SC sc ?4 where?
s.S#=sc.S# and sc.C#='001' ?5 intersect ?6 select s.S#,s.Sname ?7 from Student?
s,SC sc ?8 where s.S#=sc.S# and sc.C#='002' ?9?
--解法二:使用exists 10 select?
s.S#,s.Sname 11 from Student s,SC sc 12 where s.S#=sc.S# and sc.C#='001' and?
exists 13 ( 14 ? ? select * from SC sc2 where sc.S#=sc2.S# and sc2.C#='002' 15 )
? ?
PS:?
EXISTS用于檢查子查詢是否至少會返回一行數(shù)據(jù),該子查詢實(shí)際上并不返回任何數(shù)據(jù),而是
返回值True或False。那么, 這里我們來看一下 in和exists的區(qū)別 : ?①in?
是把外表和內(nèi)表作hash?
連接,而exists是對外表作loop循環(huán),每次loop循環(huán)再對內(nèi)表進(jìn)行查詢。 ?②一直以來認(rèn)為?
exists比in效率高的說法是不準(zhǔn)確的 。
??
-->如果查詢的兩個(gè)表大小相當(dāng),那么用in和exists差別不大。 ?
-->如果兩個(gè)表中一個(gè)較小,一個(gè)是大表,則子查詢表大的用exists,子查詢表小的用in。 ?
?三、拓展內(nèi)容,下邊有更詳細(xì)的經(jīng)典數(shù)據(jù)庫筆試題和面試題
(1條消息) 經(jīng)典的數(shù)據(jù)庫筆試題你值得擁有-其它文檔類資源-CSDN文庫https://download.csdn.net/download/m0_49428126/86540014
總結(jié)
- 上一篇: 每日一题(41)—— 数组和链表的区别
- 下一篇: mysql配置环境变量(win 10)_