Ideas for writing sql statements---personal ideas

-- sql statement test questions answers--
1. Query the student numbers of all students with higher grades in "a 1" course than "a 2" course
SELECT sid,sname FROM student WHERE sid IN
(
SELECT a.sid FROM (SELECT s.score ,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid
WHERE c.cname='Chinese')AS a,
(SELECT s.score,s.sid FROM score s INNER JOIN course c ON c. cid= s.cid
WHERE c.cname='English')AS b
WHERE a.sid=b.sid AND a.score>b.score
)
-- external in-app join query 1, internal in-app join query 1+internal Connection query 2 + conditional query (where) and
-- 2. Query the student number and average score of students whose average score is greater than 60;
SELECT AVG(score), sid FROM score AVG(s.score)>60 -- Error
SELECT AVG(score),sid FROM score s GROUP BY sid HAVING AVG(s.score)>60 -- Correct
-- Aggregate function, group query
-- 3. Query the student number, name, number of courses, and total score of all students
SELECT SUM(score),COUNT(cid),sid FROM score GROUP BY sid -- total score, number of courses selected, student number
SELECT sname,sid sid FROM student GROUP BY sid -- student number, name

SELECT SUM(score),COUNT(cid),s.sid,s.sname sid FROM score sc LEFT JOIN student s
ON sc.sid=s.sid GROUP BY s.sid -- total score, number of courses selected, student number, Name
-- left outer join query, grouping query, aggregate function
-- 4. Query the number of teachers with the last name "Li";
SELECT COUNT(tid) FROM teacher WHERE tname='Li'
-- aggregate function, conditional query

-- 5. Query the student IDs and names of students who have not taken Mr. Li's class;
SELECT c.cid,c.tid,sc.sid FROM course c LEFT JOIN score sc ON c.cid = sc.cid -- Query the student information of the same course
SELECT t.tid,c.cid,t.tname FROM teacher t RIGHT JOIN course c ON t.tid =c.tid WHERE t.tname='Li' -- Query the course taught by teacher Li course information

-- 上过李老师课的学生学号
SELECT a.sid FROM
(SELECT c.cid,c.tid,sc.sid FROM course c INNER JOIN score sc ON c.cid = sc.cid)AS a,
(SELECT t.tid,c.cid,t.tname FROM teacher t INNER JOIN course c ON t.tid =c.tid WHERE t.tname='李')AS b
WHERE a.tid = b.tid
-- 没上过李老师课的学生学号,姓名
SELECT sid,sname FROM student WHERE sid NOT IN (
SELECT a.sid FROM
(SELECT c.cid,c.tid,sc.sid FROM course c RIGHT JOIN score sc ON c.cid = sc.cid)AS a,
(SELECT t.tid,c.cid,t.tname FROM teacher t RIGHT JOIN course c ON t.tid =c.tid WHERE t.tname='李')AS b
WHERE a.tid = b.tid
)

-- Same as the problem-solving idea of ​​the first question + the use of not in, external conditional query in + internal right (both left and inside) join query, conditional query where
-- 6. Query has learned "```" and also learned it The student number and name of the classmates in the "``" course;
SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='Chinese' -- learned Chinese
SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='English' -- student number of students who have studied English --
all Same student number
SELECT sid,sname FROM student WHERE sid IN (
SELECT a.sid FROM
(SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='Chinese' )AS a,
(SELECT sc.sid,c.cid FROM course c INNER JOIN score sc ON c.cid=sc.cid WHERE c.cname='English')AS b
WHERE a.sid=b.sid
)
-- External Cartesian product query, conditional query (in),
-- 7. Query the student numbers and names of students who have studied all the courses taught by Mr. Li;
SELECT c.cid FROM course c INNER JOIN teacher t ON c.tid=t.tid WHERE t.tname='Li' -- Query the courses taught by Mr. Li
SELECT sc.sid FROM course c INNER JOIN score sc ON c.cid =sc.cid -- Query the student's student ID and the course ID of the course studied

