天天看點

Sql常見面試題

Sql常見面試題

1.用一條SQL語句 查詢出每門課都大于80分的學生姓名  

name   kecheng   fenshu 

張三     國文       81

張三     數學       75

李四     國文       76

李四     數學       90

王五     國文       81

王五     數學       100

王五     英語       90

A: select distinct name from table  where  name not in (select distinct name from table where fenshu<=80)

2.學生表 如下:

自動編号   學号   姓名 課程編号 課程名稱 分數

1        2005001  張三  0001      數學    69

2        2005002  李四  0001      數學    89

3        2005001  張三  0001      數學    69

删除除了自動編号不同,其他都相同的學生備援資訊

A: delete tablename where 自動編号 not in(select min(自動編号) from tablename group by 學号,姓名,課程編号,課程名稱,分數)

一個叫department的表,裡面隻有一個字段name,一共有4條紀錄,分别是a,b,c,d,對應四個球對,現在四個球對進行比賽,用一條sql語句顯示所有可能的比賽組合.

你先按你自己的想法做一下,看結果有我的這個簡單嗎?

答:select a.name, b.name 

from team a, team b 

where a.name < b.name

請用SQL語句實作:從TestDB資料表中查詢出所有月份的發生額都比101科目相應月份的發生額高的科目。請注意:TestDB中有很多科目,都有1-12月份的發生額。

AccID:科目代碼,Occmonth:發生額月份,DebitOccur:發生額。

資料庫名:JcyAudit,資料集:Select * from TestDB

答:select a.*

from TestDB a 

,(select Occmonth,max(DebitOccur) Debit101ccur from TestDB where AccID='101' group by Occmonth) b

where a.Occmonth=b.Occmonth and a.DebitOccur>b.Debit101ccur

************************************************************************************

面試題:怎麼把這樣一個表兒

year  month amount

1991   1     1.1

1991   2     1.2

1991   3     1.3

1991   4     1.4

1992   1     2.1

1992   2     2.2

1992   3     2.3

1992   4     2.4

查成這樣一個結果

year m1  m2  m3  m4

1991 1.1 1.2 1.3 1.4

1992 2.1 2.2 2.3 2.4 

答案一、

select year, 

(select amount from  aaa m where month=1  and m.year=aaa.year) as m1,

(select amount from  aaa m where month=2  and m.year=aaa.year) as m2,

(select amount from  aaa m where month=3  and m.year=aaa.year) as m3,

(select amount from  aaa m where month=4  and m.year=aaa.year) as m4

from aaa  group by year

這個是ORACLE  中做的:

select * from (select name, year b1, lead(year) over

(partition by name order by year) b2, lead(m,2) over(partition by name order by year) b3,rank()over(

partition by name order by year) rk from t) where rk=1;

精妙的SQL語句!

精妙SQL語句  

作者:不詳 發文時間:2003.05.29 10:55:05 

說明:複制表(隻複制結構,源表名:a 新表名:b) 

SQL: select * into b from a where 1<>1 

說明:拷貝表(拷貝資料,源表名:a 目标表名:b) 

SQL: insert into b(a, b, c) select d,e,f from b; 

說明:顯示文章、送出人和最後回複時間 

SQL: select a.title,a.username,b.adddate from table a,(select max(adddate) adddate from table where table.title=a.title) b 

說明:外連接配接查詢(表名1:a 表名2:b) 

SQL: select a.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a = b.c 

說明:日程安排提前五分鐘提醒 

SQL: select * from 日程安排 where datediff('minute',f開始時間,getdate())>5 

說明:兩張關聯表,删除主表中已經在副表中沒有的資訊 

SQL: 

delete from info where not exists ( select * from infobz where info.infid=infobz.infid ) 

說明:-- 

SELECT A.NUM, A.NAME, B.UPD_DATE, B.PREV_UPD_DATE 

FROM TABLE1, 

(SELECT X.NUM, X.UPD_DATE, Y.UPD_DATE PREV_UPD_DATE 

FROM (SELECT NUM, UPD_DATE, INBOUND_QTY, STOCK_ONHAND 

FROM TABLE2 

WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = TO_CHAR(SYSDATE, 'YYYY/MM')) X, 

(SELECT NUM, UPD_DATE, STOCK_ONHAND 

WHERE TO_CHAR(UPD_DATE,'YYYY/MM') = 

TO_CHAR(TO_DATE(TO_CHAR(SYSDATE, 'YYYY/MM') 

本文轉自yonghu86 51CTO部落格,原文連結:http://blog.51cto.com/yonghu/1321439,如需轉載請自行聯系原作者

上一篇: 前言