數據庫編程基本練習題

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

準備數據的sql代碼:sql

create table score(
id int primary key auto_increment,
name varchar(20),
subject varchar(20),
score int);

insert into score values
(null,'張三','語文',81),
(null,'張三','數學',75),
(null,'李四','語文',76),
(null,'李四','數學',90),
(null,'王五','語文',81),
(null,'王五','數學',100),
(null,'王五 ','英語',90);

答案:數據庫

select distinct name from score where name not in (select distinct name from score where score<=80)

二、每月份的發生額都比101科目多的科目

請用SQL語句實現:從TestDB數據表中查詢出全部月份的發生額都比101科目相應月份的發生額高的科目。請注意:TestDB中有不少科目,都有1-12月份的發生額。 AccID:科目代碼,Occmonth:發生額月份,DebitOccur:發生額。 數據庫名:JcyAudit,數據集:Select * from TestDB函數

準備數據的sql代碼:

drop table if exists TestDB;
create table TestDB(
id int primary key auto_increment,
AccID varchar(20), 
Occmonth date, 
DebitOccur bigint);

insert into TestDB values
(null,'101','1988-1-1',100),
(null,'101','1988-2-1',110),
(null,'101','1988-3-1',120),
(null,'101','1988-4-1',100),
(null,'101','1988-5-1',100),
(null,'101','1988-6-1',100),
(null,'101','1988-7-1',100),
(null,'101','1988-8-1',100);

--複製上面的數據,故意把第一個月份的發生額數字改小一點
insert into TestDB values
(null,'102','1988-1-1',90),
(null,'102','1988-2-1',110),
(null,'102','1988-3-1',120),
(null,'102','1988-4-1',100),
(null,'102','1988-5-1',100),
(null,'102','1988-6-1',100),
(null,'102','1988-7-1',100),
(null,'102','1988-8-1',100);

--複製最上面的數據,故意把全部發生額數字改大一點
insert into TestDB values
(null,'103','1988-1-1',150),
(null,'103','1988-2-1',160),
(null,'103','1988-3-1',180),
(null,'103','1988-4-1',120),
(null,'103','1988-5-1',120),
(null,'103','1988-6-1',120),
(null,'103','1988-7-1',120),
(null,'103','1988-8-1',120);

--複製最上面的數據,故意把全部發生額數字改大一點
insert into TestDB values
(null,'104','1988-1-1',130),
(null,'104','1988-2-1',130),
(null,'104','1988-3-1',140),
(null,'104','1988-4-1',150),
(null,'104','1988-5-1',160),
(null,'104','1988-6-1',170),
(null,'104','1988-7-1',180),
(null,'104','1988-8-1',140);

--複製最上面的數據,故意把第二個月份的發生額數字改小一點
insert into TestDB values
(null,'105','1988-1-1',100),
(null,'105','1988-2-1',80),
(null,'105','1988-3-1',120),
(null,'105','1988-4-1',100),
(null,'105','1988-5-1',100),
(null,'105','1988-6-1',100),
(null,'105','1988-7-1',100),
(null,'105','1988-8-1',100);

答案:post

select distinct AccID from TestDB
where AccID not in
(select TestDB.AccIDfrom TestDB,
(select * from TestDB where AccID='101') as db101
where TestDB.Occmonth=db101.Occmonth 
and TestDB.DebitOccur<=db101.DebitOccur
);

三、統計每一年每個月的信息

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 
準備sql語句:

drop table if exists sales;
create table sales(
id int auto_increment primary key,
year varchar(10), 
month varchar(10),
amount float(2,1));

insert into sales values
(null,'1991','1',1.1),
(null,'1991','2',1.2),
(null,'1991','3',1.3),
(null,'1991','4',1.4),
(null,'1992','1',2.1),
(null,'1992','2',2.2),
(null,'1992','3',2.3),
(null,'1992','4',2.4);

答案:學習

select sales.year,
(select t.amount from sales as t where t.month='1' and t.year = sales.year) as 'm1',
(select t.amount from sales as t where t.month='2' and t.year = sales.year) as 'm2',
(select t.amount from sales as t where t.month='3' and t.year = sales.year) as 'm3',
(select t.amount from sales as t where t.month='4' and t.year = sales.year) as 'm4'
from sales group by year

 四、顯示文章標題,發帖人、最後回覆時間

