【LeetCode-SQL专项突破】-第1天:选择

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
云数据库 RDS MySQL,高可用系列 2核4GB
简介: 【LeetCode-SQL专项突破】-第1天:选择

a202c0275f9e44eda2cd0a0cab0203a1.png

595.大的国家


🚀 World 表:
+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| name        | varchar |
| continent   | varchar |
| area        | int     |
| population  | int     |
| gdp         | int     |
+-------------+---------+
name 是这张表的主键。
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。
🚀 需求
如果一个国家满足下述两个条件之一,则认为该国是大国 :
面积至少为 300 平方公里(即,3000000 km2),或者人口至少为 2500 万(即 25000000)
编写一个 SQL 查询以报告 大国 的国家名称、人口和面积。
按 任意顺序 返回结果表。
查询结果格式如下例所示。
🚀 示例:
输入:
World 表:
+-------------+-----------+---------+------------+--------------+
| name        | continent | area    | population | gdp          |
+-------------+-----------+---------+------------+--------------+
| Afghanistan | Asia      | 652230  | 25500100   | 20343000000  |
| Albania     | Europe    | 28748   | 2831741    | 12960000000  |
| Algeria     | Africa    | 2381741 | 37100000   | 188681000000 |
| Andorra     | Europe    | 468     | 78115      | 3712000000   |
| Angola      | Africa    | 1246700 | 20609294   | 100990000000 |
+-------------+-----------+---------+------------+--------------+
输出:
+-------------+------------+---------+
| name        | population | area    |
+-------------+------------+---------+
| Afghanistan | 25500100   | 652230  |
| Algeria     | 37100000   | 2381741 |
+-------------+------------+---------+
🐴🐴 答案
# Write your MySQL query statement below
select name,population,area from World
where area>=3000000
or population >=25000000
/* Write your T-SQL query statement below */
select name,population,area from World
where area>=3000000
or population >=25000000
/* Write your PL/SQL query statement below */
select 
name "name",
population "population",
area "area"
from World
where area>=3000000
or population >=25000000


b77da38bc0384de385026259999c2034.png


1757. 可回收且低脂的产品


🚀 表: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 语句,查找既是低脂又是可回收的产品编号。
返回结果 无顺序要求 。
查询结果格式如下例所示:
Products 表:
+-------------+----------+------------+
| product_id  | low_fats | recyclable |
+-------------+----------+------------+
| 0           | Y        | N          |
| 1           | Y        | Y          |
| 2           | N        | Y          |
| 3           | Y        | Y          |
| 4           | N        | N          |
+-------------+----------+------------+
Result 表:
+-------------+
| product_id  |
+-------------+
| 1           |
| 3           |
+-------------+
只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。
🐴🐴 答案
# Write your MySQL query statement below
select product_id  from Products
where low_fats = 'Y'
and recyclable ='Y'
/* Write your T-SQL query statement below */
select product_id  from Products
where low_fats = 'Y'
and recyclable ='Y'
/* Write your PL/SQL query statement below */
select product_id "product_id" from Products
where low_fats = 'Y'
and recyclable ='Y'


98e43d2181af497c9e1814beabdfd8dc.png


584. 寻找用户推荐人

b143510fc6c24985b77a3855ed12154b.pngb143510fc6c24985b77a3855ed12154b.png

🚀 给定表 customer ,里面保存了所有客户信息和他们的推荐人。
+------+------+-----------+
| id   | name | referee_id|
+------+------+-----------+
|    1 | Will |      NULL |
|    2 | Jane |      NULL |
|    3 | Alex |         2 |
|    4 | Bill |      NULL |
|    5 | Zack |         1 |
|    6 | Mark |         2 |
+------+------+-----------+
🚀 需求
写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都不是2。
对于上面的示例数据,结果为:
+------+
| name |
+------+
| Will |
| Jane |
| Bill |
| Zack |
+------+
🐴🐴 答案
# Write your MySQL query statement below
select name  from customer
where IFNULL(referee_id,0) <> 2
--mysql判断非空的函数
ISNULL(expr)  如果expr为null返回值1,否则返回值为0
IFNULL(expr1,expr2) 如果expr1值为null返回expr2的值,否则返回expr1的值
/* Write your T-SQL query statement below */
select name  from customer
where referee_id <> 2 OR referee_id IS NULL
/* Write your PL/SQL query statement below */
select name "name"  from customer
where nvl(referee_id,0) <> 2

b143510fc6c24985b77a3855ed12154b.png

183. 从不订购的客户

3d81d6453d0243f69d1555f93069ac17.png

🚀 某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
+----+-------+
| Id | Name  |
+----+-------+
| 1  | Joe   |
| 2  | Henry |
| 3  | Sam   |
| 4  | Max   |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1  | 3          |
| 2  | 1          |
+----+------------+
🚀 需求
例如给定上述表格,你的查询应返回:
+-----------+
| Customers |
+-----------+
| Henry     |
| Max       |
+-----------+
🐴🐴 答案
# Write your MySQL query statement below
select Name "Customers" from Customers
where id not in (select CustomerId  from Orders)
/* Write your T-SQL query statement below */
select Name "Customers" from Customers
where id not in (select CustomerId  from Orders)
/* Write your PL/SQL query statement below */
select Name "Customers" from Customers a
where not exists (select 1  from Orders b where a.Id = b.CustomerId)
order by 1


3d81d6453d0243f69d1555f93069ac17.png



相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4月前
|
SQL
面试必备杀技:SQL查询专项训练(二)
面试必备杀技:SQL查询专项训练
|
4月前
|
SQL
面试必备杀技:SQL查询专项训练(一)
面试必备杀技:SQL查询专项训练
|
4月前
|
SQL
面试必备杀技:SQL查询专项训练
面试必备杀技:SQL查询专项训练
leetcode剑指 Offer 专项突击版(051、008、016)
leetcode剑指 Offer 专项突击版(051、008、016)
103 0
leetcode剑指 Offer 专项突击版(23、047、028、036)
leetcode剑指 Offer 专项突击版(23、047、028、036)
|
SQL 测试技术
LeetCode SQL专项练习 (8) 计算函数
LeetCode SQL专项练习 (8) 计算函数
150 0
LeetCode SQL专项练习 (6) 合并&多表查询
LeetCode SQL专项练习 (6) 合并&多表查询
140 0
LeetCode SQL专项练习 (10) 过滤
LeetCode SQL专项练习 (10) 过滤
165 0
LeetCode SQL专项练习 (9) 控制流
LeetCode SQL专项练习 (9) 控制流
144 0
LeetCode SQL专项练习 (7) 计算函数&分组统计
LeetCode SQL专项练习 (7) 计算函数&分组统计
120 0