SQL复杂查询练习

环境:MySQL5.0+

实例一:银行系统

若有问题欢迎指正

下面提供表结构和一些数据,直接使用即可。

-- branch
create table branch
(branch_name varchar(20) not null,
branch_city varchar(20),
assets int,
constraint primary key(branch_name)
);
-- account
create table account
(account_number int not null auto_increment,
branch_name varchar(20),
balance int,
constraint primary key(account_number),
constraint foreign key(branch_name)references branch(branch_name) on delete cascade
);
-- depositor
create table depositor
(customer_name varchar(20),
account_number int,
constraint foreign key(account_number) references account(account_number) on delete cascade
);
-- customer  主键默认非空不可重复
create table customer
(customer_name varchar(20),
customer_street varchar(20),
customer_city varchar(20),
constraint primary key(customer_name)
);
-- loan
create table loan
(loan_number int,
branch_name varchar(20),
amount int,
constraint foreign key(branch_name) references branch(branch_name) on delete cascade,
constraint primary key(loan_number)
);
-- borrower
create table borrower
(customer_name varchar(20),
loan_number int,
constraint foreign key(customer_name) references customer(customer_name) on delete cascade,
constraint foreign key(loan_number) references loan(loan_number) on delete cascade
);
-----------------------data
-- customer
insert into customer values('Adams','Spring','Pittsfield');
insert into customer values('Brooks','Senator','Brooklyn');
insert into customer values('Curry','North','Rye');
insert into customer values('Glenn','Sand Hill','Woodside');
insert into customer values('Green','Walnut','Stamford');
insert into customer values('Hayes','Main','Harrison');
insert into customer values('Johnson','Alma','Palo Alto');
insert into customer values('Jones','Main','Harrison');
insert into customer values('Lindasy','Park','Pittsfield');
insert into customer values('Smith','North','Rye');
insert into customer values('Turner','Putnam','Stamford');
insert into customer values('Willianms','Nassau','Princeton');
-- branch
insert into branch values('Brighton','Brooklyn',7100000);
insert into branch values('Downtown','Brooklyn',9000000);
insert into branch values('Mianus','Horseneck',400000);
insert into branch values('North Town','Rye',3700000);
insert into branch values('Perryridge','Horseneck',1700000);
insert into branch values('Pownal','Bennington',300000);
insert into branch values('Redwood','Palo Alto',2100000);
insert into branch values('Round Hill','Horseneck',8000000);
-- account
insert into account values(101,'Downtown',500);
insert into account values(102,'Perryridge',400);
insert into account values(201,'Brighton',900);
insert into account values(215,'Mianus',700);
insert into account values(217,'Brighton',750);
insert into account values(222,'Redwood',700);
insert into account values(305,'Round Hill',350);
-- depositor
insert into depositor values('Hayes',102);
insert into depositor values('Johnson',101);
insert into depositor values('Johnson',201);
insert into depositor values('Jones',217);
insert into depositor values('Lindsay',222);
insert into depositor values('Smith',215);
insert into depositor values('Turner',305);
-- loan
insert into loan values(11,'Round Hill',900);
insert into loan values(14,'Downtown',1500);
insert into loan values(15,'Perryridge',1500);
insert into loan values(16,'Perryridge',1300);
insert into loan values(17,'Downtown',1000);
insert into loan values(23,'Redwood',2000);
insert into loan values(93,'Mianus',500);
-- borrower
insert into borrower values('Adams',16);
insert into borrower values('Curry',93);
insert into borrower values('Hayes',15);
--insert into borrower values('Jacson',14);
insert into borrower values('Jones',17);
insert into borrower values('Smith',11);
insert into borrower values('Smith',23);
--insert into borrower values('Williams',17);



-----------------------query
1、Find the name, loan number and loan amount of all customers; rename the column name loan_number as loan_id.


select customer_name,loan.loan_number loan_id,amount
from loan join borrower on loan.loan_number = borrower.loan_number;


2、Find the names of all customers whose street includes the substring “Main”.
select customer_name from customer
where customer_street like "%Main%";