表:id,title,postuser,postdate,parentidthis

準備sql語句:

drop table if exists articles;

create table articles(
id int auto_increment primary key,
title varchar(50), 
postuser varchar(10), 
postdate datetime,
parentid int references articles(id)
);

insert into articles values
(null,'第一條','張三','1998-10-10 12:32:32',null),
(null,'第二條','張三','1998-10-10 12:34:32',null),
(null,'第一條回覆1','李四','1998-10-10 12:35:32',1),
(null,'第二條回覆1','李四','1998-10-10 12:36:32',2),
(null,'第一條回覆2','王五','1998-10-10 12:37:32',1),
(null,'第一條回覆3','李四','1998-10-10 12:38:32',1),
(null,'第二條回覆2','李四','1998-10-10 12:39:32',2),
(null,'第一條回覆4','王五','1998-10-10 12:39:40',1);

答案:spa

select zhu.title,zhu.postuser,
(select max(fu.postdate) from articles as fu where zhu.id = fu.parentid) as postdata
from articles as zhu 
where zhu.parentid is null

五、刪除除了id號不一樣,其餘都相同的學生冗餘信息

學生表 以下: 
id號         學號          姓名       課程編號       課程名稱       分數  
 1         2005001        張三        0001          數學         69  
 2         2005002        李四        0001          數學         89 
 3         2005001        張三        0001          數學         69 
準備數據:
create table student2(
id int auto_increment primary key,
code varchar(20),
name varchar(20)
);

insert into student2 values
(null,'2005001','張三'),
(null,'2005002','李四'),
(null,'2005001','張三');

答案:code

delete from student2 
where id
not in(select min(id) from (select * from student2) as t group by t.name);

 六、航空網的幾個航班查詢題:

實驗環境:
create table city(
cityID int auto_increment primary key,
cityName varchar(20));

create table flight (
flightID int auto_increment primary key,
StartCityID int references city(cityID),
endCityID  int references city(cityID),
StartTime timestamp);

//航班原本應該沒有日期部分纔好,可是下面的題目當中涉及到了日期
insert into city values
(null,'北京'),
(null,'上海'),
(null,'廣州');

insert into flight values
(null,1,2,'9:37:23'),
(null,1,3,'9:37:23'),
(null,1,2,'10:37:23'),
(null,2,3,'10:37:23');

(1) 查詢起飛城市是北京的全部航班,按到達城市的名字排序blog

答案:排序

select * from flight as f,city as c
where f.endCityID = c.cityID
and StartCityID = (select cityID from city where cityName = "北京")
order by endCityID desc

(2)查詢北京到上海的全部航班紀錄(起飛城市,到達城市,起飛時間,航班號)

答案:

select c1.cityName as '起飛城市',c2.cityName as '到大城市',f.StartTime as '到大城市',f.flightID as '航班號'
from city as c1,city as c2,flight f
where c1.cityID = f.StartCityID
and c2.cityID = f.endCityID
and c1.cityName = "北京"
and c2.cityName = "上海"

(3)查詢具體某一天(2005-5-8)的北京到上海的的航班次數

答案:

select count(*) 
from city as c1,city as c2,flight as f
where c1.cityID =  f.StartCityID
and c2.cityID = f.endCityID
and c1.cityName = "北京"
and c2.cityName = "上海" 
and 查幫助得到的某個日期處理函數(startTime) like '2005-5-8%'

 七、查出比經理薪水還高的員工信息:

準備數據:
Drop table if not exists employees;
create table employees(
id int primary key auto_increment,
name varchar(50),
salary int,
managerid int references employees(id));

insert into employees values 
(null,' lhm',10000,null), 
(null,' zxx',15000,1),
(null,'flx',9000,1),
(null,'tg',10000,2),
(null,'wzg',10000,3);

Wzg大於flx,lhm大於zxx

 

答案:

select e.* from employees as m,employees as e 
where m.id = e.managerid
and m.salary < e.salary

