> 文章列表 > 做题记录-力扣 LeetCode-学习计划-SQL-SQL 入门

做题记录-力扣 LeetCode-学习计划-SQL-SQL 入门

做题记录-力扣 LeetCode-学习计划-SQL-SQL 入门

第 1 天 选择

595. 大的国家

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

1757. 可回收且低脂的产品

select product_id
from Products
where low_fats='Y' and recyclable ='Y'

584. 寻找用户推荐人

= 或 != 只能判断基本数据类型 
is 关键字只能判断null 

select name
from customer
where referee_id!=2 or referee_id is null--------------------------select name
from customer
where referee_id<>2 or referee_id is null--------------------------#先把null转为0select name
from customer
where ifnull(referee_id,0)!=2;

183. 从不订购的客户

select customers.name as customers
from customers
where id not in (select customers.idfrom customers,orderswhere customers.id=orders.customerid)--------------------------select customers.name as 'Customers'
from customers
where customers.id not in
(select customerid from orders
)

第 2 天 排序 & 修改 

1873. 计算特殊奖金

select employee_id,salary as  bonus
from Employees
where (employee_id%2)!=0 and name not like "M%" 
union
select employee_id,salary=0 as  bonus
from Employees
where (employee_id%2)=0 or name like "M%" 
order by employee_id

627. 变更性别

Update 语句用于修改表中的数据。
UPDATE名称 SET 列名称 = 新值 WHERE 列名称 = 某值 

1

2

3

UPDATE table_name

SET column1=value1,column2=value2,...

WHERE column(1)=value(1),column(2)=value(2)...and column(n)=value(n);

update salary
set sex = 
(case sex when 'm' then 'f' else 'm' end
);

196. 删除重复的电子邮箱

DELETE 语句用于删除表中的行。
DELETE FROM 表名称 WHERE 列名称 = 值 

delete a
from Person a , Person b
where a.id > b.id and a.email = b.email