[Basic knowledge] sql query statement

Title query student information with grades in the SC table

select s.sname,s.sid,s.sage,s.ssex from sc inner join student as s on sc.sid=s.sid
group by s.sname,s.sid,s.sage,s.ssex

Query the number of teachers with the surname "Li"

SELECT COUNT(*) FROM teacher where tname LIKE '李%';

Query the highest score, lowest score and average score of each subject

SELECT c.cid,c.cname,MAX(sc.score),MIN(sc.score) ,AVG(sc.score) FROM sc LEFT JOIN course as c ON sc.cid=c.cid GROUP BY c.cid,c.cname

Query the number of boys and girls

SELECT ssex,COUNT(1) FROM student GROUP BY ssex

Query the information of students whose names contain the word "wind"

SELECT * FROM student where sname LIKE '%风%'

Query the list of students with the same name and the same sex, and count the number of students with the same name

SELECT sname,ssex,COUNT(*) FROM student as st GROUP BY st.sname,st.ssex HAVING COUNT(*)>1

Query the list of students born in 1990

SELECT sname from student WHERE sage like '%1990%'

Find the number of students in each course

SELECT course.cid,course.cname,COUNT(*) FROM sc  INNER JOIN  course ON sc.cid=course.cid GROUP BY course.cid,course.cname

Count the number of students enrolled in each course (only for courses with more than 5 students)

SELECT sc.cid,c.cname,COUNT(*) FROM sc 
left join course c on sc.cid = c.cid GROUP BY sc.cid,c.cname HAVING COUNT(*)>5;

Retrieve the ID numbers of students who have taken at least two courses

SELECT sid FROM sc GROUP BY sid HAVING COUNT(*)>=2;

Guess you like

Origin blog.csdn.net/DecorateCC/article/details/130626911