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

sql语句练习

时间:2019-01-21 01:13:39      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:student   www   保留   and   lex   des   from   wan   none   

-- 1.查询学习课程"python"比课程 "java" 成绩高的学生的学号;
-- select python.s_id,student.s_name from (
-- select  score.num,score.s_id from course,score where score.c_id = course.c_id and course.c_name = ‘python‘) as python,
-- (select score.num,score.s_id from course,score where score.c_id = course.c_id and course.c_name = ‘java‘) as java,
-- student 
-- WHERE python.s_id=java.s_id and python.num >java.num and python.s_id =student.s_id

-- 查询平均成绩大于65分的同学的姓名和平均成绩(保留两位小数);
-- select student.s_name,ROUND(avg(num),2) from student,score where student.s_id =score.s_id GROUP BY student.s_id HAVING avg(num)> 65
-- 查询所有同学的姓名、选课数、总成绩;
-- select student.s_name,count(score.c_id) ‘课程数‘,sum(score.num) ‘总成绩‘ from student,score WHERE student.s_id =score.s_id GROUP BY score.s_id
-- 查询所有的课程的名称以及对应的任课老师姓名;
-- select c_name,t_name from course,teacher where course.t_id =teacher.t_id
-- 查询没学过“alex”老师课的同学的姓名;
-- select student.s_name from (teacher,course,score,student) where ((course.t_id = teacher.t_id) and (score.c_id =course.c_id) and 
-- (student.s_id=score.s_id)) GROUP BY student.s_id HAVING  ‘alex‘ not in(teacher.t_name)-- 错误
-- select * from score where score.c_id in (select course.c_id from teacher,course where teacher.t_id = course.t_id and t_name = ‘alex‘)

-- select * from student where student.s_id not in(select s_id from score where score.c_id in ((select course.c_id from teacher,course where teacher.t_id = course.t_id and t_name = ‘alex‘)))

-- 查询学过‘python‘并且也学过编号‘java‘课程的同学的姓名
-- select c_id from course where c_name in(‘python‘,‘java‘);
-- select * from student where s_id in(select s_id from score where score.c_id in (select c_id from course where c_name in(‘java‘,‘python‘)))
-- select student.s_name from course,score,student where score.s_id=student.s_id and course.c_id =score.c_id and course.c_name in (‘python‘,‘java‘) GROUP BY score.s_id HAVING COUNT(*)>=2
-- 查询学过“alex”老师所教的全部课程的同学的姓名
-- SELECT student.s_name from student WHERE student.s_id in (SELECT s_id from score where score.c_id in(SELECT c_id from teacher,course WHERE teacher.t_id =course.t_id and teacher.t_name = ‘alex‘) GROUP BY score.s_id HAVING count(*)>=2)
-- 查询挂科超过两门(包括两门)的学生姓名;
-- SELECT s_name from student where student.s_id in(SELECT s_id from score where num <70 GROUP BY score.s_id HAVING count(*)>=2);
-- SELECT s_name from student,score where student.s_id =score.s_id and num<70 GROUP BY score.s_id HAVING count(*)>=2;
-- 查询有课程成绩小于60分的同学的姓名;
-- SELECT s_name FROM student,score where score.num <60 and score.s_id =student.s_id GROUP BY student.s_id;
-- SELECT DISTINCT s_name FROM student,score where score.num <60 and score.s_id =student.s_id GROUP BY student.s_id
-- 查询选修了全部课程的学生姓名;
-- SELECT student.s_name from score,student WHERE score.s_id=student.s_id GROUP BY score.s_id HAVING count(*)=(SELECT count(*) from course)-1
-- 查询至少有一门课程与“貂蝉”同学所学课程相同的同学姓名;

