查询满足任意条件之一
±------------±--------+
| Column Name | Type |
±------------±--------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | int |
±------------±--------+
name 是这张表的主键。
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。
如果一个国家满足下述两个条件之一,则认为该国是 大国 :
面积至少为 300 万平方公里(即,3000000 km2),或者
人口至少为 2500 万(即 25000000)
编写一个 SQL 查询以报告 大国 的国家名称、人口和面积。
where/or
or在数据少时使用 ,数据量大使用会破坏索引导致性能降低,。
例1:select country,area from world where area >= 5000000 or pop >=2500000
where/union
数据量大推荐使用union
例2:select t.country,t.area from world t where t.area >= 5000000 union select t.country,t.area from world t where t.pop >= 25000000
超过5名学生的课
表: Courses
±------------±--------+
| Column Name | Type |
±------------±--------+
| student | varchar |
| class | varchar |
±------------±--------+
(student, class)是该表的主键列。
该表的每一行表示学生的名字和他们注册的班级。
编写一个SQL查询来报告 至少有5个学生 的所有班级。
例1;select class from Courses group by class having count(distinct student)>=5
借鉴
例2:select class from (select class count(distinct student) as num from Courses group by class ) as temp where num>=5
例3: