数据库编程基本练习题

1、用一条SQL语句查询出每门课都大于80分的学生姓名 

准备数据的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 name not in (select distinct name from score where score<=80)

2、每个月份的发生额都比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);

答案:

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
);

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 
准备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

 

未完待续~~~~~~~~~

上一篇:springboot 文件上传示例(webuploader插件)


下一篇:vue分片上传