码迷,mamicode.com
首页 > 数据库 > 详细

常见的sql语句练习

时间:2020-01-07 13:20:09      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:str   ash   问题:   sse   合并   修改   lte   厦门   play   

 

一、

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;
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,‘计算机系‘, ‘湖南省衡阳市‘);

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

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;

 

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

常见的sql语句练习

标签:str   ash   问题:   sse   合并   修改   lte   厦门   play   

原文地址:https://www.cnblogs.com/hjy123/p/12160744.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!