数据库练习MySQL
select sname,Sage,Ssex
from student
where sage>(SELECT sage
from student
WHERE Sname='王华' );
select Sno
FROM sc
where Cno='002' AND Grade=(
SELECT max(Grade)
FROM sc
where cno='002'
);
select *
from student
WHERE sno IN(
select Sno
from sc
GROUP BY Grade
having AVG(grade)>90
);
select Sname
from student
where sno not in(
select Sno
FROM SC
where cno='001'
);
select student.sname,sc.grade
from sc,student,course
where sc.sno=student.sno and course.Cno=sc.Cno
and course.Cname='数据库';
select student.sname,sc.grade
from sc,student,course
where sc.sno=student.sno and course.Cno=sc.Cno
AND student.Sdept='CS' and course.Cname='数据库'
ORDER BY Grade DESC;
select Distinct (COUNT(*))
from course,sc
where course.Cno=sc.Cno and course.Cname='数据库';
SELECT cno,COUNT(DISTINCT(sc.sno))
from sc
GROUP BY Cno;
SELECT student.sage,COUNT(DISTINCT student.Sno)
from sc,student
WHERE sc.sno=student.sno
GROUP BY student.Sage;
select Student.Sage,count(distinct Student.Sno)
from Student,SC
where SC.Sno = Student.Sno
group by Student.Sage;
SELECT Sno,sum(course.Credit)
from sc,course
where sc.cno=course.cno AND sc.Grade>=60
GROUP BY sc.Sno;
SELECT student.sno,SumGrade
FROM student,(
select student.sno,sum(sc.Grade) SumGrade
from student,sc
where student.sno=sc.sno and sc.Grade>=60
GROUP BY student.Sno
having COUNT(*)>2
) as scc
WHERE student.sno=scc.Sno
ORDER BY SumGrade DESC;
SELECT student.sno
FROM student,(
select student.sno,sum(sc.Grade) SumGrade
from student,sc
where student.sno=sc.sno and sc.Grade>=60
GROUP BY student.Sno
having COUNT(*)>=2
) as scc
WHERE student.sno=scc.Sno;
SELECT student.Sno
from student,sc,course
where student.ssex='M' and student.sno=sc.Sno
AND sc.cno=course.Cno
gROUP BY student.Sno
HAVING max(course.Credit)>=5 and min(sc.Grade>=60);
SELECT student.Sno,AVG(sc.Grade)
from student,sc
WHERE student.sno=sc.Sno AND student.Sdept='IS'
GROUP BY student.Sno
having AVG(sc.Grade)>=90;
SELECT course.Cname
from course ,student LEFT JOIN sc on student.sno=sc.Sno
WHERE student.Sname='张三' and sc.cno=course.cno;
select sno,sname
from student
where sno in(
select sno
from sc
WHERE sc.grade>60
group by Sno
having COUNT(*)=(select COUNT(*) from course)
);
select sno,Sname
from student
where Sdept='IS' and sno in(
select Sno
from sc
GROUP BY Sno
having COUNT(*)=(SELECT COUNT(*) from course)
);
update SC
set Grade := Grade+10
where Sno = (
select Sno
from student
where Sname = '刘晨');
create VIEW view_student_course_grade as;
create view view_student_course_grade as
select Student.Sno,Student.Sname,Course.Cname,SC.Grade
from Student,SC,Course
where Student.Sno = SC.Sno and SC.Cno = Course.Cno;
CREATE VIEW view_student_average_grade as
SELECT student.sno,AVG(sc.grade)
from student,sc
where student.sno=sc.Sno
GROUP BY student.Sno;
|