SELECT sid,sname FROM student WHERE sid IN(
SELECT a.sid FROM
(SELECT c.cid,sc.sid FROM course c INNER JOIN score sc ON c.cid=sc.cid)AS a,
(SELECT c.cid FROM course c INNER JOIN teacher t ON c.tid=t.tid WHERE t.tname='李')AS b
WHERE a.cid=b.cid
)
---------------------------------------------------------------------------------------------------

Play the idea in the papyrus:

first question:

//
Apply the student number of
all students whose Chinese grades are higher than
mathematics , and get the student number
3. The same student sid=sid:a.sid = b.sid
4. Chinese score > math score where a.score = b.score


1.select a.sid from (select s.score,s.sid from score s inner join course c on c.cid= s.cid
where c.cname='Chinese')as a
2.
3.
4.
From student Look up in the table, look up according to the student number, seek the student number, and seek the student number
according to the two tables
select sid from student where sid in
(
select a.sid from (select s.score,s.sid from score s inner join course c on c. cid= s.cid
where c.cname='Chinese')as a,
select b.sid from (select s.score,s.sid from score s inner join course c on c.cid= s.cid
where c.cname ='math')as b
where a.sid=b.sid and a.score>b.score
)

(
SELECT a.sid FROM (SELECT s.score,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid
WHERE c.cname='语文')AS a,(SELECT s.score,s.sid FROM score s INNER JOIN course c ON c.cid= s.cid
WHERE c.cname='英语')AS b WHERE a.sid=b.sid AND a.score>b.score
)

select a.sid from a,b where a.sid=b.sid

2. Query the student number and average score of students whose average score is greater than 60;
average score avg(score)>60
sid
score table
select avg(score), sid from score avg(s.score)>60
SELECT AVG(score) ,sid FROM score s GROUP BY sid HAVING AVG(s.score)>60
3. Query the student number, name, number of courses selected, and total score of all
students

Student ID, total score, number of courses selected, - grade table, course
name student table
select sc.sid,sum(score),count(cid) from score sc inner join course c
on sc.cid=c.cid
(SELECT SUM (score) ssum,COUNT(cid) ccount,sid FROM score GROUP BY sid) as a
(SELECT sname,sid FROM student GROUP BY sid)as b
How to connect these two tables? There are common sid, merge common, leave different, what method to use,
join query
SELECT SUM(score), COUNT(cid), sid sid FROM score left join student group by sid

4. Query the number of teachers with the last name "Li" select count(tid) from teacher
where tname='Li'
-- aggregate function, conditional query
;
Query the student number and name of the student in the class select sid, sname
score table, student table
Have not taken the class of Mr. Li, have taken the class
of Mr. Li where tname='Li'
to query the student ID:
attended the class of Mr. Li The student number of the student who has
not attended Teacher Li's student number is to query
the student number of the student who has attended Mr. Li's class:
select sid from
attended Mr. Li's class
select a.sid from
(select sc.sid from course c inner join score sc on c.cid = sc.cid )as a
,(select * from teacher t inner join course c on t.tid =c.tid where t.tname='Li')

6. Query the student numbers and names of students who have studied "Chinese" and have also studied "English" courses;
query the student IDs of students who have studied Chinese,
query the student IDs of students who have studied English
sid=sid
Get the sname through the student ID from student
select sid,sname from student where sid in
Query the student number of students who have studied Chinese:
select sc.sid,c.cid from course c inner join score sc where c.cname='Chinese'
7. Query the teacher who has studied "Li" The student number and name of the students in all the courses
taught by Mr. Li
select c.cid from course c inner join teacher t on c.tid=t.tid where t.tname='Li'
select sid,sname from student where sid in -- query student name according to student number
select sc.sid from course c inner join score s on c.cid=sc.cid

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324518107&siteId=291194637