3、Find all customers who have a loan, an account, or both:
(select customer_name from depositor)
union
(select customer_name from borrower);


4、Find all customers who have both a loan and an account.
(select customer_name from depositor)
intersect
(select customer_name from borrower);


5、Find all customers who have an account but no loan.
(select customer_name from depositor)
except
(select customer_name from borrower);


6、Find the average account balance at the Perryridge branch.
select avg(balance) from account
where branch_name = 'Perryridge';


7、Find the number of tuples in the customer relation.
select count(*) from customer;


8、Find the number of depositors in the bank.
select count(*) from depositor,account
where account.account_number = depositor.account_number;


9、Find the number of depositors for each branch.
select branch_name,count(*) from depositor,account
where account.account_number = depositor.account_number
group by branch_name;


10、Find the names of all branches where the average account balance is more than $1,200.
select branch_name,avg(balance)
from account 
group by branch_name 
having avg(balance)>1200;


select account.branch_name,avg(balance)
from account join branch on branch.branch_name = account.branch_name 
group by branch_city
having avg(balance)>1200;


11、Find all loan number which appear in the loan relation with null values for amount.
select loan_number from loan where amount is null;


12、Find all customers who have both an account and a loan at the bank.
(select customer_name from depositor)
intersect
(select customer_name from borrower);


13、Find all customers who have a loan at the bank but do not have an account at the bank
(select customer_name from borrower)
except
(select customer_name from depositor);


14、Find all customers who have both an account and a loan at the Perryridge branch
(select customer.customer_name 
from customer join depositor on customer.customer_name = depositor.customer_name
join account on account.account_number = depositor.account_number
and branch_name = 'Perryridge';)  
intersect
(select customer.customer_name 
from customer join borrower on customer.customer_name = borrower.customer_name
join loan on borrower.loan_number = loan.loan_number
and branch_name = 'Perryridge';) 


15、Find all branches that have greater assets than some branch located in Brooklyn.
select distinct a.branch_name from branch a,branch b
where b.branch_city = 'Brooklyn' and a.branch_city != 'Brooklyn'
and a.assets > b.assets;

select branch_name from branch
where branch_name != 'Brooklyn' and 
assets > some(select assets from branch where branch_city = 'Brooklyn');


16、Find the names of all branches that have greater assets than all branches located in Brooklyn.
select branch_name from branch
where branch_name != 'Brooklyn' and 
assets > all(select assets from branch where branch_city = 'Brooklyn');

实例二:选课系统

表结构和数据:

-- 学生表:
create table Student(
Sno varchar(11) primary key,
Sname varchar(20),
Ssex varchar(2),
Sage int,
Sdept varchar(20),
constraint che_sex check(Ssex in ('男','女')),
constraint che_age check(Sage between 18 and 22)
);
-- 课程表:
create table Course(
Cno varchar(4) primary key,
Cname varchar(20),
Ctime int,
Ccredit int(10)
);
-- 学生选课表:
create table SC(
Sno varchar(11),
Cno varchar(4),
Grade int,
constraint foreign key (Sno) references Student(Sno),
constraint foreign key (Cno) references Course(Cno)
);
-----------data
--Student
insert into Student values('001','Zhang xin','男',18,'computer science');
insert into Student values('002','Wang peng','男',19,'electronic');
insert into Student values('003','Li na','女',20,'Information science');
insert into Student values('004','Ma fei','女',18,'computer science');
insert into Student values('005','Zuo li','男',19,'Information science');
insert into Student values('006','Zhang hai','女',21,'Information science');
insert into Student values('007','Li jian','男',20,'electronic');
insert into Student values('008','Wang peng','男',20,'computer science');
insert into Student values('009','Li qiang','男',18,'electronic');

--Course
insert into Course values('C01','Database System',48,1);
insert into Course values('C02','Internet',64,2);
insert into Course values('C03','Programming in Java',72,1);
insert into Course values('C04','ITIM',36,2);
insert into Course values('C05','Bibliometrics',54,3);
insert into Course values('C06','FPE',48,1);
insert into Course values('C07','LARS',36,2);

