-- Queries:
-- 1) How many people are exclusively students?
select count(*) from Student s where s.year<=4;
-- 2) How many people are exclusively teachers?
select count(*) from Teacher t where t.teacherid NOT in (select studentid from Student);
-- 3) How many people are both students and teachers?
select count(*) from Teacher t where t.teacherid in (select studentid from Student);
-- 4) Assume you have a student name Mickey Mouse, what is his Data Structure & Algorithm Grade in Spring 2010?
select s.fname, s.lname, e.grade, c.coursenumber, c.name, st.secterm, st.secyear
from Student s, Course c, Section st, Enrollment e
where s.studentid=e.studentid and c.courseid=st.courseid
and st.secid = e.secid
and s.fname='Mickey' and s.lname='Mouse'
and c.courseid='100100'
and st.secterm ='Spring'
and st.secyear='2010';
-- 5) Assume you have a student name Minnie Mouse, what is her GPA?
select ROUND(sum(point) /count(point), 1) as GPA from (
select point, count(point) from GpaLookup where grade in (
select e.grade
from Student s, Course c, Section st, Enrollment e
where s.studentid=e.studentid and c.courseid=st.courseid
and st.secid = e.secid
and s.fname='Minnie' and s.lname='Mouse'
and st.secyear='2010')
group by point) as result;
-- 6) What is the median grade for all students taking Physics in Fall 2010?
select grade as medin_grade from GpaLookup where point = (
select ROUND(avg(point), 1) as median from (
select point from GpaLookup where grade in (
select e.grade from Section st, Course c, Enrollment e
where
st.courseid = c.courseid
and st.secid = e.secid
and c.name = 'Physics'
and st.secterm = 'Fall'
and st.secyear = '2010' )) as foo);
-- 7) Assume you have a professor name Donald Duck, what courses did he teach in Summer 2010?
select c.name from Section st, Course c, Teacher t
where st.courseid = c.courseid and t.teacherid = st.teacherid
and st.secterm = 'Summer'
and st.secyear = '2010'
and t.fname ='Donald'
and t.lname ='Duck'
-- 8) What courses has Minnie Mouse taken with Donald Duck as the professor?
select c.name from Student s, Course c, Teacher t, Enrollment e, Section st
where st.teacherid = t.teacherid and c.courseid = st.courseid and s.studentid = e.studentid and e.secid = st.secid
and t.fname = 'Donald'
and t.lname = 'Duck'
and s.fname = 'Minnie'
and s.lname ='Mouse'
group by c.courseid
-- 9) Assume that Snow White is both a student and a teacher. What classes has Snow White and Minnie Mouse taken together as students?
select c.name from Course c, Student s, Enrollment e, Section st
where st.courseid = c.courseid and e.studentid = s.studentid and e.secid = st.secid
and (( s.fname = 'Snow'
and s.lname = 'White')
or ( s.fname = 'Minnie'
and s.lname = 'Mouse'))
group by c.courseid
having count(s.studentid) = 2;
-- 10) What classes has Snow White taught that Minnie Mouse has attended?
select c.name, st.secterm, st.secyear from Teacher t, Course c, Section st, Student s, Enrollment e
where st.teacherid = t.teacherid and st.courseid = c.courseid and st.secid = e.secid
and t.fname = 'Snow'
and t.lname = 'White'
and s.fname = 'Minnie'
and s.lname ='Mouse'
-- 11) What classes have been taught by both Donald Duck and Snow White?
select c.name as cname, count(distinct t.teacherid) as count from Course c, Teacher t, Section st
where t.teacherid = st.teacherid and st.courseid = c.courseid
and (( t.fname = 'Snow'
and t.lname = 'White')
or ( t.fname = 'Donald'
and t.lname = 'Duck'))
group by name
having count(t.teacherid)>=2;
-- 12) Assume that Mickey Mouse has taken Data Structures & Algorithms several times, Spring 2010 for a C-, Summer 2010 for a B+,
-- and Fall 2010 for a A-, and Winter 2010 for an A. What was his highest grade for Data Structure and Algorithms and what term was that in?
select e.grade, st.secterm, st.secyear from Section st, Course c, Enrollment e, Student s
where
st.courseid = c.courseid
and st.secid = e.secid
and s.fname = 'Mickey'
and s.lname ='Mouse'
and c.coursenumber = 'CS223'
and e.grade = ( select grade from GpaLookup where point = (
select max(point) from (
select point from GpaLookup where grade in (
select e.grade from Section st, Course c, Enrollment e, Student s
where
st.courseid = c.courseid
and st.secid = e.secid
and s.fname = 'Mickey'
and s.lname ='Mouse'
and c.coursenumber = 'CS223')) as foo)
);
-- 13) How many classes did Snow White take in 2010?
select count(c.courseid) from Course c, Student s, Section st, Enrollment e
where c.courseid = st.courseid and s.studentid = e.studentid and e.secid = st.secid
and st.secyear = '2010'
and s.fname = 'Snow'
and s.lname = 'White'
Query sql
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 本文为原创,重在学习,轻在打赏。 环境介绍 靶机采用bWAPP(buggy web Application),环境...
- SQL 语句主要分为 3 类 DDL (Data Definition Language) 语句:数据定义语句,这...
- 分析SQL执行带来的开销是优化SQL的重要手段。在MySQL数据库中,可以通过配置profiling参数来启用SQ...