八、求出小於45歲的各個老師所帶的大於12歲的學生人數

實驗數據:
drop table if exists tea_stu;
drop table if exists teacher;
drop table if exists student;

create table teacher(
teaID int primary key,
name varchar(50),
age int);

create table student(
stuID int primary key,
name varchar(50),
age int);

create table tea_stu(
teaID int references teacher(teaID),
stuID int references student(stuID));

insert into teacher values
(1,'zxx',45), 
(2,'lhm',25) , 
(3,'wzg',26) , 
(4,'tg',27);

insert into student values
(1,'wy',11), 
(2,'dh',25) , 
(3,'ysq',26) , 
(4,'mxc',27);

insert into tea_stu values
(1,1), 
(1,2), 
(1,3);

insert into tea_stu values
(2,2), 
(2,3), 
(2,4);

 insert into tea_stu values
(3,3), 
(3,4), 
(3,1);

insert into tea_stu values
(4,4), 
(4,1), 
(4,2), 
(4,3);

答案:

select teacher.name,count(student.name) as count from teacher,student,tea_stu
where teacher.teaID = tea_stu.teaID
and student.stuID = tea_stu.stuID
and teacher.age < 45
and student.age > 12
group by teacher.name

九、一個用戶表中有一個積分字段,假如數據庫中有100多萬個用戶,若要在每一年第一天凌晨將積分清零,你將考慮什麼,你將想什麼辦法解決?

alter table drop column score;

alter table add colunm score int;

可能會很快,可是須要試驗,試驗不能拿真實的環境來操刀,而且要注意,

這樣的操做時沒法回滾的,在個人印象中,只有inert update delete等DML語句才能回滾,

對於create table,drop table ,alter table等DDL語句是不能回滾。

解決方案一,
update user set score=0;

解決方案二,假設上面的代碼要執行好長時間,超出咱們的容忍範圍,那我就
alter
table user drop column score;alter table user add column score int

下面代碼實現每一年的那個凌晨時刻進行清零

Runnable runnable =
      new Runnable(){
            public void run(){
                  clearDb();
                  schedule(this,new Date(new Date().getYear()+1,0,0));
            }          
       };
schedule(runnable,new Date(new Date().getYear()+1,0,1));

 十、用一條SQL 語句 查詢出每門課都大於80 分的學生姓名

name   course   grade 
張三       語文       81 
張三       數學       75 
李四       語文       76 
李四       數學       90 
王五       語文       81 
王五       數學      100 
王五       英語       90
  

答案:

select name from table group by name having min(grade) > 80

11. 現有學生表以下: 

自動編號         學號     姓名      課程編號       課程名稱       分數 
1            2005001    張三        0001         數學          69 
2            2005002    李四        0001         數學          89 
3            2005001    張三        0001         數學          69 

刪除除了自動編號不一樣, 其餘都相同的學生冗餘信息

答案:

select 自動編號 fromwhere 自動編號 not in 
(select min(自動編號) fromgroup by 學號,姓名,課程編號,課程名稱,分數)

十二、一個叫 team 的表,裏面只有一個字段name, 一共有4 條紀錄,分別是a,b,c,d, 對應四個球對,如今四個球對進行比賽,用一條sql 語句顯示全部可能的比賽組合

select a.name as '主隊',b.name as '客隊' from team as a,team as b
where a.name < b.name

1三、查詢」 01 「課程比」 02 「課程成績高的學生的信息及課程分數

實驗數據:

