当前位置:网站首页>【力扣10天SQL入门】Day1
【力扣10天SQL入门】Day1
2022-06-21 18:04:00 【ly甲烷】
力扣学习计划地址:https://leetcode.cn/study-plan/sql/
1.大的国家
表:World
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| name | varchar |
| continent | varchar |
| area | int |
| population | int |
| gdp | int |
+-------------+---------+
name 是这张表的主键。
这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。
如果一个国家满足下述两个条件之一,则认为该国是大国 :
面积至少为 300 平方公里(即,3000000 km2),或者人口至少为 2500 万(即 25000000)
编写一个 SQL 查询以报告 大国 的国家名称、人口和面积。
答案:
SELECT name, population , area FROM World
WHERE population >= 25000000 OR area >= 3000000
2.可回收且低脂的产品
表: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'
3.寻找用户推荐人
表 customer ,里面保存了所有客户信息和他们的推荐人。
+------+------+-----------+
| id | name | referee_id|
+------+------+-----------+
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
+------+------+-----------+
需求
写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都不是2。
答案
mysql判断非空的函数
ISNULL(expr) 如果expr为null返回值1,否则返回值为0
IFNULL(expr1,expr2) 如果expr1值为null返回expr2的值,否则返回expr1的值
SELECT name FROM customer
WHERE IFNULL(referee_id, 0) != 2
4.从不订购的客户
某网站包含两个表
Customers 表:
+----+-------+
| Id | Name |
+----+-------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
+----+-------+
Orders 表:
+----+------------+
| Id | CustomerId |
+----+------------+
| 1 | 3 |
| 2 | 1 |
+----+------------+
需求
编写一个 SQL 查询,找出所有从不订购任何东西的客户。即Orders 中没有的 CustomerId 的 Customers表
+-----------+
| Customers |
+-----------+
| Henry |
| Max |
+-----------+
答案
SELECT name "Customers" FROM Customers
WHERE Id NOT IN(SELECT CustomerId FROM Orders)
总结
SELECT 列名 FROM 表 WHERE 条件
ISNULL(expr) -------------expr为 null 返回 1, 否则返回0
IFNULL(expr1, expr2) ------------ expr1为null 返回 expr2, 否则返回expr1
NOT IN --------列表不包含
边栏推荐
- 2022年6月25日PMP考试通关宝典-4
- 转发提醒 MetaMask小狐狸钱包安全公告 如何应对拓展程序潜在的私钥泄露
- 【面试高频题】难度 1.5/5,经典「前缀和 + 二分」运用题
- 品牌、产品和服务齐发力,东风日产接下来有什么新动作?
- 文献分析 Citespace 6.1.2 下载及安装教程
- R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用summary函数查看模型汇总统计信息并解读特征
- 6月22日直播 | 华南理工詹志辉: 面向昂贵优化的进化计算
- 挖财商学院属于证券公司吗?开户安全吗?
- 昇腾科研创新使能计划赋能开发者  华为计算提供三大维度支持
- 【面试高频题】难度 1/5,热门枚举类模拟题
猜你喜欢

Huawei launches new products again? These models have excellent functions

Nebula Graph入驻阿里云计算巢,助力企业打造云上超大规模图数据库

删除指定的screen

Niuke: merging two ordered arrays

数据库主键一定要自增吗?有哪些场景不建议自增?
![[Shangshui Shuo series] day one](/img/09/ab31cc494d726e896799d21fa02502.png)
[Shangshui Shuo series] day one

The main data products of China's two Fengyun meteorological "new stars" will be open and shared with global users

WWDC22 多媒体特性汇总

2022年下半年传统产品经理国际资格认证招生简章(NPDP)

W10添加系统环境变量Path
随机推荐
Is there a Hongmeng version of the song ordering system? Lao Wang was the first to experience it
ThreadLocal与线程池在使用中可能会出现的两个问题
Notes on writing questions in C language -- find s=a+aa+aaa+aaaa+aa Value of a
【一起上水硕系列】Day One
医疗费用清单秒速录入,OCR识别助力效率倍增
Mvcc implementation principle of MySQL
Fpga/cpld final examination paper for the first semester of Nanjing University of information technology 2020-2021
Huawei launches new products again? These models have excellent functions
R语言glm函数构建二分类logistic回归模型(family参数为binomial)、使用summary函数查看模型汇总统计信息并解读特征
Selection skills of national production reinforced Ethernet switch
文件上传漏洞靶场分析 UPLOAD_LABS
线上开期货户是否安全啊?不去线下可以开户吗?
How to create network redundancy for network managed national production reinforced switch
36 krypton launched | focusing on the innovation of health insurance products, and "Yingshi health" has obtained four rounds of financing
The R language catiols package divides the data, randomforest package constructs the random forest model, uses the importance function to calculate the importance of each feature in the random forest
11 Beautiful Soup 解析库的简介及安装
WWDC22 多媒体特性汇总
《Go题库·9》同一个协程里面,对无缓冲channel同时发送和接收数据有什么问题
298th weekly match
秒云云原生信创全兼容解决方案再升级,助力信创产业加速落地