一、基础
1、说明:创建数据库CREATE DATABASE database-name
2、说明:删除数据库drop database dbname
3、说明:创建新表createtabletabname(col1 type1[not null][primary key],col2 type2[not null],..)
根据已有的表创建新表:
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2…from tab_old definition only
4、说明:删除新表drop table tabname
5说明:增加一个列Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
6、说明:添加主键:Alter table tabname add primarykey(col)
说明:删除主键: Alter table tabname drop primarykey(col)
注:索引是不可更改的,想更改必须删除重新建。
7、说明:几个简单的基本的sql语句
选择:select * from able1 where 范围
插入:insert into table1(field1,field2) values (value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like’% value 1%’
排序:select * from table1 order by field1,field2[desc]
总数:select count as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
8、说明:几个高级查询运算词
A: UNION 运算符UNION运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当ALL随UNION一起使用时(即UNIONALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。
B: EXCEPT 运算符EXCEPT运算符通过包括所有在 TABLE1 中但不在 TABLE2 中的行并消除所有重复行而派生出一个结果表。当ALL随EXCEPT一起使用时 (EXCEPTALL),不消除重复行。
C: INTERSECT 运算符INTERSECT运算符通过只包括 TABLE1 和 TABLE2 中都有的行并消除所有重复行而派生出一个结果表。当ALL随INTERSECT一起使用时 (INTERSECTALL),不消除重复行。
注:使用运算词的几个查询结果行必须是一致的。
9、说明:使用外连接
A、left(outer)join:
左外连接(左连接):结果集几包括连接表的匹配行,也包括左连接表的所有行。
SQL: selecta.a, a.b, a.c, b.c, b.d, b.f from a LEFT OUT JOIN b ON a.a= b.c
B:right(outer)join:
右外连接(右连接):结果集既包括连接表的匹配连接行,也包括右连接表的所有行。
C:full/cross(outer)join:
全外连接:不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录。
10、分组:Group by:
一张表,一旦分组完成后,查询后只能得到组相关的信息。
组相关的信息:(统计信息) count,sum,max,min,avg 分组的标准)
一般来说 我只注重查询的写法,有不错的练习题欢迎共同探讨
练习题目
学生表 Student
create table Student(SId varchar(10),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('09','张三','2017-12-20','女');
insert into Student values('10','李四','2017-12-25','女');
insert into Student values('11','李四','2017-12-30','女');
insert into Student values('12','赵六','2017-01-01','女');
insert into Student values('13','孙七','2018-01-01','女');
科目表 Course
create table Course(CId varchar(10),Cnamen varchar(10),TId varchar(10));
insert into Course values('01','语文','02');
insert into Course values('02','数学','01');
insert into Course values('03','英语','03');
教师表 Teacher
create table Teacher(TId varchar(10),Tname varchar(10));
insert into Teacher values('01' , '张三');
insert into Teacher values('02' , '李四');
insert into Teacher values('03' , '王五');
成绩表 SC
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);
1. 查询" 01 "课程比" 02 "课程成绩高的学生的信息及课程分数
SELECT s.sid,sname,sage,ssex,cid,score from student s,sc where sc.SId=s.sid and s.SId in (select s.sid from sc s,sc c where s.sid=c.sid and s.CId=01 and c.CId=02 AND s.score>c.score);
1.1 查询同时存在" 01 "课程和" 02 "课程的情况
SELECT sid from sc WHERE cid=02 and sid in(select sid from sc where cid=01);
1.2 查询存在" 01 "课程但可能不存在" 02 "课程的情况(不存在时显示为 null )
select * from (select sid,score from sc where sc.CId=01) as t1 LEFT JOIN (select sid,score from sc where sc.CId=02) as t2 on t1.sid=t2.sid;
1.3 查询不存在" 01 "课程但存在" 02 "课程的情况
SELECT * from sc where cid=02 and sid not in (select sid from sc where cid=01);
2. 查询平均成绩大于等于 60 分的同学的学生编号和学生姓名和平均成绩
select s.sid,sname,AVG(score)from sc,student s where s.SId=sc.SId group by s.sid HAVING AVG(score)>60;
或者
select student.*,t1.avgscore from student inner JOIN(select sc.SId ,AVG(sc.score)as avgscore from sc GROUP BY sc.SId HAVING AVG(sc.score)>=60)as t1 on student.SId=t1.SId ;
3. 查询在 SC 表存在成绩的学生信息
select * from student where sid in (select sid from sc );
或者
select DISTINCT student.* from student ,sc where student.SId=sc.SId;
4. 查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩(没成绩的显示为 null )
select * from (select sid,COUNT(sid),SUM(score) from sc group by sc.sid) as t1 LEFT JOIN (select * from student) as t2 on t1.sid=t2.sid ;
或者
SELECT s.*,t1.* from student s,(select sid,COUNT(sid),SUM(score) from sc group by sid) as t1 where t1.sid=s.SId ;
4.1 查有成绩的学生信息
select * from student where sid in (select sid from sc);
select * from student where EXISTS(select * from sc where student.SId=sc.SId);
5. 查询「李」姓老师的数量
select count(tid) from teacher where tname like "李%";
6. 查询学过「张三」老师授课的同学的信息
select * from student where sid in (select DISTINCT(sid) from sc where cid in (select cid from course where tid in (select tid from teacher t where t.Tname="张三")));
select s.* from student s,course c,teacher t,sc where sc.sid=s.sid and t.tid=c.tid and sc.cid=c.cid and tname="张三";
7. 查询没有学全所有课程的同学的信息
select * from student where sid not in (select SId from sc group by SId
having count(sid)=(select count(CId) from course));
注:用having count(sid)<(select count(CId) from course)是不可取的,因为要考虑sc表中没有数据的情况
8. 查询至少有一门课与学号为" 01 "的同学所学相同的同学的信息
select DISTINCT s.* from sc,student s where sc.sid=s.sid and sc.sid<>01 and sc.cid in (select cid from sc where sid=01);
9. 查询和" 01 "号的同学学习的课程 完全相同的其他同学的信息
这题看个人对表结构及内容的理解
select * from student where sid in (select sid from sc where sid<>01 group by sid HAVING count(*)=select COUNT(*) from course);
10. 查询没学过"张三"老师讲授的任一门课程的学生姓名
select * from student s where s.SId not in ( select sid from sc where sc.CId in (select cid from course c, teacher t where t.tname="张三" and c.TId=t.TId));
11. 查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select * from student s RIGHT JOIN (select sid,avg(score) from sc where score<60 group by sid having count(sid)>=2) as b on s.SId=b.sid;;
select student.SId,student.Sname,avg(sc.score) from student ,sc where student.SId=sc.SId and sc.score<60
GROUP BY sc.SId HAVING count(*)>=2;
12. 检索" 01 "课程分数小于 60,按分数降序排列的学生信息
select s.*,sc.score from sc,student s where cid=01 and s.sid=sc.sid and score<60 order by sc.score desc;
13. 按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩
select a.sid,a.cid,a.score,avg(b.score)from sc a,sc b where a.sid=b.sid group by a.sid,a.cid order by avg(b.score) desc;
select sc.SId,sc.CId,sc.score,t1.avgscore from sc left join (select sc.SId,avg(sc.score) as avgscore from sc GROUP BY sc.SId) as t1 on sc.SId =t1.SId ORDER BY t1.avgscore DESC;
14. 查询各科成绩最高分、最低分和平均分:
select cid,max(score),min(score),avg(score) from sc group by cid;
以如下形式显示:课程 ID,课程 name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率
及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90
要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列
select sc.cid 课程ID,c.Cname 课程名称,max(score) 最高分,min(score) 最低分,avg(score) 平均分,count(sc.sid) 选修人数,sum(case when sc.score>=60 THEN 1 else 0 end )/count(sc.sid) 及格率,sum(case when sc.score>=70 and sc.score<80 THEN 1 else 0 end )/count(sc.sid) 中等率,sum(case when sc.score>=80 and sc.score<90 THEN 1 else 0 end )/count(sc.sid) 优良率,sum(case when sc.score>=90 THEN 1 else 0 end )/count(sc.sid) 优秀率 from sc,course c where c.CId=sc.CId group by sc.cid order by 选修人数 desc,sc.cid;
15. 按各科成绩进行排序,并显示排名, Score 重复时保留名次空缺
select sc.SId,sc.CId ,case when @pre_parent_code=sc.CId then @curRank:=@curRank+1 when @pre_parent_code:=sc.CId then @curRank:=1 end as rank,sc.score
from (select @curRank:=0,@pre_parent_code:='') as t ,sc
ORDER by sc.CId,sc.score desc
15.1 按各科成绩进行排序,并显示排名, Score 重复时合并名次
select sid,cid,score,DENSE_RANK()over(PARTITION by cid order by score desc) from sc;
17. 统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[60-0] 及所占百分比
select course.CId,course.Cname,t1.* from course LEFT JOIN (select sc.CId,CONCAT(sum(case when sc.score>=85 and sc.score<=100 then 1 else 0 end )/count(*)*100,'%') as '[85-100]',CONCAT(sum(case when sc.score>=70 and sc.score<85 then 1 else 0 end )/count(*)*100,'%') as '[70-85)',CONCAT(sum(case when sc.score>=60 and sc.score<70 then 1 else 0 end )/count(*)*100,'%') as '[60-70)',CONCAT(sum(case when sc.score>=0 and sc.score<60 then 1 else 0 end )/count(*)*100,'%') as '[0-60)'from scGROUP BY sc.CId) as t1 on course.CId=t1.CId;
18. 查询各科成绩前三名的记录
SELECT * from sc where (select COUNT(*) from sc a where sc.cid=a.cid and sc.score<a.score)<3 ORDER BY CId asc,sid ASC ,sc.score desc
19. 查询每门课程被选修的学生数
select cid,count(*) from sc GROUP BY cid;
20. 查询出只选修两门课程的学生学号和姓名
select s.Sname,s.SId from sc,student s where sc.sid=s.sid group by s.sid HAVING(count(sc.SId))=2;
21. 查询男生、女生人数
select ssex,count(*) from student group by ssex;
22. 查询名字中含有「风」字的学生信息
select count(*) from student s where s.Sname like "%风%";
23. 查询同名同性学生名单,并统计同名人数
SELECT * FROM student LEFT JOIN ( SELECT sname, ssex, count(*)人数 FROM student GROUP BY Sname, Ssex ) AS t1 ON student.Sname = t1.Sname
AND student.Ssex = t1.Ssex WHERE t1.人数 > 1;
24. 查询 1990 年出生的学生名单
select sname from student where YEAR(sage)=1990;
25. 查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列
select cid,avg(score) from sc GROUP BY cid order by avg(score) desc,cid
26. 查询平均成绩大于等于 85 的所有学生的学号、姓名和平均成绩
1、select s.Sname,t1.* from student s,(select sid,AVG(score) from sc GROUP BY sid HAVING AVG(score)>85) t1 WHERE t1.sid=s.SId;
2、select student.SId,student.Sname,t1.avgscore
from student INNER JOIN (select sc.SId ,AVG(sc.score) as avgscore from sc GROUP BY sc.SId HAVING AVG(sc.score)>85) as t1 on
student.SId=t1.SId
27. 查询课程名称为「数学」,且分数低于 60 的学生姓名和分数
select s.sname,sc.score from student s,sc,course c where s.sid=sc.SId and sc.cid=c.cid and c.Cname="数学" and sc.score<60;
28. 查询所有学生的课程及分数情况(存在学生没成绩,没选课的情况)
select * from student s LEFT JOIN sc t1 on t1.sid=s.SId
29. 查询任何一门课程成绩在 70 分以上的姓名、课程名称和分数
select s.sname,c.Cname,sc.CId,sc.score from student s,sc,course c where sc.score>70 and s.sid=sc.SId and sc.CId=c.CId
30. 查询不及格的课程
select s.sname,c.Cname,sc.CId,sc.score from student s,sc,course c where sc.score<60 and s.sid=sc.SId and sc.CId=c.CId
31. 查询课程编号为 01 且课程成绩在 80 分以上的学生的学号和姓名
select s.Sname,sc.sid from student s,sc where sc.SId=s.SId and sc.CId=01 and sc.score>=80
32. 求每门课程的学生人数
select cid,count(*) from sc GROUP BY cid
33. 成绩不重复,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select s.*,sc.cid,sc.score from student s,teacher t,course c,sc where s.SId=sc.SId and sc.CId=c.CId and c.TId=t.TId and t.Tname='张三' ORDER BY sc.score desc LIMIT 1
34. 成绩有重复的情况下,查询选修「张三」老师所授课程的学生中,成绩最高的学生信息及其成绩
select student.*,t1.score
from student INNER JOIN (select sc.SId,sc.score, case when @fontage=sc.score then @rank when @fontage:=sc.score then @rank:=@rank+1 end as rank
from course ,teacher ,sc,(select @fontage:=null,@rank:=0) as t
where course.CId=sc.CId
and course.TId=teacher.TId
and teacher.Tname='张三'
ORDER BY sc.score DESC) as t1 on student.SId=t1.SId
where t1.rank=1
35. 查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩
select DISTINCT sc.* from sc,sc d where sc.SId=d.SId and d.CId!=sc.CId and sc.score =d.score
36. 查询每门功成绩最好的前两名
SELECT * FROM sc AS t1 WHERE ( SELECT count(*) FROM sc AS t2 WHERE t1.CId = t2.cid AND t2.score>t1.score )< 2
order BY t1.CId;
37. 统计每门课程的学生选修人数(超过 5 人的课程才统计)。
select cid,count(*) from sc GROUP BY cid having count(*)>5
38. 检索至少选修两门课程的学生学号
select sid from sc GROUP BY sid having count(*)>=2
39. 查询选修了全部课程的学生信息
select s.* from student s where s.sid in (select sid from sc GROUP BY sid having count(*)=(select count(*) from course))
40. 查询各学生的年龄,只按年份来算
select sname,TIMESTAMPDIFF(year,s.Sage,CURDATE()) from student s
区别RANK,DENSE_RANK和ROW_NUMBER
RANK并列跳跃排名,并列即相同的值,相同的值保留重复名次,遇到下一个不同值时,跳跃到总共的排名。
DENSE_RANK并列连续排序,并列即相同的值,相同的值保留重复名次,遇到下一个不同值时,依然按照连续数字排名。
ROW_NUMBER连续排名,即使相同的值,依旧按照连续数字进行排名。
————————————————
版权声明:本文为CSDN博主「哲这这」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u011726005/article/details/94592866