sql 练习(五)

作者: 叨叨侠爱叨叨 | 来源:发表于2017-11-13 11:51 被阅读66次

环境是mysql

练习数据见SQL:练习的前期准备
sql 练习(一)
sql 练习(二)
sql 练习(三)
sql 练习(四)
*41、查询“1”课程比“2”课程成绩高的所有学生的学号;

SELECT a.sno
  FROM (SELECT * FROM score WHERE cno='1') a
  JOIN (SELECT * FROM score WHERE cno='2') b
    ON a.sno=b.sno
 WHERE a.degree>b.degree

  SELECT a.Sno
    FROM (SELECT sno,degree FROM score WHERE Cno='1') a
        ,(SELECT sno,degree FROM score WHERE Cno='2') b 
   WHERE a.sno=b.sno 
     AND a.degree>b.degree

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

SELECT sno,AVG(degree)
  FROM score
 GROUP BY sno 
HAVING AVG(degree)>60

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

SELECT a.sname,a.sno,b.coursenum,b.totaldegree
  FROM student a
  LEFT JOIN (SELECT COUNT(*)AS coursenum,SUM(degree) AS totaldegree,sno
               FROM score
              GROUP BY sno) b
    ON a.sno=b.sno
         
SELECT a.sno,a.Sname,COUNT(b.cno),SUM(degree) 
  FROM Student a
  LEFT JOIN score b ON a.sno=b.sno
 GROUP BY a.sno,Sname 

44、查询姓“李”的老师的个数;

SELECT COUNT(*),tname
  FROM teacher 
 WHERE tname LIKE "李%" 

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

SELECT sno,sname
  FROM student
 WHERE sno NOT IN (
            SELECT sno 
              FROM score 
             WHERE cno IN( SELECT cno 
                             FROM course  
                            WHERE cno IN (SELECT tno
                            FROM teacher
                           WHERE tname='叶平')))

46、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

SELECT a.sno,a.sname
  FROM student a
  JOIN (SELECT score.sno
         FROM score
         JOIN (SELECT sno FROM score WHERE cno=2)c
       ON score.sno=c.sno
        WHERE score.cno=1 )b
    ON a.sno=b.sno
    
SELECT a.sno,a.Sname 
  FROM Student a ,score b 
 WHERE a.sno=b.sno AND b.cno='1'
   AND EXISTS( SELECT sno FROM score c WHERE c.sno=b.sno AND c.Cno='2')

47、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

-- my way
SELECT sno,sname
  FROM student
 WHERE sno IN(SELECT sno 
                FROM score 
               WHERE cno IN(SELECT cno 
                  FROM course 
                 WHERE tno IN (SELECT tno
                         FROM teacher
                        WHERE tname='叶平')))
SELECT sno,Sname 
  FROM Student 
  WHERE sno IN (SELECT sno
                  FROM score ,Course ,Teacher 
                 WHERE score.cno=Course.cno 
                   AND Teacher.tno=Course.tno 
                   AND Teacher.Tname='叶平' 
                 GROUP BY sno 
                HAVING COUNT(score.cno)=(SELECT COUNT(cno) 
                                           FROM Course,Teacher 
                                          WHERE Teacher.tno=Course.tno
                                            AND Tname='叶平')); 

48、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

SELECT sno,sname
 FROM student 
WHERE sno IN (SELECT a.sno
           FROM (SELECT degree,sno FROM score WHERE cno='2')a
           JOIN (SELECT degree,sno FROM score WHERE cno='1')b
             ON a.sno=b.sno
          WHERE a.degree<b.degree)

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

SELECT sno,Sname 
 FROM Student 
WHERE sno NOT IN (SELECT b.sno
                FROM score b 
               WHERE b.degree>60); 

50、查询[没有]学全所有课的同学的学号、姓名;

-- 方法一

SELECT a.sno,a.sname
  FROM student a
  JOIN (SELECT sno 
      FROM score
     GROUP BY sno 
    HAVING COUNT(*)<(SELECT COUNT(*) FROM course))b
    ON a.sno=b.sno
    
-- 方法二

SELECT a.sno,a.Sname 
  FROM Student a,score b 
 WHERE a.sno=b.sno
 GROUP BY  a.sno,a.Sname 
HAVING COUNT(b.sno) <(SELECT COUNT(cno) FROM Course)

相关文章

  • sql 练习(五)

    环境是mysql 练习数据见SQL:练习的前期准备 sql 练习(一)sql 练习(二)sql 练习(三)sql ...

  • sql 练习(四)

    环境是mysql 练习数据见SQL:练习的前期准备 sql 练习(一)sql 练习(二)sql 练习(三)31、查...

  • sql 练习(三)

    环境是mysql 练习数据见SQL:练习的前期准备 sql 练习(一)sql 练习(二)21、查询成绩高于学号为“...

  • MySQL Operation

    sql语句练习sql练习2 MYSQL导入数据出现The MySQL server is running with...

  • MySQL 练习题

    01第一天20180703 【SQL练习】经典SQL练习题 02第二天20180704 sql语句练习50题(My...

  • SQL练习

    SQL练习-4张表 针对下面的4张表格进行SQL语句的练习。 image SQL练习-题目 查询001课程比002...

  • 总结:SQL练习【SQL经典练习题】

    刚刷完SQL练习【SQL经典练习题】,本篇文章将对我不牢固的知识做简单汇总。没对比就没标准,当练习超经典SQL练习...

  • MySQL语句练习

    -- ------------------------------------------------SQL练习语...

  • 导入数据时出现的3个问题:【SQL练习】经典SQL练习题

    数据来源:【SQL练习】sql经典练习前言:有人戏称做完这50道练习题你的SQL就过关了!对于如此之高的评价,我也...

  • 2018-08-05--08-11

    08-05配置1、sql语句练习。根据月乔的文档&sql优化,根据文档练习2、hive语句1)hive,sql连接...

网友评论

  • 蛋总Maximus:44、查询姓“李”的老师的个数
    作者您的解答似乎有误,跑不出来,我认为正确的解法应该是:
    select '李', count(*) as name_count from student where sname like '李%';

    因为我们相当于对于符合条件的李老师们做了一个group操作,不应该再要求返回tname
    叨叨侠爱叨叨:已改,当时加上tname是可以做验证。dear 你的表好像写错了,写SQL要很小心哦~
    叨叨侠爱叨叨:@蛋总Maximus 好哒,谢谢提出意见,我晚点看看

本文标题:sql 练习(五)

本文链接:https://www.haomeiwen.com/subject/ssilmxtx.html