SQL SQL EVERYDAY 20181008

版权声明:请勿转载 https://blog.csdn.net/weixin_43093831/article/details/82965167

数据分析工具目前在Python上大容量吸收以及提高当前的水平,但是,作为辅助手段的SQL以及spss也会顺带练习,SQL会每天都会有练习,spss每周会有练习。希望自己能在数据分析的道路上可以更进一步。

第一个50道SQL题20181008

-- 21、查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。
use exercise100;
select s1.cno,s1.degree,s1.sno
from scores s1 inner join scores s2 
on s1.sno=s2.sno and s1.degree>s2.degree
where s1.cno='3-105' and s2.sno='109'
order by s1.sno;
***-- 该题类似第19题,如上为我的答案,反馈答案错误,
***-- 与如下答案差别为内连接条件不同******
SELECT s1.Sno,s1.Degree
FROM Scores AS s1 INNER JOIN Scores AS s2
ON(s1.Cno=s2.Cno AND s1.Degree>s2.Degree)
WHERE s1.Cno='3-105' AND s2.Sno='109'
ORDER BY s1.Sno;
***-- 需要反思:链接关键词不同,创作的表不同,如下:***
select s1.cno,s1.degree,s1.sno
from scores s1 inner join scores s2 
on s1.sno=s2.sno
order by s1.sno;

select s1.cno,s1.degree,s1.sno
from scores s1 inner join scores s2 
on s1.cno=s2.cno
order by s1.sno;

-- 22.查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。
select s1.Sno,s1.Sname,s1.Sbirthday
from students s1 inner join students s2
on year(s1.sbirthday) =year(s2.sbirthday)
where s1.sno='108';
-- 思路同第19题

-- 23.查询“张旭“教师任课的学生成绩。
select t.tno,t.tname,s.degree
from teachers t inner join courses c
on t.tno=c.tno
inner join scores s
on c.cno=s.cno
where t.tname='张旭';
-- 链接两张表

-- 24.查询选修某课程的同学人数多于5人的教师姓名。
select tname
from teachers t inner join courses c
on t.tno=c.tno
inner join scores s 
on c.cno=s.cno
group by s.cno
having count(sno)>5;
-- 以上为自己的作出来的,反馈信息如同答案给出的反馈信息,下面嵌套函数
SELECT DISTINCT Tname
FROM Scores INNER JOIN Courses
ON(Scores.Cno=Courses.Cno) INNER JOIN Teachers
ON(Courses.Tno=Teachers.Tno)
WHERE Courses.Cno IN(SELECT Cno FROM Scores GROUP BY(Cno) HAVING COUNT(Sno)>5);

---------------未完待续----------------------------------------------------

猜你喜欢

转载自blog.csdn.net/weixin_43093831/article/details/82965167
SQL
今日推荐