国产激情久久久久影院小草_国产91高跟丝袜_99精品视频99_三级真人片在线观看

SQL查詢面試題與答案

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

SQL查詢面試題與答案

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

SQL查詢面試題與答案

  SQL查詢面試題與答案一

  1.一道SQL語句面試題,關(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語句?

  勝 負(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.請教一個面試中遇到的SQL語句的查詢問題

  表中有A B C三列,用SQL語句實現(xiàn):當(dāng)A列大于B列時選擇A列否則選擇B列,當(dāng)B列大于C列時選擇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.面試題:一個日期判斷的sql語句?

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

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

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

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

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

  顯示格式:

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

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

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

  select

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

  when 語文>=60 then '及格'

  else '不及格') as 語文,

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

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

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

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

  when 英語>=60 then '及格'

  else '不及格') as 英語,

  from table

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

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

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

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

  區(qū)別:

  用戶臨時表只對創(chuàng)建這個表的用戶的Session可見,對其他進程是不可見的.

  當(dāng)創(chuàng)建它的進程消失時這個臨時表就自動刪除.

  全局臨時表對整個SQL Server實例都可見,但是所有訪問它的Session都消失的時候,它也自動刪除.

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

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

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

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

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

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

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

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

  日志文件

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

  7.請用一個sql語句得出結(jié)果

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

  如使用存儲過程也可以。

  table1

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

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

  一月份 01 10

  一月份 02 10

  一月份 03 5

  二月份 02 8

  二月份 04 9

  三月份 03 8

  table2

  部門dep 部門名稱dname

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

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

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

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

  04 國際業(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è)績yj as '一月份',c.業(yè)績yj as '二月份',d.業(yè)績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.華為一道面試題

  一個表中的Id有多個記錄,把所有這個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 平均成績

  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程成績第 3 名到第 6 名的學(xué)生成績單:企業(yè)管理(001),馬克思(002),UML (003),數(shù)據(jù)庫(004)

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

  SELECT DISTINCT top 3

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

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

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

  T2.score AS 馬克思,

  T3.score AS UML,

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

  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ǐ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é)生平均成績及其名次

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

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

  FROM SC

  GROUP BY S#

  ) AS T1

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

  S# as 學(xué)生學(xué)號,平均成績

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

  FROM SC

  GROUP BY S#

  ) AS T2

  ORDER BY 平均成績 desc;

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

  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é)號和姓名

  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、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學(xué)的學(xué)號、姓名;

  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、查詢所有課程成績小于60分的同學(xué)的學(xué)號、姓名;

  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é)號、姓名;

  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é)號為“1001”的同學(xué)所學(xué)相同的同學(xué)的學(xué)號和姓名;

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

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

  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”表中“葉平”老師教的課的成績都更改為此課程的平均成績;

  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”號的同學(xué)學(xué)習(xí)的課程完全相同的其他同學(xué)學(xué)號和姓名;

  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表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“003”課程的同學(xué)學(xué)號、2、

  號課的平均成績;

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

  SELECT S# as 學(xué)生ID

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

  ,(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 英語

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

  FROM SC AS t

  GROUP BY S#

  ORDER BY avg(t.score)

  18、查詢各科成績最高和最低的分:以如下形式顯示:課程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ǐn)?shù)從高到低順序

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

  ,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ǐn)?shù)(用"1行"顯示): 企業(yè)管理(001),馬克思(002),OO&UML (003),數(shù)據(jù)庫(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ù)庫平均分

  ,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ù)庫及格百分?jǐn)?shù)

  FROM SC

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

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

SQL查詢語句大全09-29

SQL查詢語句講解例子11-12

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

sql語句的各種模糊查詢11-11

2017年Sql常見面試題08-14

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

SQL語句說明與范例11-12