SQL查詢面試題與答案

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

    SQL查詢面試題與答案

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

    SQL查詢面試題與答案

      SQL查詢面試題與答案一

      1.一道SQL語句面試題,關于group by表內容:

      2005-05-09 勝

      2005-05-09 勝

      2005-05-09 負

      2005-05-09 負

      2005-05-10 勝

      2005-05-10 負

      2005-05-10 負

      如果要生成下列結果, 該如何寫sql語句?

      勝 負

      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','負')

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

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

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

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

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

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

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

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

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

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

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

      where a.col001=b.col001

      2.請教一個面試中遇到的SQL語句的查詢問題

      表中有A B C三列,用SQL語句實現:當A列大于B列時選擇A列否則選擇B列,當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字段)為當天的所有記錄?(SendTime字段為datetime型,包含日期與時間)

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

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

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

      大于或等于80表示優秀,大于或等于60表示及格,小于60分表示不及格。

      顯示格式:

      語文 數學 英語

      及格 優秀 不及格

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

      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

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

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

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

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

      區別:

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

      當創建它的進程消失時這個臨時表就自動刪除.

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

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

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

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

      SQL Server 2000 數據庫有三種類型的文件:

      主要數據文件

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

      次要數據文件

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

      日志文件

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

      7.請用一個sql語句得出結果

      從table1,table2中取出如table3所列格式數據,注意提供的數據及結果不準確,只是作為一個格式向大家請教。

      如使用存儲過程也可以。

      table1

      月份mon 部門dep 業績yj

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

      一月份 01 10

      一月份 02 10

      一月份 03 5

      二月份 02 8

      二月份 04 9

      三月份 03 8

      table2

      部門dep 部門名稱dname

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

      01 國內業務一部

      02 國內業務二部

      03 國內業務三部

      04 國際業務部

      table3 (result)

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

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

      01 10 null null

      02 10 8 null

      03 null 5 8

      04 null null 9

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

      1)

      select a.部門名稱dname,b.業績yj as '一月份',c.業績yj as '二月份',d.業績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的記錄查出來,并顯示共有多少條記錄數。

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

      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、查詢如下課程成績第 3 名到第 6 名的學生成績單:企業管理(001),馬克思(002),UML (003),數據庫(004)

      [學生ID],[學生姓名],企業管理,馬克思,UML,數據庫,平均成績

      SELECT DISTINCT top 3

      SC.S# As 學生學號,

      Student.Sname AS 學生姓名 ,

      T1.score AS 企業管理,

      T2.score AS 馬克思,

      T3.score AS UML,

      T4.score AS 數據庫,

      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、統計列印各科成績,各分數段人數:課程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、查詢學生平均成績及其名次

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

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

      FROM SC

      GROUP BY S#

      ) AS T1

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

      S# as 學生學號,平均成績

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

      FROM SC

      GROUP BY S#

      ) AS T2

      ORDER BY 平均成績 desc;

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

      SELECT t1.S# as 學生ID,t1.C# as 課程ID,Score as 分數

      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、查詢每門課程被選修的學生數

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

      7、查詢出只選修了一門課程的全部學生的學號和姓名

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

      from SC ,Student

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

      8、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名;

      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分的同學的學號、姓名;

      select S#,Sname

      from Student

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

      10、查詢沒有學全所有課的同學的學號、姓名;

      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、查詢至少有一門課與學號為“1001”的同學所學相同的同學的學號和姓名;

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

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

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

      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、刪除學習“葉平”老師課的SC表記錄;

      Delect SC

      from course ,Teacher

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

      16、向SC表中插入一些記錄,這些記錄要求符合以下條件:沒有上過編號“003”課程的同學學號、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、按平均成績從高到低顯示所有學生的“數據庫”、“企業管理”、“英語”三門的課程成績,按如下形式顯示: 學生ID,,數據庫,企業管理,英語,有效課程數,有效平均分

      SELECT S# as 學生ID

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

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

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

      ,COUNT(*) AS 有效課程數, 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、按各科平均成績從低到高和及格率的百分數從高到低順序

      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 及格百分數

      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、查詢如下課程平均成績和及格率的百分數(用"1行"顯示): 企業管理(001),馬克思(002),OO&UML (003),數據庫(004)

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

      ,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 企業管理及格百分數

      ,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 馬克思及格百分數

      ,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及格百分數

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

      ,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 數據庫及格百分數

      FROM SC

    【SQL查詢面試題與答案】相關文章:

    結構化查詢語言SQL習題與答案11-11

    SQL查詢語句大全09-29

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

    SQL語言查詢基礎:連接查詢和聯合查詢11-11

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

    2017年Sql常見面試題08-14

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

    使用sql語句查詢日期的方法03-15

    SQL語句說明與范例11-12

    91久久大香伊蕉在人线_国产综合色产在线观看_欧美亚洲人成网站在线观看_亚洲第一无码精品立川理惠

      日久精品不卡一区在线观看 | 日韩高清中文字幕 | 亚洲欧洲日韩淙合久久 | 亚洲精品国产乱码不卡 | 亚洲人成网站色在线 | 亚洲欧洲视频在线播放 |