SQL classic interview questions

Use a SQL statement to query the xuesheng table for the names of students whose scores are greater than 80 in each course. This is a very classic interview question for the SQL interview test.

Having and not in
query the xuesheng table for the names of students whose scores are greater than 80 for each course

name kecheng score
Zhang Sanyu 81
Zhang San Mathematics 73
Li Siyu 86
Li Si Mathematics 90
Wang Wu Mathematics 89
Wang Wu Language 88 Wang Wu
English 96
Solution One:
Having If you don’t consider the student’s course less entry situation (such as Zhang San only 2 courses, Wang Wu has 3 courses)

SELECT name
FROM xuesheng
GROUP BY name
HAVING MIN(score)> 80
If considering the number of students' courses is greater than or equal to 3

SELECT name
FROM xuesheng
GROUP BY name
HAVING MIN(score)> 80
AND COUNT(kecheng)>=3
Solution 2: Not in
can use reverse thinking, first query the table for names with less than 80 points, and then use not in Remove

SELECT DISTINCT name
FROM xuesheng
WHERE name NOT IN
(SELECT DISTINCT name
FROM xuesheng
WHERE score <=80);

Delete the
student table xueshengbiao as follows: automatically number student number name course number course name score

autoid id name kcid kcname score
1 2005001 Zhang San 0001 Mathematics 69
2 2005002 Li Si 0001 Mathematics 89
3 2005001 Zhang San 0001 Mathematics 69
Delete redundant student information that is the same except for the automatic numbering.

DELETE t1
FROM xueshengbiao t1, xueshengbiao t2
WHERE t1.id = t2.id
and t1.name = t2.name
and t1.kcid = t2.kcid
and t1.kcname = t2.kcname
and t1.score = t2.score
and t1.autoid < t2.autoid

If you only find out the redundant information of the students whose automatic number is different and the others are the same, you can use group by

SELECT * from xueshengbiao t1
WHERE t1.autoid
NOT IN
(SELECT MIN(autoid) as autoid FROM xueshengbiao
GROUP BY id, name, kcid, kcname, score)

Fuzzy query%
table name: student, use sql to query the information of students whose average score is greater than 75 among the students with the surname "Zhang";

name kecheng score
Zhang Qing Chinese 72
Zhang Hua English 81
Wang Hua Mathematics 72
Zhang Qing Physics 67
Li Li Chemistry 98
Zhang Qing Chemistry 76
select * from student
where name in
(select name from student
where name like'张%' group by name having avg(score)> 75);
SQL wildcards
In SQL, wildcards are used with the SQL LIKE operator. SQL wildcards are used to search data in the table. In SQL, the following wildcards can be used:

Wildcard description
% Replace 0 or more characters
_ Replace one character
[charlist] Any single character in the character list
[^charlist] or [!charlist] Do not
use any single character in the character list MySQL uses REGEXP or NOT REGEXP operators (Or RLIKE and NOT RLIKE) to manipulate regular expressions

Find out the classmates surnamed Zhang and Li, and use rlike to match multiple

– Find out the
select * from xuesheng
where name in
(select name from xuesheng
where name rlike'[张李]' group by name having avg(score)> 75); find the surname Zhang and surname Lee select * from xuesheng where name rlike'[张李]' group by name having avg(score)> 75);
REGEXP can also be used, combined with regular matching

select * from xuesheng
where name in
(select name from xuesheng
where name REGEXP ‘1’ group by name having avg(score) > 75);


  1. Zhang Li↩︎

Guess you like

Origin blog.csdn.net/w13716207404/article/details/103060640