当前位置:网站首页>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进行比对。
边栏推荐
猜你喜欢

网关Gateway的介绍与使用

Horizontal and vertical centering method and compatibility

Performance comparison of tidb for PostgreSQL and yugabytedb on sysbench

Spark Tuning (III): persistence reduces secondary queries

The difference and working principle between compiler and interpreter

Personal notes of graphics (4)

spark调优(三):持久化减少二次查询

记录Servlet学习时的一次乱码

整理几个重要的Android知识,高级Android开发面试题

AutoLISP series (3): function function 3
随机推荐
整理几个重要的Android知识,高级Android开发面试题
【医学分割】attention-unet
【PHP】PHP接口继承及接口多继承原理与实现方法
Record the migration process of a project
Iptables only allows the specified IP address to access the specified port
SqlServer2014+: 创建表的同时创建索引
os、sys、random标准库主要功能
AutoLISP series (2): function function 2
[PHP] PHP interface inheritance and interface multi inheritance principle and implementation method
three. JS create cool snow effect
Logback logging framework third-party jar package is available for free
字节跳动Android面试,知识点总结+面试题解析
AutoLISP series (3): function function 3
Vs2019 configuration matrix library eigen
URL和URI的关系
laravel post提交数据时显示异常
Statistical learning method -- perceptron
Xcode Revoke certificate
three.js打造酷炫下雪效果
删除 console 语句引发的惨案