<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 325,  comments - 25,  trackbacks - 0

    一道經(jīng)典SQL面試題及答案

    現(xiàn)在我們假設(shè)只有一個(gè)table,名為pages,有四個(gè)字段,id, url,title,body。里面儲存了很多網(wǎng)頁,網(wǎng)頁的url地址,title和網(wǎng)頁的內(nèi)容,然后你用一個(gè)sql查詢將url匹配的排在最前, title匹配的其次,body匹配最后,沒有任何字段匹配的,不返回。

    就是上面這道面試題,讓我想了一個(gè)下午,在網(wǎng)上找資料,最后用下面方法實(shí)現(xiàn)


    SELECT *
    FROM page where url like '%baidu%' or title like '%baidu%' or like ''
    ORDER BY CHARINDEX('baidu', url) DESC, CHARINDEX('baidu', title) DESC,
          CHARINDEX('baidu', body) DESC

    但我感覺這種方法并不是最簡單的,后來把這個(gè)方法發(fā)給面試的人,他給我了一種更簡單方法,只要用基本的Sql語句就可以實(shí)現(xiàn)。代碼如下

    select a.[id],a.mark from
    (
    select [page].[id],100 as mark from [page] where [page].[url] like '%baidu%'
    union
    select [page].[id],50 as mark from [page] where [page].[title] like '%baidu%'
    union
    select [page].[id],10 as mark from [page] where [page].[body] like '%baidu%'
    ) as a  order by mark desc

    用union 實(shí)現(xiàn)聯(lián)合查詢,在每個(gè)查詢語句中定義一個(gè)臨時(shí)變量mark 并給mark賦值,在最后的輸出時(shí)采用mark來排序,這樣實(shí)現(xiàn),非常簡單,我感覺這題更多考研我們的編程思想。

    select url,title,body from pages where url<>'' or title<>'' or body <>''

    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.請教一個(gè)面試中遇到的SQL語句的查詢問題
    表中有A B C三列,用SQL語句實(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語句?
    請取出tb_send表中日期(SendTime字段)為當(dāng)天的所有記錄?(SendTime字段為datetime型,包含日期與時(shí)間)
    ------------------------------------------
    select * from tb where datediff(dd,SendTime,getdate())=0

    4.有一張表,里面有3個(gè)字段:語文,數(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)建一張用戶臨時(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í)表只對創(chuàng)建這個(gè)表的用戶的Session可見,對其他進(jìn)程是不可見的.
    當(dāng)創(chuàng)建它的進(jìn)程消失時(shí)這個(gè)臨時(shí)表就自動(dòng)刪除.

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

    6.sqlserver2000是一種大型數(shù)據(jù)庫,他的存儲容量只受存儲介質(zhì)的限制,請問它是通過什么方式實(shí)現(xiàn)這種無限容量機(jī)制的。
    ------------------------------------------
    它的所有數(shù)據(jù)都存儲在數(shù)據(jù)文件中(*.dbf),所以只要文件夠大,SQL Server的存儲容量是可以擴(kuò)大的.

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

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

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

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

    7.請用一個(gè)sql語句得出結(jié)果
    從table1,table2中取出如table3所列格式數(shù)據(jù),注意提供的數(shù)據(jù)及結(jié)果不準(zhǔn)確,只是作為一個(gè)格式向大家請教。
    如使用存儲過程也可以。

    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.華為一道面試題
    一個(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

     

    1、刪除表中重復(fù)的記錄(記錄完全相同才算重復(fù))

    select distinct * into #table_name from table_name
    delete from table_name
    select * into table_name from #table_name
    drop table #table_name

    2、學(xué)生表student,字段sID,sName,sAge;教師表teacher,字段tID,TName,tAge;教師-學(xué)生表ts,字段id,tID,sID。請統(tǒng)計(jì)每個(gè)教師有多少學(xué)生,要求教師年齡大于30,學(xué)生年齡大于12,并列出教師ID,教師姓名,學(xué)生人數(shù)答案:如果教師不同名,則語句如下

    select teacher.tid,teacher.tname,count(*) as snum from ts,teacher,student
    where teacher.tid=ts.tid and student.sid=ts.sid and teacher.tage>30 and student.sage>22
    group by teacher.tid,teacher.tname

    如果教師有同名,則語句如下

    select temptable.tid,tname,temptable.snum from teacher,(select teacher.tid,count(*) as snum from ts,teacher,student where teacher.tid=ts.tid and student.sid=ts.sid and teacher.tage>30 and student.sage>22 group by teacher.tid) temptable
    where teacher.tid=temptable.tid
    posted on 2009-04-16 10:00 長春語林科技 閱讀(500) 評論(0)  編輯  收藏 所屬分類: sqlserver

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導(dǎo)航:
     
    <2009年4月>
    2930311234
    567891011
    12131415161718
    19202122232425
    262728293012
    3456789

     

    長春語林科技?xì)g迎您!

    常用鏈接

    留言簿(6)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    相冊

    收藏夾

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 精品久久久久久久久免费影院| 在线日韩av永久免费观看| 亚洲AV无码成人专区| 又粗又硬免费毛片| 久久99免费视频| 亚洲欧美国产国产一区二区三区| 亚洲国产V高清在线观看| 永久免费视频网站在线观看| 亚洲日韩精品国产一区二区三区| 国产精品亚洲综合专区片高清久久久| 未满十八18禁止免费无码网站| 亚洲AV日韩AV永久无码色欲| 日本亚洲视频在线| 日日夜夜精品免费视频| 午夜免费福利视频| 黄色网页免费观看| 国产成人无码免费网站| 久久狠狠高潮亚洲精品| 日韩精品电影一区亚洲| 日韩免费高清一级毛片| 亚洲一区二区三区在线观看蜜桃 | 亚洲三级中文字幕| 亚洲视频在线免费| 91情侣在线精品国产免费| 曰批全过程免费视频在线观看无码| 亚洲色无码专区一区| 日韩精品一区二区亚洲AV观看| 免费中文字幕不卡视频| 欧美大尺寸SUV免费| 日韩内射激情视频在线播放免费| 五月天国产成人AV免费观看| 亚洲日本成本人观看| 亚洲欧洲尹人香蕉综合| 久久精品国产亚洲av四虎| 中文字幕亚洲专区| 免费大片黄手机在线观看| 成人无遮挡裸免费视频在线观看 | 国产在线观看免费av站| 免费国产黄网站在线观看动图 | 日韩免费无码一区二区三区| 成人精品视频99在线观看免费|