-- SELECT DISTINCT s_name from student,score where student.s_id= score.s_id and c_id in (SELECT score.c_id from score,student WHERE score.s_id =(SELECT s_id from student where s_name = ‘貂蝉‘) and student.s_id= score.s_id) and student.s_name!=‘貂蝉‘
-- 
-- 查询学过‘貂蝉‘同学全部课程 的其他同学姓名;
-- SELECT s_name from student,score where student.s_id= score.s_id and c_id in (SELECT score.c_id from score,student WHERE score.s_id =(SELECT s_id from student where s_name = ‘貂蝉‘) and student.s_id= score.s_id) and student.s_name!=‘貂蝉‘ GROUP BY score.s_id HAVING count(*)=(SELECT count(*) from score,student WHERE score.s_id =(SELECT s_id from student where s_name = ‘貂蝉‘) and student.s_id= score.s_id);
-- 查询和‘貂蝉‘同学学习的课程完全相同的,其他同学姓名
-- SELECT score.c_id from score,student WHERE score.s_id =(SELECT s_id from student where s_name = ‘貂蝉‘) and student.s_id= score.s_id
-- SELECT * from score,student WHERE score.s_id=student.s_id and score.s_id in (SELECT s_id FROM score GROUP BY score.s_id HAVING count(*)=(SELECT count(*) FROM score,student WHERE score.s_id = student.s_id  and student.s_name=‘貂蝉‘ GROUP BY score.s_id )) and score.c_id in (1,2) GROUP BY score.s_id HAVING count(*)=2 and student.s_name!=‘貂蝉‘
-- 按平均成绩倒序显示所有学生的“python”、“java”、“linux”三门的课程成绩,按如下形式显示: 学生ID,python,java,linux,课程数,平均分
-- SELECT num as ‘python‘  from  score,course WHERE course.c_id =score.c_id and score.s_id=1 and course.c_name = ‘python‘;
-- SELECT num from  score,course WHERE course.c_id =score.c_id and score.s_id=1 and course.c_name = ‘java‘;
-- SELECT num from  score,course WHERE course.c_id =score.c_id and score.s_id=1 and course.c_name = ‘linux‘;
-- SELECT 
-- s.s_id as ‘学生ID‘,
-- count(s.c_id) as ‘课程数‘,
-- avg(s.num) as ‘平均分‘,
-- (SELECT num  from  score,course WHERE course.c_id =score.c_id and score.s_id=s.s_id and course.c_name = ‘python‘) as ‘python‘,
-- (SELECT num  from  score,course WHERE course.c_id =score.c_id and score.s_id=s.s_id and course.c_name = ‘linux‘) as ‘linux‘,
-- (SELECT num  from  score,course WHERE course.c_id =score.c_id and score.s_id=s.s_id and course.c_name = ‘java‘) as ‘java‘
-- FROM score s GROUP BY s.s_id
-- 统计各科各分数段人数.显示格式:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60] 
-- SELECT course.c_id,course.c_name,
-- sum(CASE WHEN num BETWEEN 85 and 100 THEN 1 ELSE 0 end )as ‘[85-100]‘,
-- sum(CASE WHEN num BETWEEN 70 and 85 THEN 1 ELSE 0 end )as ‘[70-85]‘,
-- sum(CASE WHEN num BETWEEN 60 and 70 THEN 1 ELSE 0 end )as ‘[60-70]‘,
-- sum(CASE WHEN num < 60 THEN 1 ELSE 0 end )as ‘[<60]‘
-- from score,course where score.c_id =course.c_id GROUP BY course.c_id
-- 查询每门课程被选修的次数
-- SELECT course.c_name,count(*) as ‘选修次数‘ from score,course WHERE score.c_id=course.c_id GROUP BY score.c_id
-- 查询出只选修了一门课程的学生的学号和姓名
-- select s_name,student.s_id FROM score,student WHERE score.s_id=student.s_id GROUP BY score.s_id HAVING count(*)=1
-- 查询学生表中男生、女生各有多少人
-- SELECT s_sex,count(*) FROM student GROUP BY s_sex;
-- 查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
-- SELECT c_id,avg(num) from score GROUP BY c_id ORDER BY avg(num) asc,c_id DESC
-- 查询课程名称为“python”,且分数低于60的学生姓名和分数
-- SELECT student.s_name,score.num from score,course,student WHERE score.c_id=course.c_id and student.s_id =score.s_id and course.c_name=‘python‘ and score.num <70
 

  

sql语句练习

标签:student   www   保留   and   lex   des   from   wan   none   

原文地址:https://www.cnblogs.com/Lucifer77/p/10296834.html

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