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

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
云数据库 RDS PostgreSQL,高可用系列 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



相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。 &nbsp; 相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情:&nbsp;https://www.aliyun.com/product/rds/mysql&nbsp;
目录
相关文章
面试必备杀技:SQL查询专项训练(二)
面试必备杀技:SQL查询专项训练
132 0
面试必备杀技:SQL查询专项训练(一)
面试必备杀技:SQL查询专项训练
183 0
面试必备杀技:SQL查询专项训练
面试必备杀技:SQL查询专项训练
117 0
leetcode剑指 Offer 专项突击版(051、008、016)
leetcode剑指 Offer 专项突击版(051、008、016)
208 0
leetcode剑指 Offer 专项突击版(23、047、028、036)
leetcode剑指 Offer 专项突击版(23、047、028、036)
147 0
|
SQL 测试技术
LeetCode SQL专项练习 (8) 计算函数
LeetCode SQL专项练习 (8) 计算函数
207 0
LeetCode SQL专项练习 (6) 合并&多表查询
LeetCode SQL专项练习 (6) 合并&多表查询
207 0
LeetCode SQL专项练习 (10) 过滤
LeetCode SQL专项练习 (10) 过滤
214 0
LeetCode SQL专项练习 (9) 控制流
LeetCode SQL专项练习 (9) 控制流
217 0
LeetCode SQL专项练习 (7) 计算函数&分组统计
LeetCode SQL专项练习 (7) 计算函数&分组统计
183 0

热门文章

最新文章