SQL SQL EVERYDAY 20181007

版权声明:请勿转载 https://blog.csdn.net/weixin_43093831/article/details/82958861

数据分析工具目前在Python上大容量吸收以及提高当前的水平,但是,作为辅助手段的SQL以及spss也会顺带练习,SQL会每天都会有练习,spss每周会有练习。希望自己能在数据分析的道路上可以更进一步。

第一个50道SQL题20181007

-- 创建数据库并使用
create database exercise100;
use exercise100;

--  建表
create table students(sno varchar(3) not null,
                      sname varchar(4) not null,
                      ssex varchar(2) not null,
                      sbirthday datetime,
                      class varchar(5));
create table courses(cno varchar(5) not null,
                     cname varchar(10) not null,
                     tno varchar(10) not null);
create table scores(sno varchar(3) not null,
                    cno varchar(5) not null,
                    degree numeric(10,1) not null);
create table teachers(tno varchar(3) not null,
                      tname varchar(3) not null,
                      tsex varchar(2) not null,
                      tbirthday datetime not null,
                      prof varchar(6),
                      depart varchar(10) not null);
-- 插入数据
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾华' ,'男' ,'1977-09-01',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,'1975-10-02',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王丽' ,'女' ,'1976-01-23',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李军' ,'男' ,'1976-02-20',95033);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,'1975-02-10',95031);
INSERT INTO STUDENTS (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陆君' ,'男' ,'1974-06-03',95031);

INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-105' ,'计算机导论',825);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系统' ,804);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('6-166' ,'数据电路' ,856);
INSERT INTO COURSES(CNO,CNAME,TNO)VALUES ('9-888' ,'高等数学' ,100);
select * from COURSES;
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-245',86);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO SCORES(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
select * from SCORES;
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (804,'李诚','男','1958-12-02','副教授','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (856,'张旭','男','1969-03-12','讲师','电子工程系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (825,'王萍','女','1972-05-05','助教','计算机系');
INSERT INTO TEACHERS(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART) VALUES (831,'刘冰','女','1977-08-14','助教','电子工程系');

-- 1、 查询Student表中的所有记录的Sname、Ssex和Class列
select sname,ssex,class from students group by sname,ssex,class;
-- 2、 查询教师所有的单位即不重复的Depart列
select distinct depart from teachers group by depart;
-- 3、 查询Student表的所有记录。
select * from students;
-- 4、 查询Score表中成绩在60到80之间的所有记录。
#方法1
select sno,degree from scores where degree<=80 and degree>=60 group by sno;
#方法2
SELECT * FROM Scores WHERE Degree BETWEEN 60 AND 80;
-- 5、 查询Score表中成绩为85,86或88的记录。
#方法1
select sno,degree from scores where degree=85 or degree=86 or degree=88 group by sno;
#方法2
select sno,degree from scores where degree in (85,86,88);#逻辑操作符 p90 8.3.3

-- 6、 查询Student表中“95031”班或性别为“女”的同学记录。
select sno,sname,class,ssex from students where class=95031 or ssex='女' group by sno;
-- 7、 以Class降序查询Student表的所有记录。
SELECT * FROM Students ORDER BY Class DESC;

-- 8、 以Cno升序、Degree降序查询Score表的所有记录。
select * from scores order by cno,degree desc; 

-- 9、 查询“95031”班的学生人数。
#法1
select count(sno) from students where Class='95031';
#方法2
select count(1) as stunum from Students where Class='95031';#这里的count(1)不知道为何要这么用

-- 10、查询Score表中的最高分的学生学号和课程号。
select * from scores order by degree desc limit 1;
-- 11、查询‘3-105’号课程的平均分。
select avg(degree) from scores where cno='3-105';
#要把cno的赋值内容打双引号

-- 12、查询Score表中至少有5名学生选修的并以3开头的课程的平均分数。
Select Cno,Avg(Degree) 
From Scores 
Where cno like '3%' 
group by cno 
having count(sno)>=5;
#p90 逻辑操作符8.3.4

-- 13、查询最低分大于70,最高分小于90的Sno列。 
select sno
from SCORES
group by sno
having  max(degree)<90 and min(degree)>70;
#especially paying attention to:where, group by, having(SQL)

-- 14、查询所有学生的Sname、Cno和Degree列。
select st.sname,sc.cno,sc.degree
from students st
inner join scores sc on st.sno=sc.sno
order by st.sno;
#especially paying attention to:p156-p168

-- 15、查询所有学生的Sno、Cname和Degree列。
select s.Sno,c.Cname,s.degree
from courses c 
inner join scores s on c.Cno=s.Cno
order by s.Sno;

-- 16、查询所有学生的Sname、Cname和Degree列。
Select s.Sname,c.Cname,sc.Degree
from scores sc 
inner join students s  on s.sno=sc.sno
inner join courses c on sc.cno=c.cno
order by s.Sname;

-- 17、查询“95033”班所选课程的平均分。
select cname,avg(degree)
from scores sc 
inner join students s  on s.sno=sc.sno
inner join courses c on sc.cno=c.cno
where class="95033"
group by cname
having avg(degree);
#答案中无having+聚合函数,是因为SELECT里可以自行进行计算,需要注意alter

-- 18、假设使用如下命令建立了一个grade表:
create table grade(low numeric(3,0),
                   upp numeric(3,0),
                   rank char(1));

                   upp number(3) not null,
                   rank char(1) not null);
insert into grade values(90,100,'A');
insert into grade values(80,89,'B');
insert into grade values(70,79,'C');
insert into grade values(60,69,'D');
insert into grade values(0,59,'E');

select * from grade;
-- 现查询所有同学的Sno、Cno和rank列。
select Sno,Cno,Rank
from scores inner join grade
on scores.degree>=grade.low and scores.degree<= grade.upp
order by sno;
-- -------------- 传说中的,不等值结合-----------------------

-- 19、查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录

select sc.sno,sc.degree
from scores sc inner join courses c
on sc.cno=c.cno;
-- -----------这题想后且尝试操作过后不会---------------------------
select s1.sno,s1.degree
from scores s1 inner join scores s2
on s1.cno=s2.cno and s1.degree>s2.degree
where s1.cno='3-105'and s2.sno='109'
order by s1.sno;
-- --------------还能这样操作,神奇---------------------
-- 20、查询score中选学一门以上课程的同学中分数为非最高分成绩的记录。
select sno,degree
from scores
group by sno
having count(cno)>1 and degree!=max(degree);
                              


---------------未完待续----------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_43093831/article/details/82958861
SQL