力扣-SQL入门

Posted 辉小歌

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了力扣-SQL入门相关的知识,希望对你有一定的参考价值。

https://leetcode.cn/study-plan/sql/?progress=xhqm4sjh

目录

选择

595. 大的国家

# Write your mysql query statement below
select name,population,area
from World
where area>=3000000 or population>=25000000;
# Write your MySQL query statement below
select name,population,area
from World
where area>=3000000
union 
select name,population,area
from World
where population>=25000000;

1757. 可回收且低脂的产品

# Write your MySQL query statement below
select product_id
from Products
where low_fats='Y' and recyclable='Y';

584. 寻找用户推荐人


# Write your MySQL query statement below
select name
from customer
where referee_id<>2 or referee_id is NULL;

183. 从不订购的客户

# Write your MySQL query statement below
select Name Customers
from Customers
where Customers.Id not in(
    select  CustomerId
    from Orders
);

排序 & 修改

1873. 计算特殊奖金


SQL中IF函数的使用:
IF(expr1,expr2,expr3)

  • expr1 的值为 TRUE,则返回值为 expr2
  • expr1 的值为FALSE,则返回值为 expr3
# Write your MySQL query statement below
select employee_id,if(employee_id%2=1 and name not like 'M%',salary,0) bonus
from Employees
order by employee_id;        

627. 变更性别


case when的使用方法:

case 列名

    when   条件值1   then  选项1
    when   条件值2    then  选项2.......
    else     默认值      end
# Write your MySQL query statement below
update Salary
set sex=
    case sex
    when 'f' then 'm'
    else 'f' end
;
# Write your MySQL query statement below
update Salary
set sex=
    case when sex='f'then 'm'
    else 'f' end
;

196. 删除重复的电子邮箱

# Please write a DELETE statement and DO NOT write a SELECT statement.
# Write your MySQL query statement below
delete p1
from Person p1,Person p2
where p1.email=p2.email and p1.id>p2.id;

以上是关于力扣-SQL入门的主要内容,如果未能解决你的问题,请参考以下文章

数据库入门:sql与MySQL的区别

力扣570(MySQL)-至少有5名直接下属的经理(简单)

Mysql从入门到入魔——9. 游标高级SQL特性

第0天SQL快速入门-了解MySQL存储引擎(SQL 小虚竹)

第0天SQL快速入门-了解MYSQL前世今生,为了更好探索未来(SQL 小虚竹)

第0天SQL快速入门-了解MYSQL前世今生,为了更好探索未来(SQL 小虚竹)