SELECT
*
FROM
student s
JOIN sc a ON s.SId = a.SId
join sc b on s.sid=b.sid and a.cid=01 and b.cid=02
where a.score>b.score;
SELECT
*
FROM
sc a
JOIN sc b ON a.sid = b.sid
where a.cid='01' and b.cid='02'
SELECT
*
FROM
sc a
LEFT JOIN sc b ON a.sid = b.sid
AND b.cid = '02'
WHERE
a.cid = '01'
SELECT
*
FROM
sc a
WHERE
sid NOT IN ( SELECT sid FROM sc b WHERE cid = '01' )
AND cid = '02'
SELECT
s.sid,
s.sname,
AVG( sc.score )
FROM
student s
JOIN sc ON s.sid = sc.sid
GROUP BY
sc.sid,
s.sname
having AVG(sc.score)>60
SELECT
s.sid,
s.sname,
count( sc.cid ) as '选课总数',
sum( score ) as '所有课程总成绩'
FROM
student s
LEFT JOIN sc ON s.sid = sc.sid group BY s.sid,
s.sname;
SELECT
s.*
FROM
student s
JOIN sc ON s.sid = sc.sid
JOIN course c ON sc.cid = c.CId
JOIN teacher t ON c.tid = t.tid
WHERE
t.Tname = '张三';
select * from student where SId in
(SELECT
s.sid
FROM
student s
left JOIN sc ON s.sid = sc.sid
group by s.sid
having count(sc.cid)<(select count(cid) from course))
SELECT DISTINCT
s.*
FROM
student s
JOIN sc ON s.sid = sc.sid
WHERE
cid IN ( SELECT cid FROM sc WHERE sid = 01 );
思路:1、没有01号同学课程以外的课程记录,
(01号同学学习了哪些课程?,找到学习01号课程外的同学,取个反)
2、与01号同学学习的课程数量相同
select sc.sid from sc
join student s on sc.sid=s.sid
where sc.sid not in (select sid from sc where cid not in(select cid from sc where sid='01'))
group by s.sid
having count(cid)=(select count(cid) from sc where sid='01')
思路:找出学习过的,再not in
SELECT
*
FROM
student
WHERE
sid NOT IN (
SELECT
s.sid
FROM
student s
LEFT JOIN sc ON s.sid = sc.sid
WHERE
sc.cid = ( SELECT cid FROM course c JOIN teacher t ON c.tid = t.tid WHERE t.Tname = '张三' )
SELECT
s.sid,s.sname,avg(score)
FROM
student s
JOIN sc ON s.sid = sc.sid
WHERE
score < 60
GROUP BY
s.sid,s.sname
HAVING
count( score )>=2;
SELECT
sc.cid,
c.cname,
max( sc.score ) as '平均分',
min( sc.score ) as '最低分',
avg( sc.score ) as '平均分',
count(1) as '选修人数',
sum(case when score>=60 then 1 else 0 end )/count(1) as '及格率',
sum(case when score>=70 and score<80 then 1 else 0 end)/count(1) as '中等率',
sum(case when score >=80 and score <90 then 1 else 0 end )/count(1) as '优良率',
sum(case when score >=90 then 1 else 0 end )/count(1) as '优秀率'
FROM
sc
JOIN course c ON sc.cid = c.cid
GROUP BY
sc.cid,
c.cname;
order by count(1) desc ,cid
select cid,score,@rank:=@rank+1 as '排名' from sc,(select @rank:=0) as t order by score desc
SELECT
cid,
score,
CASE
WHEN @score = score
THEN @rank
when @score:=score
THEN @rank:=@rank+1
END
AS '排名'
FROM
sc,(
SELECT
@rank :=0,
@score :=NULL) as t
order by score desc;
百分比可以用cancat函数
SELECT
sc.cid,
cname,
concat(sum(case when score>85 and score <=100 then 1 else 0 end)/count(1)*100,'%') as '(85-100]',
concat( sum(case when score>70 and score <=85 then 1 else 0 end)/count(1)*100,'%')as '(70-85]',
FROM
sc
JOIN course c ON sc.cid = c.cid
GROUP BY
sc.cid,
c.cname
思路:前三名转换为:若大于此成绩的的数量少于3即为前三名。
SELECT
*
FROM
sc a
WHERE
( SELECT count( 1 ) FROM sc b WHERE b.cid = a.cid AND b.score > a.score )< 3
ORDER BY
score DESC
SELECT
s.sid,
s.sname,
count(1) as '选课数'
FROM
student s
JOIN sc ON s.sid = sc.sid
GROUP BY
s.sid,
s.sname
HAVING
count( 1 )=2
select s.ssex,count(1) from student s group by s.ssex
SELECT
a.sname,
a.ssex,
count( 1 ) as '同名同性别人数'
FROM
`student` a
JOIN student b ON a.sname = b.sname
AND a.ssex = b.ssex
AND a.sid != b.sid
group by a.sname,a.ssex
21、查询1990年出生的学生名单
用year()函数
SELECT
*
FROM
student s
WHERE
YEAR ( sage )= '1990'
SELECT
cid,
avg( score )
FROM
sc
GROUP BY
cid
ORDER BY
avg( score ),
cid
SELECT
s.sid,
s.sname,
avg( score )
FROM
sc
JOIN student s ON sc.sid = s.sid
GROUP BY
s.sid,
s.sname
HAVING
avg( score )>= 85
ORDER BY
avg(
score
)
SELECT
s.sname,
score
FROM
student s
JOIN sc ON s.sid = sc.sid
JOIN course c ON sc.cid = c.cid
WHERE
cname = '数学'
AND score < 60;
SELECT
s.sid,
s.sname,
c.cname,
sc.score
FROM
student s
JOIN sc ON s.sid = sc.sid
JOIN course c ON sc.cid = c.cid
WHERE
sc.score > 70
SELECT
s.*,
sc.score
FROM
student s
JOIN sc ON s.sid = sc.sid
JOIN course c ON sc.cid = c.cid
JOIN teacher t ON c.tid = t.tid
WHERE
t.tname = '张三'
ORDER BY
score DESC
LIMIT 1
SELECT
s.sid,
a.cid,
a.score
FROM
student s
JOIN sc a ON s.sid = a.sid
join sc b on a.sid=b.sid
where a.cid!=b.cid and a.score=b.score
group by s.sid,a.cid,a.score
SELECT
a.cid,
a.score
FROM
sc a where ( SELECT count(*) FROM sc b WHERE a.CId = b.CId AND b.score > a.score )<2
ORDER BY
a.cid
SELECT
s.sid,
s.sname
FROM
student s
JOIN sc ON s.sid = sc.sid
GROUP BY
s.sid,
s.sname
HAVING count( cid )=(SELECT
count( 1 )
FROM
course)
31、查询个学生的年龄,只按月份来算,
SELECT YEAR
(
NOW())- YEAR ( sage )
FROM
student
32、按照出生日期来算年龄,当前月日<出生的月日,则年龄减一。
SELECT
*,
timestampdiff(
YEAR,
sage,
now())
FROM
student