【Oracle】力扣简单的练习题

Oracle力扣简单的练习题

请你编写一个 SQL 查询来交换所有的 'f' 和 'm'

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

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

用decode很快就出来
update Salary set sex = decode(sex,'f','m','f')

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

/*
 Please write a DELETE statement and DO NOT write a SELECT statement.
 Write your PL/SQL query statement below
 */
 /*******
 编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件。

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

用分析函数排名一下然后删除对应的id

delete from Person where id in 
(select t.id from (select id, 
dense_rank() over(partition by email order by id) tt
from Person) t where t.tt != 1)
posted @ 2022-08-04 21:39  DbWong_0918  阅读(116)  评论(0)    收藏  举报