學生表:
create table Student(
Sid varchar(6), 
Sname varchar(10), 
Sage datetime, 
Ssex varchar(10));
insert into Student values('01' , '趙雷' , '1990-01-01' , '');
insert into Student values('02' , '錢電' , '1990-12-21' , '');
insert into Student values('03' , '孫風' , '1990-05-20' , '');
insert into Student values('04' , '李雲' , '1990-08-06' , '');
insert into Student values('05' , '周梅' , '1991-12-01' , '');
insert into Student values('06' , '吳蘭' , '1992-03-01' , '');
insert into Student values('07' , '鄭竹' , '1989-07-01' , '');
insert into Student values('08' , '王菊' , '1990-01-20' , '')
成績表
create table SC(
Sid varchar(10),
Cid varchar(10), 
score decimal(18,1));
insert into SC values('01' , '01' , 80);
insert into SC values('01' , '02' , 90);
insert into SC values('01' , '03' , 99);
insert into SC values('02' , '01' , 70);
insert into SC values('02' , '02' , 60);
insert into SC values('02' , '03' , 80);
insert into SC values('03' , '01' , 80);
insert into SC values('03' , '02' , 80);
insert into SC values('03' , '03' , 80);
insert into SC values('04' , '01' , 50);
insert into SC values('04' , '02' , 30);
insert into SC values('04' , '03' , 20);
insert into SC values('05' , '01' , 76);
insert into SC values('05' , '02' , 87);
insert into SC values('06' , '01' , 31);
insert into SC values('06' , '03' , 34);
insert into SC values('07' , '02' , 89);
insert into SC values('07' , '03' , 98)
課程表
create table Course(
Cid varchar(10),
Cname varchar(10),
Tid varchar(10));
insert into Course values
('01' , '語文' , '02');
insert into Course values
('02' , '數學' , '01');
insert into Course values
('03' , '英語' , '03')
教師表
create table Teacher(
Tid varchar(10),
Tname varchar(10));
insert into Teacher values
('01' , '張三');
insert into Teacher values
('02' , '李四');
insert into Teacher values
('03' , '王五')

1四、 根據(13題)表查詢平均成績大於等於 60 分的同窗的學生編號和學生姓名和平均成績

答案:

select s.Sid,s.Sname,avg(score) as a from Student as s,SC
where s.Sid = SC.Sid 
group by s.Sid having a>60

1五、根據(13題)表查詢在 SC 表存在成績的學生信息

答案:

select s.* from Student as s,SC where s.Sid = SC.Sid group by Sid

1六、查詢全部同窗的學生編號、學生姓名、選課總數、全部課程的總成績(沒成績的顯示爲 null )

答案:

select s.Sid,s.Sname,count(SC.Cid),sum(score) from Student as s left join SC on s.Sid = SC.Sid group by s.Sid

1七、查有成績的學生信息

答案:

select s.Sid,S.Sname,count(score),sum(score),
sum(case when SC.Cid=01 then score else null end) as score_1,
sum(case when SC.Cid=02 then score else null end) as score_2,
sum(case when SC.Cid=03 then score else null end) as score_3
from Student as s,SC,Course where s.Sid = SC.Sid
and SC.Cid = Course.Cid group by s.Sid

1八、查詢「李」姓老師的數量

答案:

select count(*) from Teacher where Tname like '李%'

1九、查詢學過「張三」老師授課的同窗的信息

select s.* from Student as s,SC,course,Teacher
where s.Sid=SC.Sid 
and Course.Cid=SC.Cid
and Teacher.Tid=Course.Tid
and Teacher.Tname = '張三'
group by s.Sid

 20、查詢沒有學全全部課程的同窗的信息

select * from Student where Sid  in 
(select Sid from SC group by Sid having count(cid) < 3)

2一、查詢至少有一門課與學號爲」 01 「的同窗所學相同的同窗的信息

select s.* from SC,Student as s where Cid in 
(select Cid from SC where Sid = '01')
and s.Sid = SC.Sid
group by Sid

2二、查詢沒學過」張三」老師講授的任一門課程的學生姓名

select Sname from Student where Sname not in
(select s.Sname from Teacher as t,SC,Course,Student as s
where t.Tid = Course.Tid
and s.Sid=SC.Sid
and SC.Cid=Course.Cid
and t.Tname = '張三')

2三、查詢兩門及其以上不及格課程的同窗的學號,姓名及其平均成績

select s.Sid,s.Sname,avg(score) from Student as s,SC
where s.Sid = SC.Sid
and SC.score < 60
group by s.Sid 
having count(score > 2)

 2四、檢索」 01 「課程分數小於 60,按分數降序排列的學生信息

select s.*,SC.score from Student as s,SC 
where Cid = '01'
and s.Sid=SC.Sid 
and score < 60
group by score desc

