力扣leetcode刷题记录1----

【以下题目来源均来自力扣leetcode】

 

595. 大的国家

World 表:

【描述】name 是这张表的主键。这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。

【问题】如果一个国家满足下述两个条件之一,则认为该国是 大国 :面积至少为 300 万平方公里(即,3000000 km2),或者人口至少为 2500 万(即 25000000)

编写一个 SQL 查询以报告 大国 的国家名称、人口和面积。按 任意顺序 返回结果表。

select name,population,area
from World
where area>=3000000 or population>=25000000;

1757. 可回收且低脂的产品

表:Products。

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';

584. 寻找用户推荐人

【描述】给定表 customer ,里面保存了所有客户信息和他们的推荐人。

【问题】写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2。

select name
from customer
where id not in (select id from customer where referee_id=2);
-- 需要避开null

183. 从不订购的客户

某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。

select name as Customers
from Customers
where id not in (select CustomerId from Orders);

627. 变更性别

请你编写一个 SQL 查询来交换所有的 'f' 和 'm' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。

注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。

update salary
set sex=if(sex='f','m','f');

196. 删除重复的电子邮箱

编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件。

以 任意顺序 返回结果表。 (注意: 仅需要写删除语句,将自动对剩余结果进行查询)

DELETE p1
from person p1,
person p2
where p1.email=p2.email and p1.id>p2.id;

1667. 修复表中的名字

编写一个 SQL 查询来修复名字,使得只有第一个字符是大写的,其余都是小写的。

返回按 user_id 排序的结果表。

concat(upper(left(name,1)),lower(substring(name,2))) as name
from Users
order by user_id;

1527. 患某种疾病的患者

写一条 SQL 语句,查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1 。

按 任意顺序 返回结果表。

select patient_id,patient_name,conditions
from Patients
where conditions LIKE '% DIAB1%' or conditions LIKE 'DIAB1%';

 

posted @ 2022-09-03 22:58  徐若离  阅读(31)  评论(0编辑  收藏  举报