当前位置:网站首页>LeetCode-SQL第一天
LeetCode-SQL第一天
2022-07-07 14:49:00 【liyatjj】
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 area >=3000000 or population >=25000000
2.可回收且低脂的产品
SQL架构
表: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' && recyclable='Y'
3.寻找用户推荐人
SQL架构
给定表 customer ,里面保存了所有客户信息和他们的推荐人。
±-----±-----±----------+
| id | name | referee_id|
±-----±-----±----------+
| 1 | Will | NULL |
| 2 | Jane | NULL |
| 3 | Alex | 2 |
| 4 | Bill | NULL |
| 5 | Zack | 1 |
| 6 | Mark | 2 |
±-----±-----±----------+
写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2。
来源:LeetCode
SELECT name
FROM customer
WHERE referee_id != 2 or referee_id is NULL
这个需要注意为空时的情况。
4.从不订购的客户
SQL架构
某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。
Customers 表:
±—±------+
| Id | Name |
±—±------+
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
±—±------+
Orders 表:
±—±-----------+
| Id | CustomerId |
±—±-----------+
| 1 | 3 |
| 2 | 1 |
±—±-----------+
来源:LeetCode
SELECT Name Customers
FROM Customers
WHERE Id not in
(
SELECT CustomerId FROM Orders
);
这个就需要注意两个表之间的关系,是Customers的Id和Orders的CustomerId进行比对。
边栏推荐
- three.js打造酷炫下雪效果
- 如何快速检查钢网开口面积比是否符合 IPC7525
- The difference and working principle between compiler and interpreter
- Inner monologue of accidental promotion
- 字节跳动Android面试,知识点总结+面试题解析
- Spark Tuning (III): persistence reduces secondary queries
- SqlServer2014+: 创建表的同时创建索引
- [Android -- data storage] use SQLite to store data
- [designmode] flyweight pattern
- 【HCSD大咖直播】亲授大厂面试秘诀-简要笔记
猜你喜欢
随机推荐
谎牛计数(春季每日一题 53)
Laravel 中config的用法
Personal notes of graphics (2)
【Android -- 数据存储】使用 SQLite 存储数据
MySQL中, 如何查询某一天, 某一月, 某一年的数据
How does laravel run composer dump autoload without emptying the classmap mapping relationship?
低代码(lowcode)帮助运输公司增强供应链管理的4种方式
logback. XML configure logs of different levels and set color output
预测——灰色预测
Pycharm terminal enables virtual environment
[designmode] template method pattern
Introduction and use of gateway
偶然升职的内心独白
[hcsd celebrity live broadcast] teach the interview tips of big companies in person - brief notes
How can laravel get the public path
prometheus api删除某个指定job的所有数据
URL和URI的关系
修改配置文件后tidb无法启动
LocalStorage和SessionStorage
SqlServer2014+: 创建表的同时创建索引