Use a SQL statement to query the names of students whose scores are greater than 80 in each course. The table style and data are as follows
student
name course score
张三 语文 81
张三 数学 75
李四 语文 76
李四 数学 90
王五 语文 81
王五 数学 100
王五 英语 90
The results are as follows:
Wang Wu
solve:
--not in
SELECT DISTINCT A.name
FROM student A
WHERE A.name not in(
SELECT Distinct S.name
FROM student S
WHERE S.score <80
)
name
王五
--not exists
SELECT DISTINCT A.name
From student A
where not exists (
SELECT 1
From student S
Where S.score <80
AND S.name =A.name
)
name
王五
--另外使用having函数的方法
select name
from student
group by name
having min(score) >= 80
;
name
王五
Remarks: create table and data
create table student(name varchar(30),course varchar(30),score int);
insert into student values('张三','语文',81);
insert into student values('张Three','Mathematics',75);
insert into student values('李四','语文',76);
insert into student values('李四','Mathematics',90);
insert into student values(' Wang Wu','语文',81);
insert into student values('王五','Mathematics',100);
insert into student values('王五','英文',90);