总共4道题 – 4 简单
595.大的国家
Table World
Column Name | Type |
---|---|
name | varchar |
continent | varchar |
area | int |
population | int |
population | int |
分别表示:国家名称、所属大陆、面积、人口和 GDP 值
如果一个国家满足下述两个条件之一,则认为该国是大国 :
面积至少为 300 万平方公里(即,3000000 km2),或者人口至少为 2500 万(即 25000000)
编写一个 SQL 查询以报告大国的国家名称、人口和面积
select name,area,population from World
where area >= 3000000 or population >= 25000000;
1757.可回收且低脂的产品
Table Products
Column Name | Type |
---|---|
product_id | int |
low_fats | enum |
recyclable | enum |
product_id 是这个表的主键。
low_fats 是枚举类型,取值为以下两种 (‘Y’, ‘N’),其中 ‘Y’ 表示该产品是低脂产品,‘N’ 表示不是低脂产品。
recyclable 是枚举类型,取值为以下两种 (‘Y’, ‘N’),其中 ‘Y’ 表示该产品可回收,而 ‘N’ 表示不可回收
写出 SQL 语句,查找既是低脂又是可回收的产品编号。返回结果无顺序要求
select product_id from Products
where low_fats = 'Y' and recyclable = 'Y';
584.寻找用户推荐人
Table customers
id | name | referee_id |
---|---|---|
1 | Will | NULL |
2 | Jane | NULL |
3 | Alex | 2 |
4 | Bill | NULL |
5 | Zack | 1 |
6 | Mark | 2 |
写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2
select name from customer
where referee_id != "2" or referee_id is null;
注意:如果以下列SQL语句查询,会将 referee_id = null 的值剔除,与题意不符
select name from customer where referee_id != "2";
原因是:MySQL 使用三值逻辑 —— TRUE, FALSE 和 UNKNOWN。任何与 NULL 值进行的比较都会与第三种值 UNKNOWN 做比较。这个“任何值”包括 NULL 本身!这就是为什么 MySQL 提供 IS NULL 和 IS NOT NULL 两种操作来对 NULL 特殊判断。
183.从不订购的客户
Table Customers
Id | Name |
---|---|
1 | Joe |
2 | Henry |
3 | Sam |
4 | Max |
Table Orders
Id | CustomerId |
---|---|
1 | 3 |
2 | 1 |
编写一个 SQL 查询,找出所有从不订购任何东西的客户
select name as "Customers" from Customers where Id not in(
select CustomerId from Orders
)
–今日SQL专项打卡结束
–上海金融科技行业小小打工人,欢迎同行互相学习交流,共同进步
–20220817