2五、按平均成績從高到低顯示全部學生的全部課程的成績以及平均成績

select Sid,
sum(case when Cid=01 then score else null end) as score_01,
sum(case when Cid=02 then score else null end) as score_02,
sum(case when Cid=03 then score else null end) as score_03,
avg(score)
from SC group by Sid
order by avg(score) desc

2六、查詢各科成績最高分、最低分和平均分,以以下形式顯示:課程 ID,課程 name,最高分,最低分,平均分,及格率,中等率,優良率,優秀率(及格爲>=60,中等爲:70-80,優良爲:80-90,優秀爲:>=90)。 要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列

select c.cid as 課程號, c.cname as 課程名稱, count(*) as 選修人數,
max(score) as 最高分, min(score) as 最低分, avg(score) as 平均分,
sum(case when score >= 60 then 1 else 0 end)/count(*) as 及格率,
sum(case when score >= 70 and score < 80 then 1 else 0 end)/count(*) as 中等率,
sum(case when score >= 80 and score < 90 then 1 else 0 end)/count(*) as 優良率,
sum(case when score >= 90 then 1 else 0 end)/count(*) as 優秀率
from sc, course c
where c.cid = sc.cid
group by c.cid
order by count(*) desc, c.cid asc

2七、統計各科成績各分數段人數:課程編號,課程名稱,[100-85],[85-70],[70-60],[60-0] 及所佔百分比

select c.Cid as 課程編號, c.Cname as 課程名稱, A.*
from course as c,
(select Cid,
sum(case when score >= 85 then 1 else 0 end)/count(*) as 100_85,
sum(case when score >= 70 and score < 85 then 1 else 0 end)/count(*) as 85_70,
sum(case when score >= 60 and score < 70 then 1 else 0 end)/count(*) as 70_60,
sum(case when score < 60 then 1 else 0 end)/count(*) as 60_0
from SC group by Cid) as A
where c.Cid = A.Cid

2八、查詢出只選修兩門課程的學生學號和姓名

select s.Sid,s.Sname,count(sc.Cid) 
from Student as s,SC as sc
where s.Sid = sc.Sid 
group by s.Sid having count(sc.Cid) = 2

2九、查詢名字中含有「風」字的學生信息

select Sname from Student where Sname like '%風%'

30、查詢 1990 年出生的學生名單

select * from Student where Sage like('1990%')

3一、成績不重複,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績

select s.* from Student as s,Teacher as t,SC as sc,Course as c
where s.Sid=sc.Sid
and c.Tid=t.Tid
and sc.Cid = c.Cid
and t.Tname='張三'
order by sc.score desc limit 0,1

3二、成績有重複的狀況下,查詢選修「張三」老師所授課程的學生中,成績最高的學生信息及其成績

select s.* from Student as s,SC
where SC.Sid=s.Sid 
and SC.Cid = (select c.Cid from Teacher as t,Course as c
where t.Tid = c.Tid
and t.Tname = '張三')
and SC.score =
(
select sc.score from Student as s,Teacher as t,SC as sc,Course as c
where s.Sid=sc.Sid
and c.Tid=t.Tid
and sc.Cid = c.Cid
and t.Tname='張三'
order by sc.score desc limit 0,1
)

3三、查詢各學生的年齡,只按年份來算

select Sname,year(now())-year(Sage) as age from Student 

3四、按照出生日期來算,當前月日 < 出生年月的月日則,年齡減一

select Sname,year(now())-year(Sage)-1 as age from Student 

3五、查詢本週過生日的學生

select Sname from Student where week(now()) = week(Sage)

3六、查詢下週過生日的學生

select Sname from Student where (week(now())+1) = week(Sage)

3七、查詢本月過生日的學生

select Sname from Student where month(now()) = month(Sage)

3八、查詢下月過生日的學生

select Sname from Student where (month(now())+1) = month(Sage)

3九、查詢和」 01 「號的同窗學習的課程徹底相同的其餘同窗的信息 

select * from Student where sid in
(select sid from SC where cid in
(select sc.Cid from SC where sc.Sid='01') 
and sid <>'01'
group by sid 
having count(cid) >= 3
)
相關文章
相關標籤/搜索