SQL一些面試題
阿新 • • 發佈:2018-12-17
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) select name from table group by name having min(fenshu)>80 selectname from table group by name having count(kecheng)>=3 and min(fenshu)>=80
2. 學生表 如下:
自動編號 學號 姓名 課程編號 課程名稱 分數 1 2005001 張三 0001 數學 69 2 2005002 李四 0001 數學 89 3 2005001 張三 0001 數學 69 刪除除了自動編號不同, 其他都相同的學生冗餘資訊 -------------------------------------------------- A: delete tablename where 自動編號 not in(selectmin( 自動編號) from tablename group by 學號, 姓名, 課程編號, 課程名稱, 分數)
3. 面試題:怎麼把這樣一個表兒
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, (selectamount 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
4. 說明:拷貝表( 拷貝資料, 源表名:a 目標表名:b)
SQL: insert into b(a, b, c) select d,e,f from a;
5.有一張表,裡面有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
6、編寫SQL語句
1) 建立一張學生表,包含以下資訊,學號,姓名,年齡,性別,家庭住址,聯絡電話
Create table stu (學號 int , 姓名 varchar(8), 年齡 int, 性別 varchar(4), 家庭地址 varchar(50), 聯絡電話 int );
2) 修改學生表的結構,新增一列資訊,學歷
Alter table stu add 學歷 varchar(6);
3) 修改學生表的結構,刪除一列資訊,家庭住址
Alter table stu drop column 家庭地址
4) 向學生表新增如下資訊:
1A22男123456小學 2B21男119中學 3C23男110高中 4D18女114大學 Insert into stu values(1,’A’,22,’男’,123456,’小學’) Insert into stu values(2,’B’,21,’男’,119,’中學’) Insert into stu values(3,’C’,23,’男’,110,’高中’) Insert into stu values(4,’D’,18,’女’,114,’大學’)
5) 修改學生表的資料,將電話號碼以11開頭的學員的學歷改為“大專”
Update stu set 學歷=’大專’ where 聯絡電話 like ‘11%’
6) 刪除學生表的資料,姓名以C開頭,性別為‘男’的記錄刪除
Delect from stu where 性別=’男’ and 姓名 like ‘c%’
7) 查詢學生表的資料,將所有年齡小於22歲的,學歷為“大專”的,學生的姓名和學號示出來
Select 姓名,學號 from stu where 年齡<22 and 學歷=’大專’
8) 查詢學生表的資料,查詢所有資訊,列出前25%的記錄
Select top 25 percent * from stu
9) 查詢出所有學生的姓名,性別,年齡降序排列
Select 姓名,性別 from stu order by 年齡 desc
10) 按照性別分組查詢所有的平均年齡
Select avg(年齡) from stu group by 性別
7、查詢A(ID,Name)表中第31至40條記錄,ID作為主鍵可能是不是連續增長的列,完整的查詢語句如下:
select top 10 * from A where ID >(select max(ID) from (select top 30 ID from A order by A ) T) order by A
8、查詢表A中存在ID重複三次以上的記錄,完整的查詢語句如下:
select * from(select count(ID) as count from table group by ID)T where T.count>3
9、說出以下聚合數的含義:avg ,sum ,max ,min , count ,count(*)
AVG:求平均值 SUM:求和 MAX:求最大值 MIN:求最小值 COUNT(*):返回所有行數 ***************************************************************** 事務 Transaction 觸發器 TRIGGER 繼續 continue 唯一 unqiue 主鍵 primary key 標識列 identity 外來鍵 foreign key 檢查 check 約束 constraint *****************************************************************
10、說明:隨機取出10條資料
select top 10 * from tablename order by newid()
11、查詢平均成績大於60分的同學的學號和平均成績;
select stuId,avg(score) from Scores group by stuId having avg(score) >60;