数据库实验七——嵌套查询

一、查询"cs"系的学生的成绩信息,包括学号,课程号,成绩。

SELECT sno,cno,grade
FROM sclj
WHERE sno IN
   (SELECT sno
    FROM studentlj
    WHERE sdept='cs')

 二、查询李1所选修的课程的课程名。

SELECT cname
FROM courselj
WHERE Cno IN
   (SELECT Cno
    FROM sclj
    WHERE Sno IN
     (SELECT Sno
      FROM studentlj
      WHERE sname='李1'))

三、查询既选了1号课程又选了2号课程的学生学号。

SELECT sno
FROM sclj
WHERE Cno='1' AND sno IN
   (SELECT sno 
   FROM sclj
   WHERE Cno='2')

 

四、查询没有选修课程的学生的名单。 

SELECT sname
FROM studentlj
WHERE Sno NOT IN
   (SELECT Sno
    FROM sclj)

五、查询选修“ch”课程的全部学生的总成绩。

SELECT SUM(grade) AS zong
FROM sclj
WHERE cno IN
  (
  SELECT cno
  FROM courselj
  WHERE cname='ch' )

六、统计学生选修表,显示学号为“2021001976”的学生在其各科成绩中,最高分成绩所对应的课程号和成绩。

SELECT cno,Grade
FROM sclj
WHERE Sno='2021001976'
AND Grade=
  (
   SELECT MAX (Grade)
   FROM sclj
   WHERE Sno='2021001976')

 

 七、查询数学系选修“语文"课程的所有学生的成绩之和。

SELECT SUM(Grade) as 语文总成绩
FROM sclj
WHERE Cno IN
(SELECT Cno
 FROM courselj
 WHERE Cname='语文')
 AND Sno IN
 (
     SELECT Sno
     FROM studentlj
     WHERE Sdept='ma'
     )

 

 

八、查询数学系‘赵980”同学“sc”的成绩

SELECT grade
FROM sclj
WHERE cno IN
 (SELECT cno
  FROM courselj
  WHERE cname='sc')
AND sno IN
 (SELECT sno
  FROM studentlj
  WHERE sdept='ma' 
  AND sname='赵980')

九、查询学号为“2021001977”的学生没选的课程的课程号和课程名。

SELECT DISTINCT cno,cname 
FROM courselj 
WHERE cno NOT IN 
  (SELECT cno 
   FROM sclj 
   WHERE Sno='2021001977')

十、检索至少选修了两门课程的学生的信息,包括学号,姓名,所在系。 

SELECT sno,sname,sdept 
FROM studentlj 
WHERE sno IN 
  (SELECT sno 
   FROM sclj 
   GROUP BY sno 
   HAVING COUNT(Cno)>=2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值