一、

1.新建表 test

id varchar2(20)
name varchar2(20)
addr varchar2(50)
score number

create table test
(id varchar2(20),
name varchar2(20),
addr varchar2(50),
score number
)

2.给id增加主键约束
alter table test add constraint pk_id primary key(id)
3.查询出分数大于60分的人员,并按分数降序进行排列

select * from test where score>60 order by score desc
4.新增数据 001 zhangsan shenzhen 80
insert into test(id,name,addr,score) values ('001','zhansgan','shenzhen','80')

6. 将zhangsan的分数修改成60
update test set score=60 where name='zhangsan'

7.删除zhangsan的信息

delete from test where name='zhangsan'

二、

Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表


问题:

1、查询平均成绩大于60分的同学的学号和平均成绩; avg

select s#,avg(score) from sc where (select avg(score) from sc)>60 group by s# ;
select s#,avg(score) from sc where group by s# having avg(score)>60;

注:where条件中不能包含聚组函数,having 子句的作用是筛选满足条件的组,即在分组之后过滤数据,条件中经常包含聚组函数。

GROUP BY,顾名思义:根据...分组,在SQL中常根据指定字段分组(指定字段内容相同是为一组),然后针对组进行相关操作

 


2、查询所有同学的学号、姓名、选课数、总成绩;

select Student.s#,sname,count(C#),sum(score) from Student inner join sc on Student.s#=sc.s# group by Student.s#,sname

 

4、查询没学过“叶平”老师课的同学的学号、姓名;

select S#,Sname from Student where s# in (select S# from sc where c# not in (select c# from Course where T#=(select T# from teacher where Tname='叶平' ))

5、查询所有课程成绩小于60分的同学的学号、姓名;

select S#,Sname from Student where s# not in (select S# from sc where score>60)

 

 三、项目实战

———————————————————————————————————————
向student表插入记录的INSERT语句如下:

INSERT INTO student VALUES( 901,'张老大', '男',1985,'计算机系', '北京市海淀区');

INSERT INTO student VALUES( 902,'张老二', '男',1986,'中文系', '北京市昌平区');

INSERT INTO student VALUES( 903,'张三', '女',1990,'中文系', '湖南省永州市');

INSERT INTO student VALUES( 904,'李四', '男',1990,'英语系', '辽宁省阜新市');

INSERT INTO student VALUES( 905,'王五', '女',1991,'英语系', '福建省厦门市');

INSERT INTO student VALUES( 906,'王六', '男',1988,'计算机系', '湖南省衡阳市');

———————————————————————————————————————

向score表插入记录的INSERT语句如下:

INSERT INTO score VALUES(NULL,901, '计算机',98);

INSERT INTO score VALUES(NULL,901, '英语', 80);

INSERT INTO score VALUES(NULL,902, '计算机',65);

INSERT INTO score VALUES(NULL,902, '中文',88);

INSERT INTO score VALUES(NULL,903, '中文',95);

INSERT INTO score VALUES(NULL,904, '计算机',70);

INSERT INTO score VALUES(NULL,904, '英语',92);

INSERT INTO score VALUES(NULL,905, '英语',94);

INSERT INTO score VALUES(NULL,906, '计算机',90);

INSERT INTO score VALUES(NULL,906, '英语',85);

———————————————————————————————————————

 

6.从student表中查询年龄18~22岁的学生信息

SELECT id,name,sex,2010-birth AS age,department,address FROM student WHERE 2010-birth>=18 AND 2010-birth<=22;     (其实没有age,直接计算今年年份-出生birth) 

SELECT id,name,sex,2013-birth AS age,department,address FROM student WHERE 2013-birth BETWEEN  18 AND 22;

 

7.从student表中查询每个院系有多少人

SELECT department, COUNT(id) FROM student GROUP BY department;

执行效率上:

列名为主键,count(列名)会比count(1)快

列名不为主键,count(1)会比count(列名)快

如果表多个列并且没有主键,则 count(1) 的执行效率优于 count(*)

如果有主键,则 select count(主键)的执行效率是最优的

如果表只有一个字段,则 select count(*)最优。

 

8.从score表中查询每个科目的最高分

SELECT c_name,MAX(grade) FROM score GROUP BY c_name;

 

9.查询李四的考试科目(c_name)和考试成绩(grade)

SELECT c_name, grade FROM score WHERE stu_id=(SELECT id FROM student WHERE name= '李四' );

 

10.用连接的方式查询所有学生的信息和考试信息

SELECT student.id,name,sex,birth,department,address,c_name,grade FROM student,score WHERE student.id=score.stu_id;

 

11.计算每个学生的总成绩

SELECT student.id,name,SUM(grade) FROM student,score WHERE student.id=score.stu_id GROUP BY id; (两个表都有id,要注意冲突)

 

12.计算每个考试科目的平均成绩

SELECT c_name,AVG(grade) FROM score GROUP BY c_name;

 

13.查询计算机成绩低于95的学生信息

SELECT * FROM student WHERE id IN (SELECT stu_id FROM score WHERE c_name="计算机" and grade<95);

 

14.查询同时参加计算机和英语考试的学生的信息

SELECT * FROM student WHERE id =ANY ( SELECT stu_id FROM score WHERE stu_id IN ( SELECT stu_id FROM score WHERE c_name= '计算机') AND c_name= '英语' );(=ANY 与子查询IN相同

SELECT a.* FROM student a ,score b ,score c WHERE a.id=b.stu_id  AND b.c_name='计算机' AND a.id=c.stu_id AND c.c_name='英语';

 


15.将计算机考试成绩按从高到低进行排序

SELECT stu_id, grade FROM score WHERE c_name= '计算机' ORDER BY grade DESC;

 


16.从student表和score表中查询出学生的学号,然后合并查询结果

SELECT id FROM student UNION SELECT stu_id FROM score;

 


17.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩

 SELECT student.id, name,sex,birth,department, address, c_name,grade FROM student, score WHERE address LIKE '湖南%' AND student.id=score.stu_id;

 

 

18.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩

SELECT student.id, name,sex,birth,department, address, c_name,grade FROM student, score WHERE (name LIKE '张%' OR name LIKE '王%') AND student.id=score.stu_id ;

 

19.查询student表中学生的学号、姓名、年龄、院系和籍贯并且按照年龄从小到大的顺序排列。
select student.id,name,2017-birth,department,address from student ORDER BY 2017-birth;

 

 

20.统计超过2人考试的科目

select c_name ,count(*) from score group by c_name having count(*)>2;

 

 

21.查找平均分大于80分的学号和平均成绩,按降序排序

select stu_id,avg(grade) from score group by stu_id having avg(grade)  > 80 order by avg(grade) desc;

select stu_id,avg(grade) from score where (select avg(grade) from sroce) group by stu_id order by avg(grade) desc ;

 

22.查找所有同学的学号、姓名、总成绩;

select student.id,name,sum(grade) from student inner join score on student.id = score.stu_id group by student.id;

INNER JOIN 关键字在表中存在至少一个匹配时返回行。如果 "student" 中的行在 "score" 中没有匹配,就不会列出这些行。

同理还有LEFT JOIN, RIGHT JOIN

 

23.查询学过计算机课程的同学的姓名和学号

select id,name from student where id in (select stu_id from score where c_name = "计算机");

 

24.查询所有课程成绩小于90分的同学的学号、姓名;

select id ,name from student where id not  in (select stu_id from score where grade >90);

 

 

 

posted on 2020-01-07 11:58  ing学习ing  阅读(794)  评论(0编辑  收藏  举报