sql classic exercise ()

- 1. Query 01 courses of high-achieving students than 02 courses and course scores.

 

- Query 1, and fractional programs,

SC * sc from the SELECT
the WHERE sc.CId = '01 '
--- queries courses and 2 scores,
the SELECT * SC sc2 from
the WHERE sc2.CId = '02'


select * from Student RIGHT JOIN (

select t1.SId,t1.class1,t2.class2 from

(select sc.SId,sc.score as class1 from SC sc where sc.CId='01' )as t1,
(select sc2.SId,sc2.score as class2 from SC sc2 where sc2.CId='02') as t2


where t1.SId=t2.SId and t1.class1>t2.class2
) r

on Student.SId = r.SId;


--- 2. The case is higher than the average score of 60 points classmates

select s.Sname,s.SId ,k.平均分 from Student s
inner join

(select sc.SId,AVG(sc.score) as 平均分
from SC sc
group by sc.SId
)k

on s.SId=k.SId

- 3. score table, there are scores of student information

select s.* from student s inner join
(select distinct sc.SId from SC sc
where sc.score is not null ) k
on s.SId=k.SId

 

--- 4. All students student number, student name, total enrollment, a total score of all the courses


select s.SId,s.Sname,k.countCorse,k.scoreSum from Student s
inner join
(
select sc.SId,COUNT(sc.CId) as countCorse,SUM(sc.score)scoreSum from SC sc
group by sc.SId
) k
on s.SId=k.SId


--- 4-1. There are scores of student information
the SELECT * from Student S
the WHERE s.sid in (the SELECT SC.SId from SC)


select * from Student s
where exists (select * from SC sc where sc.SId=s.SId )


--- 5. Query teacher surnamed Li, the number of
the SELECT COUNT (1) Teacher t from
the WHERE t.Tname like 'Lee%'
Group by t.TId


--6 student information Joe Smith heard the teacher's lesson.
- (1) Joe Smith's class teacher taught
the SELECT c.CId from Course, c, Teacher t
the WHERE c.TId = t.TId and t.Tname = ' Joe Smith '

- (2) listening to students in that class information
SELECT * Student from S, SC SC
WHERE sc.CId in (
SELECT c.CId from Course, C, T Teacher
WHERE c.TId = t.TId and t.Tname = 'John Doe'

)
and s.SId=sc.SId


--- join query method
SELECT S. * Student from S, SC SC, Course, C, T Teacher
WHERE c.TId = t.TId and
c.CId = sc.CId and
sc.SId = s.sid
and t.Tname = 'John Doe'


--7 no school curriculum fully part of people's information:

- to find out the full school curriculum department name of the person. And not in


select * from Student s
where s.SId not in(

select sc.SId from SC sc
group by sc.SId
having count(sc.CId)=( select count(1) from Course)


. --8 and 01 students have obtained at least one course are learned with people
--- seek first 01 students of the course
- and then seek to modify these courses the student id

- then even table query


- the students learned the lesson of seeking s1.


select s.* from Student s

inner join
(
select distinct sc2.SId from SC sc2 where sc2.CId in(
select sc.CId from SC sc where sc.SId='01')
) k
on s.SId=k.SId


--- 11 inquiries two classes below 60 students of information, and the average score


People --- 12.01 course less than 60 minutes, in descending order


- First found 01 sid less than 60 minutes

select s.* from Student s ,SC sc
where sc.CId='01'and
sc.score<60
and s.SId=sc.SId
order by sc.score desc


--- 13 according to the average score of high to low, to obtain student information, grade point average, total score

select s.Sname,k.theAvg,k.theSum from Student s
inner join
(select sc.SId,AVG(sc.score) as theAvg ,SUM(sc.score) theSum from SC sc
group by sc.SId)k

on s.SId=k.SId
order by k.theAvg desc


--- 19 inquiries each course elective number of students
the SELECT cid, COUNT (sc.SId)
from SC sc
Group by sc.CId


--- 20. repair only 2 course student name, student and the above mentioned id
the SELECT S. S * Student from
Inner the Join

(select sc.SId theid from SC sc
group by sc.SId
having count(sc.CId) =2 ) k

on s.SId=k.theid


--21 query number of boys, the number of girls
the SELECT COUNT (s.Ssex), s.Ssex from Student S
Group by s.Ssex


Student Information --- 22 queries containing the word wind of
the SELECT * from Student S
the WHERE s.Sname like '%% wind'

 

All information --23 student queries with the same name

select * from Student s
where s.Sname in

(
select s.Sname from Student s
group by s.Sname
having count( s.Sname)>1
)


1900 --- 24-born people
the SELECT * from Student S
the WHERE YEAR (s.Sage) = 1990;

 

--- tie points greater than 26 is 85 person's name, average
SELECT s.Sname, s.sid, k.avgScore from Student S
Inner the Join
(
SELECT sc.SId theID, the AVG (sc.score) avgScore from SC SC
Group sc.SId by
) K
ON = k.theid s.sid


--- less than 27 mathematical name 60, and scores

--方法1:
select * from Student s
where s.SId in
(
select sc.SId from Course c,SC sc
where c.Cname='数学' and sc.CId=c.CId and sc.score<60
)


--方法2:
select s.* from Student s,Course c,SC sc
where s.SId=sc.SId and
sc.CId=c.CId and
c.Cname='数学' and
sc.score<60


--28 all students score all courses. Results did not allow
SELECT s.Sname, sc.CId, sc.score from Student S
Inner the Join SC SC = ON s.sid sc.SId

--29 One course is 70 points above the student's name, course name, score

- (1) to identify more than 70 points, sid, course name, score
the SELECT sc.SId, c.Cname, sc.score
from SC sc, c Course,
the WHERE sc.CId = c.CId and sc.score> 70

---综合
select s.Sname,c.Cname,sc.score from Student s ,SC sc,Course c
where sc.CId=c.CId and sc.score>70 and s.SId=sc.SId


--30 presence of substandard performance of course
the SELECT sc.CId, COUNT (*)
from SC sc
the WHERE sc.score <60
Group by sc.CId


--31. 01 students Course name more than 80 points, the student number


select s.* from SC sc,Student s
where sc.score>78 and sc.CId='01'and
s.SId=sc.SId

 

--- 32 number of students for each course of
the SELECT sc.CId, COUNT (sc.SId)
from SC sc
Group by sc.CId


--- 37 number of students on each course, greater than 5 was statistically
the SELECT sc.CId, COUNT (sc.SId)
from SC sc
Group by sc.CId


--- 38 students enrolled in the course 2
the SELECT sc.SId, COUNT (sc.CId) from SC sc
Group by sc.SId
the HAVING COUNT (sc.CId)> 2


--- 39 students enrolled in a full course name

select * from Student s
where s.SId in (

select sc.SId
from SC sc
group by sc.SId

having count(sc.CId)=(select count(*) from Course)
)

 

 

Reference https://www.jianshu.com/p/476b52ee4f1b

 

Guess you like

Origin www.cnblogs.com/bingyizhihun/p/11520829.html