--SC
insert into SC values('001','C01',78);
insert into SC values('001','C02',64);
insert into SC values('002','C03',57);
insert into SC values('002','C04',89);
insert into SC values('002','C05',97);
insert into SC values('003','C04',null);
insert into SC values('004','C06',26);
insert into SC values('004','C07',58);
insert into SC values('005','C02',85);
insert into SC values('006','C03',85);
insert into SC values('006','C06',75);
insert into SC values('007','C07',null);
insert into SC values('007','C02',88);
insert into SC values('008','C03',52);
insert into SC values('009','C03',93);
insert into SC values('009','C01',63);
insert into SC values('009','C02',73);

----------
1、查询所有选修过“Bibliometrics”课的学生的姓名和成绩;
select sname,grade
from student,sc,course
where student.sno = sc.sno
and cname = 'Bibliometrics'
and course.cno = sc.cno;

2、查询考试成绩不及格的学生的个数;
select count(sno) from sc
where grade < 60;

3、查询名字中至少含有一个“z”字符的学生的姓名、学号和性别;
select * from student 
where sname like '%z%';

4、查询选修了“Introduction to the Internet”课程的学生的学号及其成绩,查询结果按分数的降序排列;
select sc.sno,grade from sc
join course on sc.cno = course.cno
and cname = 'Internet'
order by grade desc;

5、查询“Zuo li”同学选修课程的总学时(time)数
select sum(ctime) from course
where cno in
(select cno from student
join sc on student.sno = sc.sno and sname = 'Zuo li');


6、查询年龄不大于20岁的学生的平均考试成绩;
select avg(grade) from sc
where sno in(select sno from student where sage <= 20);


7、查询 “computer science”专业学生选修 “Database System”的人数;
select count(sno) from sc where sno in
(select sno from student where sdept = 'computer science' );


8、查询同时选修课程“Database System”和“Introduction to the Internet”的学生姓名;
(select sname from student 
join sc on student.sno = sc.sno
join course on course.cno = sc.cno
and cname = 'Database System')
intersect
(select sname from student 
join sc on student.sno = sc.sno
join course on course.cno = sc.cno
and cname = 'Internet'
);

9、查询选修的课程中含有“Wang gang”同学所有选修课程的学生姓名。 wanggang-所有同学 not exist

select sno,sname from student as eachS  
where not exist
((select cno from sc       
join student on sc.sno = student.sno
and sname = 'Wang gang')
except
(select cno from student   
join sc on son student.sno = student.sno
and sc.sno = eachS.sno  
));

10、查询“Information Technology for Information Management”考试成绩为空的学生姓名及专业名称。

select sname,sdept
from student,sc
where student.sno = sc.sno
and sc.grade is null;

11、查询“computer science”专业学生每个人的选修课总学分。

select st1.sname,sum(grade) from sc sc1,student st1
where st1.sno = sc1.sno
and st1.sno in
(select student.sno   
from student where sdept = 'computer science')
group by st1.sno;

12、查询个人考试平均成绩高于专业平均成绩的学生姓名

select sname from 
(select sname,sdept,avg(grade) a_g from student,sc      
where student.sno = sc.sno 
group by sname) as S,
(select sdept,avg(grade) a_g from student,sc   
where student.sno = sc.sno 
group by sdept) as D
where S.sdept = D.sdept
and S.a_g > D.a_g;


13、查询个人考试平均成绩高于女生平均成绩的男生姓名

select sname from 
--查询所有男生的平均成绩,结果集用S表示
(select sname,avg(grade) a_g from student st1,sc sc1      
where st1.sno = sc1.sno 
and ssex = '男') as S
where S.a_g >
--查询所有女生平均成绩
(select avg(grade) from student,sc
where student.sno = sc.sno
and ssex = '女');

14、查询比“computer science”专业所有学生年龄都大的学生姓名。

select sname from student
where sage > 
(select max(sage) from student
where sdept = 'computer science'); 

15、查询考试成绩仅有一科不及格学生姓名

select sname from student,sc
where student.sno = sc.sno
and grade < 60
group by student.sno
having count(*) = 1;


你可能感兴趣的:(DB)