Use sql statement to find out the names of students whose scores are greater than 80 in each course

According to the structure of the table, use the sql statement to query and
Insert picture description here
build the table SQL as follows:

create table stu_grade(name varchar(20),coursevarchar(20),grade int);


– Insert data


INSERT INTO stu_gradeVALUES ('Zhang San','语文', '81');
INSERT INTO stu_gradeVALUES ('Zhang San','Mathematics', '75');
INSERT INTO stu_gradeVALUES ('李四','语文', ' 76');
INSERT INTO stu_gradeVALUES ('李四','Mathematics', '90');
INSERT INTO stu_gradeVALUES ('王五','语文', '81');
INSERT INTO stu_gradeVALUES ('王五', ' Math', '100');
INSERT INTO stu_gradeVALUES ('Wang Wu','English', '90');

Query the names of students whose scores are greater than 80 in each course:

select distinct name from stu_grade where name not in(select distinct name from stu_grade where grade <=80);

There is another way

select name from stu_grade group by name HAVING MIN(grade) > 80;

Extension:
SQL SELECT distinct statement
Concept:
In the table, there may be duplicate values. This is not a problem, but sometimes you may wish to list only distinct values

Syntax:
SELECT DISTINCT column name FROM table name
When there are multiple identical values ​​in the result set, if you need unique different values, use the distinct statement.
Insert picture description here
Two w3Schools need unique values. At this time,
SELECT DISTINCT Company FROM Orders
Insert picture description here
is now in the result. Centralized, "W3School" is listed only once.

Guess you like

Origin blog.csdn.net/MiaoWei_/article/details/109201771