亚洲国产日韩欧美在线a乱码,国产精品路线1路线2路线,亚洲视频一区,精品国产自,www狠狠,国产情侣激情在线视频免费看,亚洲成年网站在线观看

SQL查詢面試題與答案

時(shí)間:2020-10-13 14:41:44 筆試題目 我要投稿

SQL查詢面試題與答案

  SQL語(yǔ)言是一種數(shù)據(jù)庫(kù)查詢和程序設(shè)計(jì)語(yǔ)言,用于存取數(shù)據(jù)以及查詢、更新和管理關(guān)系數(shù)據(jù)庫(kù)系統(tǒng);同時(shí)也是數(shù)據(jù)庫(kù)腳本文件的擴(kuò)展名。下面是小編搜集的SQL查詢面試題與答案,歡迎大家閱讀。

SQL查詢面試題與答案

  SQL查詢面試題與答案一

  1.一道SQL語(yǔ)句面試題,關(guān)于group by表內(nèi)容:

  2005-05-09 勝

  2005-05-09 勝

  2005-05-09 負(fù)

  2005-05-09 負(fù)

  2005-05-10 勝

  2005-05-10 負(fù)

  2005-05-10 負(fù)

  如果要生成下列結(jié)果, 該如何寫sql語(yǔ)句?

  勝 負(fù)

  2005-05-09 2 2

  2005-05-10 1 2

  ------------------------------------------

  create table #tmp(rq varchar(10),shengfu nchar(1))

  insert into #tmp values('2005-05-09','勝')

  insert into #tmp values('2005-05-09','勝')

  insert into #tmp values('2005-05-09','負(fù)')

  insert into #tmp values('2005-05-09','負(fù)')

  insert into #tmp values('2005-05-10','勝')

  insert into #tmp values('2005-05-10','負(fù)')

  insert into #tmp values('2005-05-10','負(fù)')

  1)select rq, sum(case when shengfu='勝' then 1 else 0 end)'勝',sum(case when shengfu='負(fù)' then 1 else 0 end)'負(fù)' from #tmp group by rq

  2) select N.rq,N.勝,M.負(fù) from (

  select rq,勝=count(*) from #tmp where shengfu='勝'group by rq)N inner join

  (select rq,負(fù)=count(*) from #tmp where shengfu='負(fù)'group by rq)M on N.rq=M.rq

  3)select a.col001,a.a1 勝,b.b1 負(fù) from

  (select col001,count(col001) a1 from temp1 where col002='勝' group by col001) a,

  (select col001,count(col001) b1 from temp1 where col002='負(fù)' group by col001) b

  where a.col001=b.col001

  2.請(qǐng)教一個(gè)面試中遇到的SQL語(yǔ)句的查詢問題

  表中有A B C三列,用SQL語(yǔ)句實(shí)現(xiàn):當(dāng)A列大于B列時(shí)選擇A列否則選擇B列,當(dāng)B列大于C列時(shí)選擇B列否則選擇C列。

  ------------------------------------------

  select (case when a>b then a else b end ),

  (case when b>c then b esle c end)

  from table_name

  3.面試題:一個(gè)日期判斷的sql語(yǔ)句?

  請(qǐng)取出tb_send表中日期(SendTime字段)為當(dāng)天的所有記錄?(SendTime字段為datetime型,包含日期與時(shí)間)

  ------------------------------------------

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

  4.有一張表,里面有3個(gè)字段:語(yǔ)文,數(shù)學(xué),英語(yǔ)。其中有3條記錄分別表示語(yǔ)文70分,數(shù)學(xué)80分,英語(yǔ)58分,請(qǐng)用一條sql語(yǔ)句查詢出這三條記錄并按以下條件顯示出來(并寫出您的思路):

  大于或等于80表示優(yōu)秀,大于或等于60表示及格,小于60分表示不及格。

  顯示格式:

  語(yǔ)文 數(shù)學(xué) 英語(yǔ)

  及格 優(yōu)秀 不及格

  ------------------------------------------

  select

  (case when 語(yǔ)文>=80 then '優(yōu)秀'

  when 語(yǔ)文>=60 then '及格'

  else '不及格') as 語(yǔ)文,

  (case when 數(shù)學(xué)>=80 then '優(yōu)秀'

  when 數(shù)學(xué)>=60 then '及格'

  else '不及格') as 數(shù)學(xué),

  (case when 英語(yǔ)>=80 then '優(yōu)秀'

  when 英語(yǔ)>=60 then '及格'

  else '不及格') as 英語(yǔ),

  from table

  5.在sqlserver2000中請(qǐng)用sql創(chuàng)建一張用戶臨時(shí)表和系統(tǒng)臨時(shí)表,里面包含兩個(gè)字段ID和IDValues,類型都是int型,并解釋下兩者的區(qū)別?

  ------------------------------------------

  用戶臨時(shí)表:create table #xx(ID int, IDValues int)

  系統(tǒng)臨時(shí)表:create table ##xx(ID int, IDValues int)

  區(qū)別:

  用戶臨時(shí)表只對(duì)創(chuàng)建這個(gè)表的用戶的Session可見,對(duì)其他進(jìn)程是不可見的.

  當(dāng)創(chuàng)建它的進(jìn)程消失時(shí)這個(gè)臨時(shí)表就自動(dòng)刪除.

  全局臨時(shí)表對(duì)整個(gè)SQL Server實(shí)例都可見,但是所有訪問它的Session都消失的時(shí)候,它也自動(dòng)刪除.

  6.sqlserver2000是一種大型數(shù)據(jù)庫(kù),他的`存儲(chǔ)容量只受存儲(chǔ)介質(zhì)的限制,請(qǐng)問它是通過什么方式實(shí)現(xiàn)這種無限容量機(jī)制的。

  ------------------------------------------

  它的所有數(shù)據(jù)都存儲(chǔ)在數(shù)據(jù)文件中(*.dbf),所以只要文件夠大,SQL Server的存儲(chǔ)容量是可以擴(kuò)大的.

  SQL Server 2000 數(shù)據(jù)庫(kù)有三種類型的文件:

  主要數(shù)據(jù)文件

  主要數(shù)據(jù)文件是數(shù)據(jù)庫(kù)的起點(diǎn),指向數(shù)據(jù)庫(kù)中文件的其它部分。每個(gè)數(shù)據(jù)庫(kù)都有一個(gè)主要數(shù)據(jù)文件。主要數(shù)據(jù)文件的推薦文件擴(kuò)展名是 .mdf。

  次要數(shù)據(jù)文件

  次要數(shù)據(jù)文件包含除主要數(shù)據(jù)文件外的所有數(shù)據(jù)文件。有些數(shù)據(jù)庫(kù)可能沒有次要數(shù)據(jù)文件,而有些數(shù)據(jù)庫(kù)則有多個(gè)次要數(shù)據(jù)文件。次要數(shù)據(jù)文件的推薦文件擴(kuò)展名是 .ndf。

  日志文件

  日志文件包含恢復(fù)數(shù)據(jù)庫(kù)所需的所有日志信息。每個(gè)數(shù)據(jù)庫(kù)必須至少有一個(gè)日志文件,但可以不止一個(gè)。日志文件的推薦文件擴(kuò)展名是 .ldf。

  7.請(qǐng)用一個(gè)sql語(yǔ)句得出結(jié)果

  從table1,table2中取出如table3所列格式數(shù)據(jù),注意提供的數(shù)據(jù)及結(jié)果不準(zhǔn)確,只是作為一個(gè)格式向大家請(qǐng)教。

  如使用存儲(chǔ)過程也可以。

  table1

  月份mon 部門dep 業(yè)績(jī)yj

  -------------------------------

  一月份 01 10

  一月份 02 10

  一月份 03 5

  二月份 02 8

  二月份 04 9

  三月份 03 8

  table2

  部門dep 部門名稱dname

  --------------------------------

  01 國(guó)內(nèi)業(yè)務(wù)一部

  02 國(guó)內(nèi)業(yè)務(wù)二部

  03 國(guó)內(nèi)業(yè)務(wù)三部

  04 國(guó)際業(yè)務(wù)部

  table3 (result)

  部門dep 一月份 二月份 三月份

  --------------------------------------

  01 10 null null

  02 10 8 null

  03 null 5 8

  04 null null 9

  ------------------------------------------

  1)

  select a.部門名稱dname,b.業(yè)績(jī)yj as '一月份',c.業(yè)績(jī)yj as '二月份',d.業(yè)績(jī)yj as '三月份'

  from table1 a,table2 b,table2 c,table2 d

  where a.部門dep = b.部門dep and b.月份mon = '一月份' and

  a.部門dep = c.部門dep and c.月份mon = '二月份' and

  a.部門dep = d.部門dep and d.月份mon = '三月份' and

  2)

  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

  8.華為一道面試題

  一個(gè)表中的Id有多個(gè)記錄,把所有這個(gè)id的記錄查出來,并顯示共有多少條記錄數(shù)。

  ------------------------------------------

  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

  SQL查詢面試題與答案二

  1、查詢不同老師所教不同課程平均分從高到低顯示

  SELECT max(Z.T#) AS 教師ID,MAX(Z.Tname) AS 教師姓名,C.C# AS 課程ID,MAX(C.Cname) AS 課程名稱,AVG(Score) AS 平均成績(jī)

  FROM SC AS T,Course AS C ,Teacher AS Z

  where T.C#=C.C# and C.T#=Z.T#

  GROUP BY C.C#

  ORDER BY AVG(Score) DESC

  2、查詢?nèi)缦抡n程成績(jī)第 3 名到第 6 名的學(xué)生成績(jī)單:企業(yè)管理(001),馬克思(002),UML (003),數(shù)據(jù)庫(kù)(004)

  [學(xué)生ID],[學(xué)生姓名],企業(yè)管理,馬克思,UML,數(shù)據(jù)庫(kù),平均成績(jī)

  SELECT DISTINCT top 3

  SC.S# As 學(xué)生學(xué)號(hào),

  Student.Sname AS 學(xué)生姓名 ,

  T1.score AS 企業(yè)管理,

  T2.score AS 馬克思,

  T3.score AS UML,

  T4.score AS 數(shù)據(jù)庫(kù),

  ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) as 總分

  FROM Student,SC LEFT JOIN SC AS T1

  ON SC.S# = T1.S# AND T1.C# = '001'

  LEFT JOIN SC AS T2

  ON SC.S# = T2.S# AND T2.C# = '002'

  LEFT JOIN SC AS T3

  ON SC.S# = T3.S# AND T3.C# = '003'

  LEFT JOIN SC AS T4

  ON SC.S# = T4.S# AND T4.C# = '004'

  WHERE student.S#=SC.S# and

  ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)

  NOT IN

  (SELECT

  DISTINCT

  TOP 15 WITH TIES

  ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0)

  FROM sc

  LEFT JOIN sc AS T1

  ON sc.S# = T1.S# AND T1.C# = 'k1'

  LEFT JOIN sc AS T2

  ON sc.S# = T2.S# AND T2.C# = 'k2'

  LEFT JOIN sc AS T3

  ON sc.S# = T3.S# AND T3.C# = 'k3'

  LEFT JOIN sc AS T4

  ON sc.S# = T4.S# AND T4.C# = 'k4'

  ORDER BY ISNULL(T1.score,0) + ISNULL(T2.score,0) + ISNULL(T3.score,0) + ISNULL(T4.score,0) DESC);

  3、統(tǒng)計(jì)列印各科成績(jī),各分?jǐn)?shù)段人數(shù):課程ID,課程名稱,[100-85],[85-70],[70-60],[ <60]

  SELECT SC.C# as 課程ID, Cname as 課程名稱

  ,SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]

  ,SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70]

  ,SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60]

  ,SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]

  FROM SC,Course

  where SC.C#=Course.C#

  GROUP BY SC.C#,Cname;

  4、查詢學(xué)生平均成績(jī)及其名次

  SELECT 1+(SELECT COUNT( distinct 平均成績(jī))

  FROM (SELECT S#,AVG(score) AS 平均成績(jī)

  FROM SC

  GROUP BY S#

  ) AS T1

  WHERE 平均成績(jī) > T2.平均成績(jī)) as 名次,

  S# as 學(xué)生學(xué)號(hào),平均成績(jī)

  FROM (SELECT S#,AVG(score) 平均成績(jī)

  FROM SC

  GROUP BY S#

  ) AS T2

  ORDER BY 平均成績(jī) desc;

  5、查詢各科成績(jī)前三名的記錄:(不考慮成績(jī)并列情況)

  SELECT t1.S# as 學(xué)生ID,t1.C# as 課程ID,Score as 分?jǐn)?shù)

  FROM SC t1

  WHERE score IN (SELECT TOP 3 score

  FROM SC

  WHERE t1.C#= C#

  ORDER BY score DESC

  )

  ORDER BY t1.C#;

  6、查詢每門課程被選修的學(xué)生數(shù)

  select c#,count(S#) from sc group by C#;

  7、查詢出只選修了一門課程的全部學(xué)生的學(xué)號(hào)和姓名

  select SC.S#,Student.Sname,count(C#) AS 選課數(shù)

  from SC ,Student

  where SC.S#=Student.S# group by SC.S# ,Student.Sname having count(C#)=1;

  8、查詢課程編號(hào)“002”的成績(jī)比課程編號(hào)“001”課程低的所有同學(xué)的學(xué)號(hào)、姓名;

  Select S#,Sname from (select Student.S#,Student.Sname,score ,(select score from SC SC_2 where SC_2.S#=Student.S# and SC_2.C#='002') score2

  from Student,SC where Student.S#=SC.S# and C#='001') S_2 where score2

  9、查詢所有課程成績(jī)小于60分的同學(xué)的學(xué)號(hào)、姓名;

  select S#,Sname

  from Student

  where S# not in (select Student.S# from Student,SC where S.S#=SC.S# and score>60);

  10、查詢沒有學(xué)全所有課的同學(xué)的學(xué)號(hào)、姓名;

  select Student.S#,Student.Sname

  from Student,SC

  where Student.S#=SC.S# group by Student.S#,Student.Sname having count(C#) <(select count(C#) from Course);

  11、查詢至少有一門課與學(xué)號(hào)為“1001”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號(hào)和姓名;

  select S#,Sname from Student,SC where Student.S#=SC.S# and C# in select C# from SC where S#='1001';

  12、查詢至少學(xué)過學(xué)號(hào)為“001”同學(xué)所有一門課的其他同學(xué)學(xué)號(hào)和姓名;

  select distinct SC.S#,Sname

  from Student,SC

  where Student.S#=SC.S# and C# in (select C# from SC where S#='001');

  13、把“SC”表中“葉平”老師教的課的成績(jī)都更改為此課程的平均成績(jī);

  update SC set score=(select avg(SC_2.score)

  from SC SC_2

  where SC_2.C#=SC.C# ) from Course,Teacher where Course.C#=SC.C# and Course.T#=Teacher.T# and Teacher.Tname='葉平');

  14、查詢和“1002”號(hào)的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)學(xué)號(hào)和姓名;

  select S# from SC where C# in (select C# from SC where S#='1002')

  group by S# having count(*)=(select count(*) from SC where S#='1002');

  15、刪除學(xué)習(xí)“葉平”老師課的SC表記錄;

  Delect SC

  from course ,Teacher

  where Course.C#=SC.C# and Course.T#= Teacher.T# and Tname='葉平';

  16、向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號(hào)“003”課程的同學(xué)學(xué)號(hào)、2、

  號(hào)課的平均成績(jī);

  Insert SC select S#,'002',(Select avg(score)

  from SC where C#='002') from Student where S# not in (Select S# from SC where C#='002');

  17、按平均成績(jī)從高到低顯示所有學(xué)生的“數(shù)據(jù)庫(kù)”、“企業(yè)管理”、“英語(yǔ)”三門的課程成績(jī),按如下形式顯示: 學(xué)生ID,,數(shù)據(jù)庫(kù),企業(yè)管理,英語(yǔ),有效課程數(shù),有效平均分

  SELECT S# as 學(xué)生ID

  ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='004') AS 數(shù)據(jù)庫(kù)

  ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='001') AS 企業(yè)管理

  ,(SELECT score FROM SC WHERE SC.S#=t.S# AND C#='006') AS 英語(yǔ)

  ,COUNT(*) AS 有效課程數(shù), AVG(t.score) AS 平均成績(jī)

  FROM SC AS t

  GROUP BY S#

  ORDER BY avg(t.score)

  18、查詢各科成績(jī)最高和最低的分:以如下形式顯示:課程ID,最高分,最低分

  SELECT L.C# As 課程ID,L.score AS 最高分,R.score AS 最低分

  FROM SC L ,SC AS R

  WHERE L.C# = R.C# and

  L.score = (SELECT MAX(IL.score)

  FROM SC AS IL,Student AS IM

  WHERE L.C# = IL.C# and IM.S#=IL.S#

  GROUP BY IL.C#)

  AND

  R.Score = (SELECT MIN(IR.score)

  FROM SC AS IR

  WHERE R.C# = IR.C#

  GROUP BY IR.C#

  );

  19、按各科平均成績(jī)從低到高和及格率的百分?jǐn)?shù)從高到低順序

  SELECT t.C# AS 課程號(hào),max(course.Cname)AS 課程名,isnull(AVG(score),0) AS 平均成績(jī)

  ,100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分?jǐn)?shù)

  FROM SC T,Course

  where t.C#=course.C#

  GROUP BY t.C#

  ORDER BY 100 * SUM(CASE WHEN isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC

  20、查詢?nèi)缦抡n程平均成績(jī)和及格率的百分?jǐn)?shù)(用"1行"顯示): 企業(yè)管理(001),馬克思(002),OO&UML (003),數(shù)據(jù)庫(kù)(004)

  SELECT SUM(CASE WHEN C# ='001' THEN score ELSE 0 END)/SUM(CASE C# WHEN '001' THEN 1 ELSE 0 END) AS 企業(yè)管理平均分

  ,100 * SUM(CASE WHEN C# = '001' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '001' THEN 1 ELSE 0 END) AS 企業(yè)管理及格百分?jǐn)?shù)

  ,SUM(CASE WHEN C# = '002' THEN score ELSE 0 END)/SUM(CASE C# WHEN '002' THEN 1 ELSE 0 END) AS 馬克思平均分

  ,100 * SUM(CASE WHEN C# = '002' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '002' THEN 1 ELSE 0 END) AS 馬克思及格百分?jǐn)?shù)

  ,SUM(CASE WHEN C# = '003' THEN score ELSE 0 END)/SUM(CASE C# WHEN '003' THEN 1 ELSE 0 END) AS UML平均分

  ,100 * SUM(CASE WHEN C# = '003' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '003' THEN 1 ELSE 0 END) AS UML及格百分?jǐn)?shù)

  ,SUM(CASE WHEN C# = '004' THEN score ELSE 0 END)/SUM(CASE C# WHEN '004' THEN 1 ELSE 0 END) AS 數(shù)據(jù)庫(kù)平均分

  ,100 * SUM(CASE WHEN C# = '004' AND score >= 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN C# = '004' THEN 1 ELSE 0 END) AS 數(shù)據(jù)庫(kù)及格百分?jǐn)?shù)

  FROM SC

【SQL查詢面試題與答案】相關(guān)文章:

結(jié)構(gòu)化查詢語(yǔ)言SQL習(xí)題與答案11-11

SQL查詢語(yǔ)句大全09-29

SQL查詢語(yǔ)句講解例子11-12

SQL語(yǔ)言查詢基礎(chǔ):連接查詢和聯(lián)合查詢11-11

sql語(yǔ)句的各種模糊查詢11-11

2017年Sql常見面試題08-14

楓葉軟件面試題與答案11-16

SQL語(yǔ)句說明與范例11-12