学生表sql语句练习
程序员文章站
2022-03-08 17:36:28
...
学生表sql语句练习
总结
干啥啥不行,想要编程第一名!
最近开始学写项目,常常sql出错,这就很烦!哈哈哈
所以多练吧!以下sql有些还能简化,请不要*我!
1.查询student表的所有记录
SELECT * from student
2.查询student表的第2条到4条记录
select * from student LIMIT 1,3
3.从student表查询所有学生的学号(id)、姓名(name)和院系(department)的信息
SELECT id,name,department from student
4.从student表中查询计算机系和英语系的学生的信息
SELECT * from student where department='计算机系' or department='英语系'
5.从student表中查询每个院系有多少人
SELECT department,count(*) from student GROUP BY department
6.从score表中查询每个科目的最高分
SELECT c_name,max(grade) from score GROUP BY c_name
7.查询李四的考试科目(c_name)和考试成绩(grade)
select name,c_name,grade FROM score t1 join student t2 on t1.stu_id=t2.id and t2.name='李四'
select name,c_name,grade FROM score join student on score.stu_id=student.id and student.name='李四'
8.查询所有学生的信息和考试信息
select * from student,score WHERE score.stu_id=student.id
9.计算每个学生的总成绩
select t2.name,sum(t1.grade) from score t1 join student t2 on t1.stu_id=t2.id GROUP BY t2.name
10.计算每个考试科目的平均成绩
select c_name,AVG(grade) from score GROUP BY c_name
11.查询计算机成绩低于95的学生信息
select * from student,score where score.stu_id=student.id and score.grade<95 and score.c_name='计算机'
12.查询同时参加计算机和英语考试的学生的信息
select * from student where id in (
select stu_id from score where c_name ='计算机' and stu_id in(
select stu_id from score where c_name ='英语'));
SELECT c.aid,c.aname from (SELECT a.id aid,a.name aname from student a JOIN score b on a.id=b.stu_id where b.c_name='计算机') c
join (SELECT a.id bid,a.name bname from student a JOIN score b on a.id=b.stu_id where b.c_name='英语') d
on c.aid=d.bid
13.将计算机考试成绩按从高到低进行排序
SELECT * from score where c_name='计算机' ORDER BY grade DESC
14.查询姓张或者姓王的同学的姓名、院系和考试科目及成绩
select name,department,c_name,grade from student,score where score.stu_id=student.id
and student.name like "%张%" or student.name like "%王%"
15.查询都是湖南的学生的姓名、年龄、院系和考试科目及成绩
select name,birth,department,c_name,grade,address from student,score
where score.stu_id=student.id and address like "%湖南%"