當前位置:首頁 » 編程語言 » 洋錢罐sql面試題
擴展閱讀
web原版 2023-02-01 01:28:35
35寸硬碟是多大 2023-02-01 01:27:07

洋錢罐sql面試題

發布時間: 2022-11-29 18:55:56

sql面試題

翻譯
題目很簡單,樓主看看
MovieBuster在線電影收看服務擁有如下信息的資料庫
(*)號表示外鍵參考,
電影信息表:MovieInfo(mvID, title, rating, year, length, studio)
分類信息表:GenreInfo(mvID*, genre)
工作室信息表:DirectInfo(mvID*, director)
電影信息表主要存放電影的相關信息,其中主鍵mvID是電影出產時人為給定的一個ID值,一個電影可能擁有多個類別並且有多個工作室一起生產。
注意這些表與資料庫中的其他表名已經區別開來。
一個只讀數據信息已經被建立在oracle上並且可以通過如下命令sqlmb1獲得信息。作為選擇,你可以通過此建立自己的數據表。

數據可以從黑板上看到
從資料庫中查詢如下問題並給出正確答案:
(1)工作室 "Paramount Pictures"已經生產多少部 rating="G" 並且年份在1940-1950年之間的電影?
(2)查出rating ="PG" 生產年份在1940之前或者2000年之後的電影的總數。
(3)列出每種分類的電影數,查出結果按電影分數總數降序排序。
(4)查找出生產的電影平均長度至少大於等於3的工作室的名字以及對應的平均電影長度。

❷ sql面試題50題(mysql版)

