- 1, the query "02" high course grade student information and course grade "01" than the course select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a join score b on a.s_id=b.s_id and b.c_id='01' left join score c on a.s_id=c.s_id and c.c_id='02' or c.c_id = NULL where b.s_score>c.s_score - You can also write select a.*,b.s_score as 01_score,c.s_score as 02_score from student a,score b,score c where a.s_id=b.s_id and a.s_id=c.s_id and b.c_id='01' and c.c_id='02' and b.s_score>c.s_score - 2, the query "02" low course grade student information and course grade "01" than the course select a.* ,b.s_score as 01_score,c.s_score as 02_score from student a left join score b on a.s_id=b.s_id and b.c_id='01' or b.c_id=NULL join score c on a.s_id=c.s_id and c.c_id='02' where b.s_score<c.s_score - 3 inquiry average score of 60 points or greater number of students and the student's name and student grade point average select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING avg_score >=60; --4, the query is less than the average score of 60 points classmates student number and student name and grade point average - (including performance and non-performance) select b.s_id,b.s_name,ROUND(AVG(a.s_score),2) as avg_score from student b left join score a on b.s_id = a.s_id GROUP BY b.s_id,b.s_name HAVING avg_score <60 union select a.s_id,a.s_name,0 as avg_score from student a where a.s_id not in ( select distinct s_id from score); --5, the query for all students student number, total score the student's name, the total number of elective, all courses select a.s_id,a.s_name,count(b.c_id) as sum_course,sum(b.s_score) as sum_score from student a left join score b on a.s_id=b.s_id GROUP BY a.s_id,a.s_name; - 6, number of queries "Lee" the teacher's name select count(t_id) from teacher where t_name like '李%'; --7 query learned information "Joe Smith" of teachers to teach students select a.* from student a join score b on a.s_id=b.s_id where b.c_id in( select c_id from course where t_id =( select t_id from teacher where t_name = '张三')); - 8, the query never learned information "Joe Smith" of teachers to teach students select * from student c where c.s_id not in( select a.s_id from student a join score b on a.s_id=b.s_id where b.c_id in( select a.c_id from course a join teacher b on a.t_id = b.t_id where t_name ='张三')); 9 information, query learned numbered "01" and I have learned a number "02" in the course of the students - select a.* from student a,score b,score c where a.s_id = b.s_id and a.s_id = c.s_id and b.c_id='01' and c.c_id='02'; 10 information, query learned numbered "01" but did not learn the number "02" in the course of the students - select a.* from student a where a.s_id in (select s_id from score where c_id='01' ) and a.s_id not in(select s_id from score where c_id='02') --11 query did not learn the full information of the students for all courses - @ wendiepei wording select s.* from student s left join Score s1 on s1.s_id=s.s_id group by s.s_id having count(s1.c_id)<(select count(*) from course) - @ k1051785839 wording select * from student where s_id not in( select s_id from score t1 group by s_id having count(*) =(select count(distinct c_id) from course)) --12, the query has at least one course with the same number of students to learn "01" students have learned information select * from student where s_id in( select distinct a.s_id from score a where a.c_id in(select a.c_id from score a where a.s_id='01') ); - 13 and students query and "01" was exactly the same course of study of other students of information - @ ouyang_1993 wording SELECT Student.* FROM Student WHERE s_id IN (SELECT s_id FROM Score GROUP BY s_id HAVING COUNT(s_id) = ( # The following statement is to find the number of students learning courses '01' SELECT COUNT(c_id) FROM Score WHERE s_id = '01' ) ) AND s_id NOT IN ( # The following statement is found learned '01' students have not studied the course, which the students. And exclude them SELECT s_id FROM Score WHERE c_id IN( # The following statement is to find the '01' students never learned lessons SELECT DISTINCT c_id FROM Score WHERE c_id NOT IN ( # The following statement is to find courses '01' students learn SELECT c_id FROM Score WHERE s_id = '01' ) ) GROUP BY s_id ) # The following is excluded 01 students AND s_id NOT IN ('01') - @ k1051785839 wording SELECT t3. * FROM ( SELECT s_id, group_concat(c_id ORDER BY c_id) group1 FROM score WHERE s_id <> '01' GROUP BY s_id ) t1 INNER JOIN ( SELECT group_concat(c_id ORDER BY c_id) group2 FROM score WHERE s_id = '01' GROUP BY s_id ) t2 ON t1.group1 = t2.group2 INNER JOIN student t3 ON t1.s_id = t3.s_id --14 query never learned, "Joe Smith" to any one course teacher taught the student's name select a.s_name from student a where a.s_id not in ( select s_id from score where c_id = (select c_id from course where t_id =( select t_id from teacher where t_name = '张三'))); --15 query failed two or more courses and students of the school number, name and grade point average select a.s_id,a.s_name,ROUND(AVG(b.s_score)) from student a left join score b on a.s_id = b.s_id where a.s_id in( select s_id from score where s_score<60 GROUP BY s_id having count(1)>=2) GROUP BY a.s_id,a.s_name 16 student information retrieval "01" course score less than 60, in descending order by score - select a.*,b.c_id,b.s_score from student a,score b where a.s_id = b.s_id and b.c_id='01' and b.s_score<60 ORDER BY b.s_score DESC; --17, show average grades from high to low scores for all students in all courses and grade point average select a.s_id,(select s_score from score where s_id=a.s_id and c_id='01') as 语文, (select s_score from score where s_id=a.s_id and c_id='02') as 数学, (select s_score from score where s_id=a.s_id and c_id='03') as 英语, round(avg(s_score),2) as 平均分 from score a GROUP BY a.s_id ORDER BY 平均分 DESC; - @ drinking this cup there is a box wording SELECT a.s_id,MAX(CASE a.c_id WHEN '01' THEN a.s_score END ) 语文, MAX(CASE a.c_id WHEN '02' THEN a.s_score END ) 数学, MAX(CASE a.c_id WHEN '03' THEN a.s_score END ) 英语, avg(a.s_score),b.s_name FROM Score a JOIN Student b ON a.s_id=b.s_id GROUP BY a.s_id ORDER BY 5 DESC - 18. Query all subjects highest score, lowest score and the average score: Displayed in the following form: course ID, course name, highest score, lowest score, average score, pass rate, medium rate, good rate, excellent rate - passing of> = 60, Medium is: 70-80, for the good: 80-90, is excellent:> = 90 select a.c_id,b.c_name,MAX(s_score),MIN(s_score),ROUND(AVG(s_score),2), ROUND(100*(SUM(case when a.s_score>=60 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 及格率, ROUND(100*(SUM(case when a.s_score>=70 and a.s_score<=80 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 中等率, ROUND(100*(SUM(case when a.s_score>=80 and a.s_score<=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优良率, ROUND(100*(SUM(case when a.s_score>=90 then 1 else 0 end)/SUM(case when a.s_score then 1 else 0 end)),2) as 优秀率 from score a left join course b on a.c_id = b.c_id GROUP BY a.c_id,b.c_name --19, according to all subjects sort, and display ranking - mysql not rank function select a.s_id,a.c_id, @i: = @ i +1 as i reserved rankings, @k: = (case when @ score = a.s_score then @k else @i end) as rank ranking is not retained, @score:=a.s_score as score from ( select s_id,c_id,s_score from score GROUP BY s_id,c_id,s_score ORDER BY s_score DESC )a,(select @k:=0,@i:=0,@score:=0)s - @ k1051785839 wording (select * from (select t1.c_id, t1.s_score, (select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='01') rank FROM score t1 where t1.c_id='01' order by t1.s_score desc) t1) union (select * from (select t1.c_id, t1.s_score, (select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='02') rank FROM score t1 where t1.c_id='02' order by t1.s_score desc) t2) union (select * from (select t1.c_id, t1.s_score, (select count(distinct t2.s_score) from score t2 where t2.s_score>=t1.s_score and t2.c_id='03') rank FROM score t1 where t1.c_id='03' order by t1.s_score desc) t3) --20 query student total score and rank select a.s_id, @i:=@i+1 as i, @k:=(case when @score=a.sum_score then @k else @i end) as rank, @score:=a.sum_score as score from (select s_id,SUM(s_score) as sum_score from score GROUP BY s_id ORDER BY sum_score DESC)a, (select @k:=0,@i:=0,@score:=0)s --21 query different teachers teach different courses to low average display select a.t_id,c.t_name,a.c_id,ROUND(avg(s_score),2) as avg_score from course a left join score b on a.c_id=b.c_id left join teacher c on a.t_id=c.t_id GROUP BY a.c_id,a.t_id,c.t_name ORDER BY avg_score DESC; --22, student information queries score all courses of the first two to the third place and the course grade select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@i:=@i+1 as 排名 from score a,(select @i:=0)s where a.c_id='01' ORDER BY a.s_score DESC )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3 UNION select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@j:=@j+1 as 排名 from score a,(select @j:=0)s where a.c_id='02' ORDER BY a.s_score DESC )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3 UNION select d.*,c.排名,c.s_score,c.c_id from ( select a.s_id,a.s_score,a.c_id,@k:=@k+1 as 排名 from score a,(select @k:=0)s where a.c_id='03' ORDER BY a.s_score DESC )c left join student d on c.s_id=d.s_id where 排名 BETWEEN 2 AND 3; --23, statistics all subjects the fraction of the number of segments: course number, course name, [100-85], [85-70], [70-60], [0-60] and the percentage of select distinct f.c_name, a.c_id, b.`85-100`, b. percentage, c.`70-85`, c. percentage, d.`60-70`, d. percentage, e.`0 -60`, e. percentage from score a left join (select c_id,SUM(case when s_score >85 and s_score <=100 then 1 else 0 end) as `85-100`, ROUND(100*(SUM(case when s_score >85 and s_score <=100 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)b on a.c_id=b.c_id left join (select c_id,SUM(case when s_score >70 and s_score <=85 then 1 else 0 end) as `70-85`, ROUND(100*(SUM(case when s_score >70 and s_score <=85 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)c on a.c_id=c.c_id left join (select c_id,SUM(case when s_score >60 and s_score <=70 then 1 else 0 end) as `60-70`, ROUND(100*(SUM(case when s_score >60 and s_score <=70 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)d on a.c_id=d.c_id left join (select c_id,SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end) as `0-60`, ROUND(100*(SUM(case when s_score >=0 and s_score <=60 then 1 else 0 end)/count(*)),2) as 百分比 from score GROUP BY c_id)e on a.c_id=e.c_id left join course f on a.c_id = f.c_id --24, query the students grade point average and ranking select a.s_id, @i: = @ i + 1 as 'not retained vacancy ranking' @k: = (case when @ avg_score = a.avg_s then @k else @i end) as 'reserved vacancies ranking' @avg_score: = avg_s as 'average' from (select s_id,ROUND(AVG(s_score),2) as avg_s from score GROUP BY s_id ORDER BY avg_s DESC)a,(select @avg_score:=0,@i:=0,@k:=0)b; --25, the top three all subjects Search record - 1. All selected groups larger than a table results table b - 2. selected larger than the current id scores of less than three select a.s_id,a.c_id,a.s_score from score a left join score b on a.c_id = b.c_id and a.s_score<b.s_score group by a.s_id,a.c_id,a.s_score HAVING COUNT(b.s_id)<3 ORDER BY a.c_id,a.s_score DESC --26, the number of queries for each course is elective students select c_id,count(s_id) from score a GROUP BY c_id --27, check out the only two courses of all students in the school number and name select s_id,s_name from student where s_id in( select s_id from score GROUP BY s_id HAVING COUNT(c_id)=2); --28, query the number of boys and girls select s_sex,COUNT(s_sex) as 人数 from student GROUP BY s_sex 29 student information, query name contains "wind" word - select * from student where s_name like '%风%'; --30, homosexual students of the same name query list, and count the number of the same name select a.s_name,a.s_sex,count(*) from student a JOIN student b on a.s_id !=b.s_id and a.s_name = b.s_name and a.s_sex = b.s_sex GROUP BY a.s_name,a.s_sex --31, query the list of students born in 1990 select s_name from student where s_birth like '1990%' --32, query the average score for each course, results in descending order according to the average score, the average score is the same, according to the course numbers in ascending order select c_id,ROUND(AVG(s_score),2) as avg_score from score GROUP BY c_id ORDER BY avg_score DESC,c_id ASC - 33, the average score is greater than or equal to 85 inquiries for all students of the school number, name and grade point average select a.s_id,b.s_name,ROUND(avg(a.s_score),2) as avg_score from score a left join student b on a.s_id=b.s_id GROUP BY s_id HAVING avg_score>=85 - 34, the query name for the course "Mathematics" and scores below 60 student names and scores select a.s_name,b.s_score from score b join student a on a.s_id=b.s_id where b.c_id=( select c_id from course where c_name ='数学') and b.s_score<60 - 35, a query for all students courses and score situation; select a.s_id,a.s_name, SUM (case c.c_name when 'language' then b.s_score else 0 end) as 'language', SUM(case c.c_name when '数学' then b.s_score else 0 end) as '数学', SUM (case c.c_name when 'English' then b.s_score else 0 end) as 'English', SUM (b.s_score) as 'out' from student a left join score b on a.s_id = b.s_id left join course c on b.c_id = c.c_id GROUP BY a.s_id,a.s_name - 36, a query for any name in the course grade of 70 points or more, the course name and scores; select a.s_name,b.c_name,c.s_score from course b left join score c on b.c_id = c.c_id left join student a on a.s_id=c.s_id where c.s_score>=70 - 37, the query failed courses select a.s_id,a.c_id,b.c_name,a.s_score from score a left join course b on a.c_id = b.c_id where a.s_score<60 --38 inquiries courses numbered 01 students and the course grade school number and name in 80 points or more; select a.s_id,b.s_name from score a LEFT JOIN student b on a.s_id = b.s_id where a.c_id = '01' and a.s_score>80 - 39, find the number of students in each course select count(*) from score GROUP BY c_id; - 40 students granted elective courses inquiry "John Doe" Teacher, the highest-achieving students information and their grades - Query teacher id select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三' - Query highest score (might have the same score) select MAX(s_score) from score where c_id='02' -- search information select a.*,b.s_score,b.c_id,c.c_name from student a LEFT JOIN score b on a.s_id = b.s_id LEFT JOIN course c on b.c_id=c.c_id where b.c_id =(select c_id from course c,teacher d where c.t_id=d.t_id and d.t_name='张三') and b.s_score in (select MAX(s_score) from score where c_id='02') - 41, the same query different course grade student student number, course number, student achievement select DISTINCT b.s_id,b.c_id,b.s_score from score a,score b where a.c_id != b.c_id and a.s_score = b.s_score - 42, the top two in each door inquiry best work results - Niubi wording select a.s_id,a.c_id,a.s_score from score a where (select COUNT(1) from score b where b.c_id=a.c_id and b.s_score>=a.s_score)<=2 ORDER BY a.c_id --43 statistics the number of students per elective course (more than five courses only statistics). Requirements output number and the number of elective courses, according to the number of query results in descending order, if the same number of people, according to the course numbers in ascending order select c_id,count(*) as total from score GROUP BY c_id HAVING total>5 ORDER BY total,c_id ASC - 44, to retrieve at least two courses of elective student number select s_id,count(*) as sel from score GROUP BY s_id HAVING sel>=2 - 45, all queries elective courses student information select * from student where s_id in( select s_id from score GROUP BY s_id HAVING count(*)=(select count(*) from course)) --46, query each student's age - to count by date of birth, date of the current month <date of birth is May Day, the age minus one select s_birth,(DATE_FORMAT(NOW(),'%Y')-DATE_FORMAT(s_birth,'%Y') - (case when DATE_FORMAT(NOW(),'%m%d')>DATE_FORMAT(s_birth,'%m%d') then 0 else 1 end)) as age from student; --47 query birthday this week, students select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))=WEEK(s_birth) select * from student where YEARWEEK(s_birth)=YEARWEEK(DATE_FORMAT(NOW(),'%Y%m%d')) select WEEK(DATE_FORMAT(NOW(),'%Y%m%d')) - 48, the birthday of student inquiry next week select * from student where WEEK(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =WEEK(s_birth) - 49, student queries birthday this month select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d')) =MONTH(s_birth) --50, student inquiry next month birthday select * from student where MONTH(DATE_FORMAT(NOW(),'%Y%m%d'))+1 =MONTH(s_birth)