--插入學生表測試數據
insert into Student values(༽' , '趙雷' , 񟬶-01-01' , '男');
insert into Student values(༾' , '錢電' , 񟬶-12-21' , '男');
insert into Student values(༿' , '孫風' , 񟬶-05-20' , '男');
insert into Student values(ཀ' , '李雲' , 񟬶-08-06' , '男');
insert into Student values(ཁ' , '周梅' , 񟬷-12-01' , '女');
insert into Student values(ག' , '吳蘭' , 񟬸-03-01' , '女');
insert into Student values(གྷ' , '鄭竹' , 񟬵-07-01' , '女');
insert into Student values(ང' , '王菊' , 񟬶-01-20' , '女');
--課程表測試數據
insert into Course values(༽' , '語文' , ༾');
insert into Course values(༾' , '數學' , ༽');
insert into Course values(༿' , '英語' , ༿');
--教師表測試數據
insert into Teacher values(༽' , '張三');
insert into Teacher values(༾' , '李四');
insert into Teacher values(༿' , '王五');
--成績表測試數據
insert into Score values(༽' , ༽' , 80);
insert into Score values(༽' , ༾' , 90);
insert into Score values(༽' , ༿' , 99);
insert into Score values(༾' , ༽' , 70);
insert into Score values(༾' , ༾' , 60);
insert into Score values(༾' , ༿' , 80);
insert into Score values(༿' , ༽' , 80);
insert into Score values(༿' , ༾' , 80);
insert into Score values(༿' , ༿' , 80);
insert into Score values(ཀ' , ༽' , 50);
insert into Score values(ཀ' , ༾' , 30);
insert into Score values(ཀ' , ༿' , 20);
insert into Score values(ཁ' , ༽' , 76);
insert into Score values(ཁ' , ༾' , 87);
insert into Score values(ག' , ༽' , 31);
insert into Score values(ག' , ༿' , 34);
insert into Score values(གྷ' , ༾' , 89);
insert into Score values(གྷ' , ༿' , 98);

-- 1、查詢"01"課程比"02"課程成績高的學生的信息及課程分數
select c.*,a.s_score as 01課程score,b.s_score as 02課程score from
score a,score b
left join student c
on b.s_id = c.s_id
where a.s_id = b.s_id and a.c_id = ༽' and b.c_id = ༾' and a.s_score > b.s_score;

-- 2、查詢"01"課程比"02"課程成績低的學生的信息及課程分數
select a.* ,b.s_score as 01課程,c.s_score as 02課程 from student a
join score b
on a.s_id=b.s_id and b.c_id = ༽'
left join score c
on b.s_id = c.s_id and c.c_id = ༾'
where b.s_score < c.s_score ;

-- 3、查詢平均成績大於等於60分的同學的學生編號和學生姓名和平均成績
select a.s_id,a.s_name,round(avg(b.s_score),2) as 平均成績 from student a
join score b
on a.s_id = b.s_id
group by b.s_id having 平均成績 >= 60;
備註:round[avg(成績),1]里,round是四捨五入函數,1代表保留1位小數

-- 4、查詢平均成績小於60分的同學的學生編號和學生姓名和平均成績
-- (包括有成績的和無成績的)
select b. ,round(avg(a.s_score),2) as 平均成績 from
student b
left join score a on b.s_id = a.s_id group by a.s_id having 平均成績 < 60
union
select b.
,0 as 平衡成績 from student b where b.s_id not in (select s_id from score);

-- 5、查詢所有同學的學生編號、學生姓名、選課總數、所有課程的總成績
select a.s_id,a.s_name,count(b.c_id) as 選課總數 ,sum(b.s_score) as 總分 from student a
left join score b
on a.s_id = b.s_id group by s_id ;

-- 6、查詢"李"姓老師的數量
select count(*) as 李姓老師數量 from teacher where t_name like '李%'

-- 7、查詢學過"張三"老師授課的同學的信息
select a.* from student a join score b
on a.s_id = b.s_id
where b.c_id in (select c.c_id from course c
join teacher d on c.t_id = d.t_id where d.t_name = '張三');

-- 8、查詢沒學過"張三"老師授課的同學的信息
select a.* from student a left join score b on a.s_id = b.s_id where a.s_id not in
(select s_id from score where c_id =
(select c_id from course where t_id =
(select t_id from teacher where t_name = '張
三'))) group by a.s_id;

-- 9、查詢學過編號為"01"並且也學過編號為"02"的課程的同學的信息
select * from student where s_id in
(select a.s_id from score a join score b on a.s_id = b.s_id
where a.c_id = ༽' and b.c_id = ༾');

-- 10、查詢學過編號為"01"但是沒有學過編號為"02"的課程的同學的信息
select * from student where s_id in
(select s_id from score where c_id = ༽' )
and s_id not in (select s_id from score where c_id = ༾' );

-- 11、查詢沒有學全所有課程的同學的信息
select * from student where s_id not in
(select s_id from score group by s_id having count(c_id) = 3);

-- 12、查詢至少有一門課與學號為"01"的同學所學相同的同學的信息
select distinct a.* from student a left join score b
on a.s_id = b.s_id where b.c_id in
(select c_id from score where s_id = ༽') and a.s_id != ༽' ;
注意:distinct是去重的

-- 13、查詢和"01"號的同學學習的課程完全相同的其他同學的信息
select * from student where s_id in
(select s_id from score group by s_id having count(c_id) =
(select count(c_id) from score where s_id = ༽') and s_id not in
(select s_id from score where c_id not in
(select c_id from score where s_id = ༽')) and s_id != ༽');

-- 14、查詢沒學過"張三"老師講授的任一門課程的學生姓名
select s_name from student where s_id not in
(select s_id from score where c_id in
(select c_id from course where t_id in
(select t_id from teacher where t_name ='張三')));

-- 15、查詢兩門及其以上不及格課程的同學的學號,姓名及其平均成績
select a.s_id ,b.s_name,round(avg(a.s_score),2) as 平均成績 from score a
left join student b on a.s_id = b.s_id
where s_score < 60 group by s_id having count(1) >=2;
或者試試
select a.s_id ,b.s_name,round(avg(a.s_score),2) as 平均成績 from score a
left join student b on a.s_id = b.s_id
where a.s_score < 60 group by a.s_id having count(*) >=2;

-- 16、檢索"01"課程分數小於60,按分數降序排列的學生信息
select a.* ,b.c_id ,b.s_score from student a
left join score b on a.s_id = b.s_id
where b.c_id = ༽' and b.s_score < 60
order by b.s_score desc;

-- 17、按平均成績從高到低顯示所有學生的所有課程的成績以及平均成績
select a.s_name ,
sum(case when b.c_id = ༽' then s_score else null end ) as 語文,
sum(case when b.c_id = ༾' then s_score else null end ) as 數學,
sum(case when b.c_id = ༿' then s_score else null end ) as 英語,
round(avg(s_score),2) as 平均成績
from student a left join score b on a.s_id = b.s_id group by a.s_name
order by 平均成績 desc;

-- 18.查詢各科成績最高分、最低分和平均分:以如下形式顯示:課程ID,課程name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率
--及格為>=60,中等為:70-80,優良為:80-90,優秀為:>=90
select b.c_id,b.c_name,
max(a.s_score) as 最高分,
min(a.s_score) as 最低分,
round(avg(a.s_score),2) as 平均分,
round(sum(case when a.s_score>= 60 then 1 else 0 end)/count(s_id),2) as 及格率 ,
round(sum(case when a.s_score>= 70 and a.s_score <80 then 1 else 0 end)/count(s_id),2) as 中等率,
round(sum(case when a.s_score>= 80 and a.s_score <90 then 1 else 0 end)/count(s_id),2) as 優良率,
round(sum(case when a.s_score>= 90 then 1 else 0 end)/count(s_id),2) as 優秀率
from score a
left join course b
on a.c_id = b.c_id group by b.c_id;

-- 19、按各科成績進行排序,並顯示排名
第一種:
set @pre_c_id:= ༽'
set @rank:=0;
select tb2.s_id ,tb2.c_id,tb2.s_score,tb2.排名 from
(select *,(case when tb1.c_id = @pre_c_id then @rank:[email protected]+1 else @rank:=1 end) as 排名,
(case when @pre_c_id = tb1.c_id then @pre_c_id else @pre_c_id:=tb1.c_id end ) as pre_c_id
from
(select * from score order by c_id,s_score desc) tb1 )tb2;

如果看不懂用第二種方法:
SELECT a.c_id,a.s_id,a.s_score,COUNT(b.s_score)+1 AS 排名
FROM score a LEFT JOIN score b ON a.s_score<b.s_score AND a.c_id = b.c_id
GROUP BY a.c_id,a.s_id,a.s_score ORDER BY a.c_id,排名,a.s_id ASC

-- 20、查詢學生的總成績並進行排名
set @rank:=0;
select * ,(@rank:[email protected]+1) as rank from
(select s_id ,sum(s_score) as 總成績 from score
group by s_id order by 總成績 desc) tb1;

-- 21、查詢不同老師所教不同課程平均分從高到低顯示
select a.c_id, d.t_name,round(avg(a.s_score)) as 平均分 from score a
left join student b on a.s_id = b.s_id
left join course c on a.c_id = c.c_id
left join teacher d on c.t_id = d.t_id group by a.c_id
order by 平均分 desc;

-- 22、查詢所有課程的成績第2名到第3名的學生信息及該課程成績
set @pre_c_id:= ༽'
set @rank:=0;
select b.s_name,tb2.s_id ,tb2.c_id,tb2.s_score,tb2.排名 from
(select *,(case when tb1.c_id = @pre_c_id then @rank:[email protected]+1 else @rank:=1 end) as 排名,
(case when @pre_c_id = tb1.c_id then @pre_c_id else @pre_c_id:=tb1.c_id end ) as pre_c_id
from
(select * from score order by c_id,s_score desc) tb1 )tb2 join student b on tb2.s_id = b.s_id where 排名 = 2 or 排名 =3;

-- 23、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],(85-70],(70-60],(0-60]及所佔百分比
select b.c_id,b.c_name ,
sum(case when a.s_score >=85 then 1 else 0 end) as 100-85 ,
concat(round(100 sum(case when a.s_score >=85 then 1 else 0 end)/count( ),2), '%') as 百分比,
sum(case when a.s_score <85 and a.s_score >=70 then 1 else 0 end) as 85-70 ,
concat(round(100 sum(case when a.s_score <85 and a.s_score >=70 then 1 else 0 end)/count( ),2),'%') as 百分比,
sum(case when a.s_score <70 and a.s_score >=60 then 1 else 0 end) as 70-60 ,
concat(round(100 sum(case when a.s_score <70 and a.s_score >=60 then 1 else 0 end)/count( ),2) ,'%')as 百分比,
sum(case when a.s_score <60 and a.s_score >=0 then 1 else 0 end) as 60-0 ,
concat(round(100 sum(case when a.s_score <60 and a.s_score >=0 then 1
else 0 end)/count(
),2),'%') as 百分比
from score a left join course b on a.c_id = b.c_id group by b.c_id;

-- 24、查詢學生平均成績及其名次
select tb1.*,(@rank:[email protected] +1 ) as rank from
(select s_id ,round(avg(s_score),2) as 平均成績 from score
group by s_id order by 平均成績 desc) tb1,(select @rank:=0) b;

-- 25、查詢各科成績前三名的記錄
set @pre_c_id:= ༽'
set @rank:=0;
select b.s_name,tb2.s_id ,tb2.c_id,tb2.s_score,tb2.排名 from
(select *,(case when tb1.c_id = @pre_c_id then @rank:[email protected]+1 else @rank:=1 end) as 排名,
(case when @pre_c_id = tb1.c_id then @pre_c_id else @pre_c_id:=tb1.c_id end ) as pre_c_id
from
(select * from score order by c_id,s_score desc) tb1 )tb2 join student b on tb2.s_id = b.s_id where 排名 <4;

-- 26、查詢每門課程被選修的學生數
select c_id ,count(s_id) as 選修人數 from score group by c_id;

-- 27、查詢出只有兩門課程的全部學生的學號和姓名
select a.s_id ,b.s_name from score a left join student b on a.s_id = b.s_id group by s_id having count(*) = 2;

-- 28、查詢男生、女生人數
select sum(case s_sex when '男' then 1 else 0 end) as 男生人數,
sum(case s_sex when '女' then 1 else 0 end) as 女生人數 from student;

-- 29、查詢名字中含有"風"字的學生信息
select * from student where s_name like '%風%'

-- 30、查詢同名同性學生名單,並統計同名人數
--略,不想寫

-- 31、查詢1990年出生的學生名單
select * from student where s_birth like 񟬶%'

-- 32、查詢每門課程的平均成績,結果按平均成績降序排列,平均成績相同時,按課程編號升序排列
select c_id ,round(avg(s_score),2) as 平均成績 from score group by c_id order by 平均成績 desc, c_id asc;

-- 33、查詢平均成績大於等於85的所有學生的學號、姓名和平均成績
select a.s_id,b.s_name ,round(avg(s_score),2) as 平均成績 from score a
left join student b on a.s_id = b.s_id group by a.s_id having 平均成績>=85;

-- 34、查詢課程名稱為"數學",且分數低於60的學生姓名和分數
select b.s_name ,a.s_score from score a
left join student b on a.s_id = b.s_id
where a.c_id=(select c_id from course where c_name = '數學')and a.s_score < 60;

-- 35、查詢所有學生的課程及分數情況;
select b.s_name,
sum(case when a.c_id = ༽' then a.s_score else null end) as 語文,
sum(case when a.c_id = ༾' then a.s_score else null end) as 數學,
sum(case when a.c_id = ༿' then a.s_score else null end) as 英語
from score a right join student b on a.s_id = b.s_id group by b.s_name

-- 36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數;
select b.s_name,
sum(case when a.c_id = ༽' then a.s_score else null end) as 語文,
sum(case when a.c_id = ༾' then a.s_score else null end) as 數學,
sum(case when a.c_id = ༿' then a.s_score else null end) as 英語
from score a right join student b on a.s_id = b.s_id group by b.s_name having 語文>= 70 or 數學>= 70 or 英語>= 70 ;

-- 37、查詢不及格的課程
select a.s_id,a.c_id,b.c_name,a.s_score from score a
left join course b on a.c_id = b.c_id where a.s_score<60;

--38、查詢課程編號為01且課程成績在80分以上的學生的學號和姓名;
select a.s_id,b.s_name from score a left join student b on a.s_id = b.s_id where a.c_id = ༽' and a.s_score>=80;

-- 39、求每門課程的學生人數
select c_id,count(*) as 學生人數 from score group by c_id ;

-- 40、查詢選修"張三"老師所授課程的學生中,成績最高的學生信息及其成績
select a.*,b.c_id,max(b.s_score) as 最高成績 from student a
right join score b on a.s_id = b.s_id
group by b.c_id
having b.c_id = (select c_id from course
where t_id = (select t_id from teacher where t_name = '張三'));

-- 41、查詢不同課程成績相同的學生的學生編號、課程編號、學生成績
--(這題我搞不清題目是什麼意思,是指查找學生個體參加了的所有課程的成績各不相同的那個學生信息呢?還是所有課程之間做對比呢,我更傾向於理解為前者)

--理解為前者的寫法
select * from
(select * from score group by s_id,s_score) tb1
group by s_id having count(*) = 1;

--理解為後者的寫法
select distinct a.s_id,a.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score;

-- 42、查詢每門課程成績最好的前兩名
set @pre_c_id:= ༽'
set @rank:=0;
select tb2.s_id ,tb2.c_id,tb2.s_score from
(select *,(case when tb1.c_id = @pre_c_id then @rank:[email protected]+1 else @rank:=1 end) as 排名,
(case when @pre_c_id = tb1.c_id then @pre_c_id else @pre_c_id:=tb1.c_id end ) as pre_c_id
from
(select * from score order by c_id,s_score desc) tb1 )tb2
join student b on tb2.s_id = b.s_id where 排名 <3;

-- 43、統計每門課程的學生選修人數(超過5人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人相同,按課程號升序排列
select c_id ,count(*) as 選修人數 from score group by c_id having 選修人數>5 order by 選修人數 desc , c_id asc;

-- 44、檢索至少選修兩門課程的學生學號
select s_id from score group by s_id having count(*) >= 2;

-- 45、查詢選修了全部課程的學生信息
select * from student where s_id in
(select s_id from score group by s_id having count(*) = 3)

--46、查詢各學生的年齡
select s_name ,(date_format(now(),'%Y')-date_format(s_birth,'%Y') + (CASE when date_format(now(),'%m%d')>=date_format(s_birth,'%m%d') then 0 else 1 end)) as age
from student

-- 47、查詢本周過生日的學生
---(實現得並不完全,因為例如出生月日為『01-01』在每一年可能會輸入不同周)
select * from student where week(date_format(s_birth,'%m%d'))=week(date_format(now(),'%m%d')) ;

-- 48、查詢下周過生日的學生
select * from student
where week(date_format(s_birth,'%m%d'))=week(date_format(date_add(now(),interval 7-dayofweek(now())+1 day),'%m%d'));

-- 49、查詢本月過生日的學生
select * from student where date_format(s_birth,'%m') = date_format(now(),'%m')

-- 50、查詢下月過生日的學生
select * from student where date_format(s_birth,'%m') = date_format(date_add(now(),interval 1 month),'%m')

❸ SQL資料庫面試題 急急急

a)select pname as '商品名',avg(qty) as 平均銷售量 from s,p,m where m.city='上海' and s.mno=m.mno and p.pno=s.pno,select p.Pno,p.pname,sum(s.qty)
from s left join p on s.pno=p.pno left join m on p.Mno=m.Mno
where m.city='上海市'
group by p.Pno,p.pname,p.city,p.color
b)、先刪除Sale表的外鍵PNO,再刪除gds表。

c)聯系:視圖(view)是在基本表之上建立的表,它的結構(即所定義的列)和內容(即所有數據行)都來自基本表,它依據基本表存在而存在。一個視圖可以對應一個基本表,也可以對應多個基本表。視圖是基本表的抽象和在邏輯意義上建立的新關系
區別:1、視圖是已經編譯好的sql語句。而表不是
2、視圖沒有實際的物理記錄。而表有。
3、表是內容,視圖是窗口
4、表只用物理空間而視圖不佔用物理空間,視圖只是邏輯概念的存在,表可以及時四對它進行修改,但視圖只能有創建的語句來修改
5、表是內模式,視圖是外模式
6、視圖是查看數據表的一種方法,可以查詢數據表中某些欄位構成的數據,只是一些SQL語句的集合。從安全的角度說,視圖可以不給用戶接觸數據表,從而不知道表結構。
7、表屬於全局模式中的表,是實表;視圖屬於局部模式的表,是虛表。
8、視圖的建立和刪除隻影響視圖本身,不影響對應的基本表。

❹ 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 面試題

1. select s.title, count(p.id)
from 書表 s left join 評論 p on s.id=p.書表中的id
group by s.title

(注意:左外連接的作用是將評價數為0的書顯示出來.count(p.id)和count(*)的區別是count(p.id)不計入p.id為null的行)

2. select top 1 s.title, count(p.id)
from 書表 s left join 評論 p on s.id=p.書表中的id
group by s.title order by 2 desc

(以第2列倒序排序,取第1行)

❻ sql面試題 不會做 都進來看看吧

才兩張表,隨便寫寫就出來了 你是沒見過該死的bom 那才傷腦筋

--先建立測試用例:
create table users(Id Int,Name varchar(20),regDate date);
alter table users add constraint users_pk primary key(Id);
create table UploadInfo(Id Int,UserId Int,FileName varchar(20),foreign key(UserId) references users(Id));
insert into users values(1,'A',sysdate-1);
insert into users values(2,'B',sysdate-2);
insert into users values(3,'C',sysdate-3);
insert into users values(4,'D',sysdate-4);
insert into users values(5,'E',sysdate-5);
select*from users;

--隨機插入上傳數據的存儲過程
create or replace procere InsertUploadInfoData is
i int:=0;
begin
for i in 1..100 loop
insert into UploadInfo values(i,trunc(DBMS_RANDOM.value(1,5)),'FileName '||trunc(DBMS_RANDOM.value(1,1000)));
end loop;
commit;
end ;

begin
InsertUploadInfoData;
end;

select*from UploadInfo;

--開始了

--第一題
select UserUploadRecord.Id,UserUploadRecord.UploadCount
from
(
select users.Id,count(UploadInfo.Id) as UploadCount from users
inner join UploadInfo on users.Id=UploadInfo.UserId --內聯結
where users.regDate>=sysdate-3 and users.regDate<=sysdate --三天內
group by users.Id
order by UploadCount desc
) UserUploadRecord
where rownum=1 --取第一條記錄
;

--創建用戶名重復的測試用例
insert into users values(6,'A',sysdate-4);

--由於有外鍵約束,所以需先刪除子表記錄
delete from UploadInfo where UploadInfo.Userid
in
(
select users.id from users where (users.name,users.regdate) in
(
select users.name,max(users.regdate) from users group by users.name having count(users.id)>1 --取重復用戶名的最大注冊時間
)
)

--再刪除父表,做法差不多
delete from users where (users.name,users.regdate) in
(
select users.name,max(users.regdate) from users group by users.name having count(users.id)>1
)

————————————————————
建表語法 測試用例都寫給你了 還說運行不了?直接復制進去就行了

❼ SQL語句面試題

SELECT*,
(SELECTCOUNT(*)FROM(SELECTCOUNT(*),b.sidFROMscbLEFTJOINcoursecONb.Cid=c.CidGROUPBYb.sid,c.tid)owWHEREow.sid=a.sid)as'
選課數量',
(SELECTsum(Score)FROMScdWHEREa.sid=d.sid)as'總成績'
FROMstudenta
;

SELECTd.sid,d.snamefromteachera
LEFTJOINcoursebona.tid=b.tid
LEFTJOINscconc.cid=b.cid
leftJOINstudentdONd.sid=c.sid
WHEREa.tname='葉萍';

SELECTsid,sname
FROMstudent
WHEREsidin(selecta.sid
FROMscAleftjoinscbona.sid=b.sid
WHEREa.cid=1andb.cid=2anda.score>b.score)

SELECTsc.Sid,sum(CASEWHENc.Cname='
數學'thensc.Scoreelse0end)數學,SUM(casewhenc.Cname='物理
'THENsc.ScoreELSE0END)物理,AVG(sc.Score)平均分
FROMsc
INNERJOINCourseconsc.Cid=c.Cid
WHEREc.Cnamein('數學','物理')
GROUPBYsc.Sid
ORDERBYAVG(sc.Score)DESC

insertintoSc(Sid,Cid,Score)values(003,3,85);

insertintoSc(Sid,Cid,Score)values(003,3,30);

首先這個不知道是你i寫錯了還是怎麼一個人化學成績有2個所以我在這里按照你這個上做的查詢所以有一個人是選了單個課程

❽ sql面試題

1.
select
s.title,
count(p.id)
from
書表
s
left
join
評論
p
on
s.id=p.書表中的id
group
by
s.title
(注意:左外連接的作用是將評價數為0的書顯示出來.count(p.id)和count(*)的區別是count(p.id)不計入p.id為null的行)
2.
select
top
1
s.title,
count(p.id)
from
書表
s
left
join
評論
p
on
s.id=p.書表中的id
group
by
s.title
order
by
2
desc
(以第2列倒序排序,取第1行)

❾ sql面試題 原表 張三 語文 30 張三 數學 40 張三 英語 70 李斯特 語文 40 李斯特 數學 50 李斯特 英語 60

create table tb(姓名 varchar(10) , 課程 varchar(10) , 分數 int) insert into tb values('張三' , '語文' , 74) insert into tb values('張三' , '數學' , 83) insert into tb values('張三' , '物理' , 93) insert into tb values('李四' , '語文' , 74) insert into tb values('李四' , '數學' , 84) insert into tb values('李四' , '物理' , 94) go --SQL SERVER 2000 靜態SQL,指課程只有語文、數學、物理這三門課程。(以下同) select 姓名 as 姓名 , max(case 課程 when '語文' then 分數 else 0 end) 語文, max(case 課程 when '數學' then 分數 else 0 end) 數學, max(case 課程 when '物理' then 分數 else 0 end) 物理 from tb group by 姓名 --SQL SERVER 2000 動態SQL,指課程不止語文、數學、物理這三門課程。(以下同) declare @sql varchar(8000) set @sql = 'select 姓名 ' select @sql = @sql + ' , max(case 課程 when ''' + 課程 + ''' then 分數 else 0 end) [' + 課程 + ']' from (select distinct 課程 from tb) as a set @sql = @sql + ' from tb group by 姓名' exec(@sql) --SQL SERVER 2005 靜態SQL。 select * from (select * from tb) a pivot (max(分數) for 課程 in (語文,數學,物理)) b --SQL SERVER 2005 動態SQL。 declare @sql varchar(8000) select @sql = isnull(@sql + '],[' , '') + 課程 from tb group by 課程 set @sql = '[' + @sql + ']' exec ('select * from (select * from tb) a pivot (max(分數) for 課程 in (' + @sql + ')) b') 很不錯哦,你可以試下
c蟆qf唷c蟆f唷gЛ錠s姚m┈炸Лm┈炸ЛgЛ錠42511174322011-9-16 0:35:47