1,连接查询
175,left join:组合两个表
表: Person +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | PersonId | int | | FirstName | varchar | | LastName | varchar | +-------------+---------+ personId 是该表的主键列。 该表包含一些人的 ID 和他们的姓和名的信息。 表: Address +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | AddressId | int | | PersonId | int | | City | varchar | | State | varchar | +-------------+---------+ addressId 是该表的主键列。 该表的每一行都包含一个 ID = PersonId 的人的城市和州的信息。
题目:编写一个SQL查询来报告
Person
表中每个人的姓、名、城市和州。如果personId
的地址不在Address
表中,则报告为空null
。select a.firstName,a.lastName,b.city,b.state from person a left join address b on a.personId = b.personId
607,left join:销售员
表: SalesPerson +-----------------+---------+ | Column Name | Type | +-----------------+---------+ | sales_id | int | | name | varchar | | salary | int | | commission_rate | int | | hire_date | date | +-----------------+---------+ sales_id 是该表的主键列。 该表的每一行都显示了销售人员的姓名和 ID ,以及他们的工资、佣金率和雇佣日期。 表: Company +-------------+---------+ | Column Name | Type | +-------------+---------+ | com_id | int | | name | varchar | | city | varchar | +-------------+---------+ com_id 是该表的主键列。 该表的每一行都表示公司的名称和 ID ,以及公司所在的城市。 表: Orders +-------------+------+ | Column Name | Type | +-------------+------+ | order_id | int | | order_date | date | | com_id | int | | sales_id | int | | amount | int | +-------------+------+ order_id 是该表的主键列。 com_id 是 Company 表中 com_id 的外键。 sales_id 是来自销售员表 sales_id 的外键。 该表的每一行包含一个订单的信息。这包括公司的 ID 、销售人员的 ID 、订单日期和支付的金额。
题目:编写一个SQL查询,报告没有任何与名为 “RED” 的公司相关的订单的所有销售人员的姓名。以 任意顺序 返回结果表。
select s.name from salesperson s where s.name in (select s.name from salesperson s left join orders o on o.sales_id = s.sales_id left join company c on o.com_id = c.com_id where c.name is null) or s.name not in (select s.name from salesperson s left join orders o on o.sales_id = s.sales_id left join company c on o.com_id = c.com_id where c.name = 'RED')
1148,left join:文章浏览I
Views 表: +---------------+---------+ | Column Name | Type | +---------------+---------+ | article_id | int | | author_id | int | | viewer_id | int | | view_date | date | +---------------+---------+ 此表无主键,因此可能会存在重复行。 此表的每一行都表示某人在某天浏览了某位作者的某篇文章。 请注意,同一人的 author_id 和 viewer_id 是相同的。
题目:请编写一条 SQL 查询以找出所有浏览过自己文章的作者,结果按照 id 升序排列。
select distinct a.author_id as id from views a left join views b on a.article_id = b.article_id where a.author_id = b.viewer_id order by a.author_id
182,级联:查找重复的电子邮箱
编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。 +----+---------+ | Id | Email | +----+---------+ | 1 | a@b.com | | 2 | c@d.com | | 3 | a@b.com | +----+---------+
题目:编写一个 SQL 查询,查找
Person
表中所有重复的电子邮箱。select distinct a.email from person a, person b where a.email = b.email and a.id <> b.id
2,比较运算符
176,第二高的薪水
Employee 表: +-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ id 是这个表的主键。 表的每一行包含员工的工资信息。
题目:编写一个 SQL 查询,获取并返回
Employee
表中第二高的薪水 。如果不存在第二高的薪水,查询应该返回null
。select distinct salary from Employee order by salary desc limit 1,1 就可以,但是输不出null,所以外面再加一层 select (select distinct salary from Employee order by salary desc limit 1,1) as SecondHighestSalary
177,第N高的薪水
表: Employee +-------------+------+ | Column Name | Type | +-------------+------+ | id | int | | salary | int | +-------------+------+ Id是该表的主键列。 该表的每一行都包含有关员工工资的信息。
题目:编写一个SQL查询来报告
Employee
表中第n
高的工资。如果没有第n
个最高工资,查询应该报告为null
。CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT BEGIN declare m INT; SET m = N-1; RETURN ( # Write your MySQL query statement below. select (select distinct salary from Employee order by salary desc limit m,1) as getNthHighestSalary ); END
586,订单最多的客户
表: Orders +-----------------+----------+ | Column Name | Type | +-----------------+----------+ | order_number | int | | customer_number | int | +-----------------+----------+ Order_number是该表的主键。 此表包含关于订单ID和客户ID的信息。
题目:编写一个SQL查询,为下了 最多订单 的客户查找
customer_number
。select customer_number from orders group by customer_number order by count(order_number) desc limit 0,1
183,从不订购的客户
某网站包含两个表,Customers 表和 Orders 表。 Customers 表: +----+-------+ | Id | Name | +----+-------+ | 1 | Joe | | 2 | Henry | | 3 | Sam | | 4 | Max | +----+-------+ Orders 表: +----+------------+ | Id | CustomerId | +----+------------+ | 1 | 3 | | 2 | 1 | +----+------------+ 例如给定上述表格,你的查询应返回: +-----------+ | Customers | +-----------+ | Henry | | Max | +-----------+
题目:编写一个 SQL 查询,找出所有从不订购任何东西的客户。
select name as Customers from Customers where id not in (select CustomerId from orders)
584,寻找用户推荐人
给定表 customer ,里面保存了所有客户信息和他们的推荐人。 +------+------+-----------+ | id | name | referee_id| +------+------+-----------+ | 1 | Will | NULL | | 2 | Jane | NULL | | 3 | Alex | 2 | | 4 | Bill | NULL | | 5 | Zack | 1 | | 6 | Mark | 2 | +------+------+-----------+
题目:写一个查询语句,返回一个客户列表,列表中客户的推荐人的编号都 不是 2。
select name from customer where not referee_id = 2 or referee_id is null
595,大的国家
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
1757,可回收且低脂的产品
表: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' and recyclable='Y'
3,集合运算
511,游戏玩法分析I
活动表 Activity: +--------------+---------+ | Column Name | Type | +--------------+---------+ | player_id | int | | device_id | int | | event_date | date | | games_played | int | +--------------+---------+ 表的主键是 (player_id, event_date)。 这张表展示了一些游戏玩家在游戏平台上的行为活动。 每行数据记录了一名玩家在退出平台之前,当天使用同一台设备登录平台后打开的游戏的数目(可能是 0 个)。
题目:写一条 SQL 查询语句获取每位玩家 第一次登陆平台的日期。
select player_id,min(event_date)as "first_login" from activity group by player_id
1050,合作过至少三次的演员和导演
ActorDirector 表: +-------------+---------+ | Column Name | Type | +-------------+---------+ | actor_id | int | | director_id | int | | timestamp | int | +-------------+---------+ timestamp 是这张表的主键.
题目:写一条SQL查询语句获取合作过至少三次的演员和导演的 id 对
(actor_id, director_id)
select actor_id,director_id from actordirector a group by actor_id,director_id having count(actor_id) >3 or count(actor_id) =3
1084,销售分析III
Table: Product +--------------+---------+ | Column Name | Type | +--------------+---------+ | product_id | int | | product_name | varchar | | unit_price | int | +--------------+---------+ Product_id是该表的主键。 该表的每一行显示每个产品的名称和价格。 Table: Sales +-------------+---------+ | Column Name | Type | +-------------+---------+ | seller_id | int | | product_id | int | | buyer_id | int | | sale_date | date | | quantity | int | | price | int | +------ ------+---------+ 这个表没有主键,它可以有重复的行。 product_id 是 Product 表的外键。 该表的每一行包含关于一个销售的一些信息。
题目:编写一个SQL查询,报告
2019年春季
才售出的产品。即仅在2019-01-01
至2019-03-31
(含)之间出售的商品。select distinct a.product_id, a.product_name from product a left join sales b on a.product_id = b.product_id where b.product_id is not null and a.product_id not in( select product_id from sales where sale_date < '2019-01-01' or sale_date > '2019-03-31' )
1141,查询近30天活跃用户数
动记录表:Activity +---------------+---------+ | Column Name | Type | +---------------+---------+ | user_id | int | | session_id | int | | activity_date | date | | activity_type | enum | +---------------+---------+ 该表是用户在社交网站的活动记录。 该表没有主键,可能包含重复数据。 activity_type 字段为以下四种值 ('open_session', 'end_session', 'scroll_down', 'send_message')。 每个 session_id 只属于一个用户。
题目:请写SQL查询出截至 2019-07-27(包含2019-07-27),近 30 天的每日活跃用户数(当天只要有一条活动记录,即为活跃用户)。以 任意顺序 返回结果表。
select activity_date'day',count(distinct user_id)'active_users' from activity where activity_date between '2019-06-28' and '2019-07-27' group by activity_date
1158,市场分析I
Table: Users +----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | join_date | date | | favorite_brand | varchar | +----------------+---------+ 此表主键是 user_id。 表中描述了购物网站的用户信息,用户可以在此网站上进行商品买卖。 Table: Orders +---------------+---------+ | Column Name | Type | +---------------+---------+ | order_id | int | | order_date | date | | item_id | int | | buyer_id | int | | seller_id | int | +---------------+---------+ 此表主键是 order_id。 外键是 item_id 和(buyer_id,seller_id)。 Table: Items +---------------+---------+ | Column Name | Type | +---------------+---------+ | item_id | int | | item_brand | varchar | +---------------+---------+ 此表主键是 item_id。
题目:请写出一条SQL语句以查询每个用户的注册日期和在
2019
年作为买家的订单总数。以 任意顺序 返回结果表。select u.user_id as buyer_id, join_date,count(o.order_id) as "orders_in_2019" from users u left join orders o on u.user_id = o.buyer_id where o.order_date between "2019-01-01" and "2020-01-01" group by u.user_id union select u.user_id as buyer_id, join_date, 0 as "orders_in_2019" from users u where u.user_id not in (select buyer_id from orders where order_date between "2019-01-01" and "2020-01-01")
1393,股票的资本损益
Stocks 表: +---------------+---------+ | Column Name | Type | +---------------+---------+ | stock_name | varchar | | operation | enum | | operation_day | int | | price | int | +---------------+---------+ (stock_name, day) 是这张表的主键 operation 列使用的是一种枚举类型,包括:('Sell','Buy') 此表的每一行代表了名为 stock_name 的某支股票在 operation_day 这一天的操作价格。 保证股票的每次'Sell'操作前,都有相应的'Buy'操作。
题目:编写一个SQL查询来报告每支股票的资本损益。股票的资本损益是一次或多次买卖票后的全部收益或损失。以任意顺序返回结果即可。
select stock_name, sum(case operation when 'Buy' then -price when 'Sell' then price end) as capital_gain_loss from Stocks group by stock_name
1407,排名靠前的旅行者
表:Users +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | name | varchar | +---------------+---------+ id 是该表单主键。 name 是用户名字。 表:Rides +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | user_id | int | | distance | int | +---------------+---------+ id 是该表单主键。 user_id 是本次行程的用户的 id, 而该用户此次行程距离为 distance 。
题目:写一段 SQL , 报告每个用户的旅行距离。返回的结果表单,以 travelled_distance 降序排列 ,如果有两个或者更多的用户旅行了相同的距离, 那么再以 name 升序排列 。
select name,IFNULL(sum(distance), 0) as travelled_distance from users left join rides on users.id = rides.user_id group by rides.user_id order by sum(distance) desc,name
1484,按日期分组销售产品
表 Activities: +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | sell_date | date | | product | varchar | +-------------+---------+ 此表没有主键,它可能包含重复项。 此表的每一行都包含产品名称和在市场上销售的日期
题目:编写一个 SQL 查询来查找每个日期、销售的不同产品的数量及其名称。每个日期的销售产品名称应按词典序排列。返回按
sell_date
排序的结果表。select sell_date, # 获取“不同的”产品数【count(distinct product)】 count(distinct product) as num_sold, # “不同的”【distinct product】产品按照字典排序【order by product】 & “,”分割【separator ','】 group_concat(distinct product order by product separator ',') as products from Activities group by sell_date order by sell_date;
+------------+----------+------------------------------+ | sell_date | num_sold | products | +------------+----------+------------------------------+ | 2020-05-30 | 3 | Basketball,Headphone,T-shirt | | 2020-06-01 | 2 | Bible,Pencil | | 2020-06-02 | 1 | Mask | +------------+----------+------------------------------+
1581,进店却未进行过交易的顾客
表:Visits +-------------+---------+ | Column Name | Type | +-------------+---------+ | visit_id | int | | customer_id | int | +-------------+---------+ visit_id 是该表的主键。 该表包含有关光临过购物中心的顾客的信息。 表:Transactions +----------------+---------+ | Column Name | Type | +----------------+---------+ | transaction_id | int | | visit_id | int | | amount | int | +----------------+---------+ transaction_id 是此表的主键。 此表包含 visit_id 期间进行的交易的信息。
题目:有一些顾客可能光顾了购物中心但没有进行交易。请你编写一个 SQL 查询,来查找这些顾客的 ID ,以及他们只光顾不交易的次数。返回以 任何顺序 排序的结果表。
select a.customer_id,count(a.customer_id) as count_no_trans from visits a left join transactions b on a.visit_id = b.visit_id where b.transaction_id is null group by a.customer_id
1587,银行账户概要II
表: Users +--------------+---------+ | Column Name | Type | +--------------+---------+ | account | int | | name | varchar | +--------------+---------+ account 是该表的主键. 表中的每一行包含银行里中每一个用户的账号. 表: Transactions +---------------+---------+ | Column Name | Type | +---------------+---------+ | trans_id | int | | account | int | | amount | int | | transacted_on | date | +---------------+---------+ trans_id 是该表主键. 该表的每一行包含了所有账户的交易改变情况. 如果用户收到了钱, 那么金额是正的; 如果用户转了钱, 那么金额是负的. 所有账户的起始余额为 0.
题目:写一个 SQL, 报告余额高于 10000 的所有用户的名字和余额. 账户的余额等于包含该账户的所有交易的总和。
select name, SUM(amount) as balance from Users a left join Transactions b on a.account = b.account group by b.account having SUM(amount) > 10000
1693,每天的领导和合伙人
表:DailySales +-------------+---------+ | Column Name | Type | +-------------+---------+ | date_id | date | | make_name | varchar | | lead_id | int | | partner_id | int | +-------------+---------+ 该表没有主键。 该表包含日期、产品的名称,以及售给的领导和合伙人的编号。名称只包含小写英文字母。
题目:写一条 SQL 语句,使得对于每一个 date_id 和 make_name,返回不同的 lead_id 以及不同的 partner_id 的数量。按 任意顺序 返回结果表。
select date_id, make_name,count(distinct lead_id) as unique_leads,count(distinct partner_id) as unique_partners from dailysales group by date_id, make_name
1729,求关注着的数量
表: Followers +-------------+------+ | Column Name | Type | +-------------+------+ | user_id | int | | follower_id | int | +-------------+------+ (user_id, follower_id) 是这个表的主键。 该表包含一个关注关系中关注者和用户的编号,其中关注者关注用户。
题目:写出 SQL 语句,对于每一个用户,返回该用户的关注者数量。按
user_id
的顺序返回结果表。select user_id, count(follower_id) as followers_count from followers group by user_id order by user_id
1890,2020年最后一次登录
表: Logins +----------------+----------+ | 列名 | 类型 | +----------------+----------+ | user_id | int | | time_stamp | datetime | +----------------+----------+ (user_id, time_stamp) 是这个表的主键。 每一行包含的信息是user_id 这个用户的登录时间。
题目:编写一个 SQL 查询,该查询可以获取在
2020
年登录过的所有用户的本年度 最后一次 登录时间。结果集 不 包含2020
年没有登录过的用户。select user_id, max(time_stamp) as last_stamp from logins where time_stamp < "2021-01-01 00:00:00" and time_stamp > "2020-01-01 00:00:00" group by user_id
608,树节点
给定一个表 tree,id 是树节点的编号, p_id 是它父节点的 id 。 +----+------+ | id | p_id | +----+------+ | 1 | null | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 2 | +----+------+
树中每个节点属于以下三种类型之一:
- 叶子:如果这个节点没有任何孩子节点。
- 根:如果这个节点是整棵树的根,即没有父节点。
- 内部节点:如果这个节点既不是叶子节点也不是根节点。
题目:写一个查询语句,输出所有节点的编号和节点的类型,并将结果按照节点编号排序。上面样例的结果为:
+----+------+ | id | Type | +----+------+ | 1 | Root | | 2 | Inner| | 3 | Leaf | | 4 | Leaf | | 5 | Leaf | +----+------+
select id,'Root' as Type from tree where p_id is null union select a.id,'Leaf' as Type from tree a where a.id not in (select distinct b.p_id from tree b where b.p_id is not null) and a.p_id is not null union select id,'Inner' as Type from tree where id in (select p_id from tree) and p_id is not null order by id
1795,每个产品在不同商店的价格
表:Products +-------------+---------+ | Column Name | Type | +-------------+---------+ | product_id | int | | store1 | int | | store2 | int | | store3 | int | +-------------+---------+ 这张表的主键是product_id(产品Id)。 每行存储了这一产品在不同商店store1, store2, store3的价格。 如果这一产品在商店里没有出售,则值将为null。
题目:请你重构 Products 表,查询每个产品在不同商店的价格,使得输出的格式变为(product_id, store, price) 。如果这一产品在商店里没有出售,则不输出这一行。输出结果表中的 顺序不作要求 。
select product_id,'store1' as store,store1 as price from products where store1 is not null union select product_id,'store2' as store,store2 as price from products where store2 is not null union select product_id,'store3' as store,store3 as price from products where store3 is not null
1965,丢失信息的演员
表: Employees +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | name | varchar | +-------------+---------+ employee_id 是这个表的主键。 每一行表示雇员的id 和他的姓名。 表: Salaries +-------------+---------+ | Column Name | Type | +-------------+---------+ | employee_id | int | | salary | int | +-------------+---------+ employee_id is 这个表的主键。 每一行表示雇员的id 和他的薪水。
题目:写出一个查询语句,找到所有 丢失信息 的雇员id。当满足下面一个条件时,就被认为是雇员的信息丢失:
- 雇员的 姓名 丢失了
- 雇员的 薪水信息 丢失了
返回这些雇员的id employee_id , 从小到大排序 。
select e.employee_id from employees e where e.employee_id not in (select s.employee_id from salaries s) union select s.employee_id from salaries s where s.employee_id not in (select e.employee_id from employees e) order by employee_id
4,关键字
178,分数排名
表: Scores +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | score | decimal | +-------------+---------+ Id是该表的主键。 该表的每一行都包含了一场比赛的分数。Score是一个有两位小数点的浮点值。
编写 SQL 查询对分数进行排序。排名按以下规则计算:
- 分数应按从高到低排列。
- 如果两个分数相等,那么两个分数的排名应该相同。
- 在排名相同的分数后,排名数应该是下一个连续的整数。换句话说,排名之间不应该有空缺的数字。
按
score
降序返回结果表。select score,dense_rank() over(order by score desc) `rank` from scores
197,date_sub:上升温度
表: Weather +---------------+---------+ | Column Name | Type | +---------------+---------+ | id | int | | recordDate | date | | temperature | int | +---------------+---------+ id 是这个表的主键 该表包含特定日期的温度信息
题目:编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的
id
。返回结果 不要求顺序。select a.id from weather a left join weather b on b.recordDate=date_sub(a.recordDate,interval 1 day) where a.temperature > b.temperature and a.recordDate is not null and b.recordDate is not null
1527,like:患某种疾病的患者
患者信息表: Patients +--------------+---------+ | Column Name | Type | +--------------+---------+ | patient_id | int | | patient_name | varchar | | conditions | varchar | +--------------+---------+ patient_id (患者 ID)是该表的主键。 'conditions' (疾病)包含 0 个或以上的疾病代码,以空格分隔。 这个表包含医院中患者的信息。
题目:写一条 SQL 语句,查询患有 I 类糖尿病的患者 ID (patient_id)、患者姓名(patient_name)以及其患有的所有疾病代码(conditions)。I 类糖尿病的代码总是包含前缀 DIAB1 。
select * from Patients where conditions like 'DIAB1%' or conditions like '% DIAB1%'
1667,upper:修复表中的名字(首字母大写)
表: Users +----------------+---------+ | Column Name | Type | +----------------+---------+ | user_id | int | | name | varchar | +----------------+---------+ user_id 是该表的主键。 该表包含用户的 ID 和名字。名字仅由小写和大写字符组成。
题目:编写一个 SQL 查询来修复名字,使得只有第一个字符是大写的,其余都是小写的。
返回按
user_id
排序的结果表。select user_id, concat(upper(SUBSTRING(name,1,1)), lower(SUBSTRING(name,2,length(name)))) as name from users order by user_id
1741,sum:查找每个员工花费的总时间
表: Employees +-------------+------+ | Column Name | Type | +-------------+------+ | emp_id | int | | event_day | date | | in_time | int | | out_time | int | +-------------+------+ (emp_id, event_day, in_time) 是这个表的主键。 该表显示了员工在办公室的出入情况。 event_day 是此事件发生的日期,in_time 是员工进入办公室的时间,而 out_time 是他们离开办公室的时间。 in_time 和 out_time 的取值在1到1440之间。 题目保证同一天没有两个事件在时间上是相交的,并且保证 in_time 小于 out_time。
题目:编写一个SQL查询以计算每位员工每天在办公室花费的总时间(以分钟为单位)。 请注意,在一天之内,同一员工是可以多次进入和离开办公室的。 在办公室里一次进出所花费的时间为out_time 减去 in_time。
select event_day as day, emp_id, sum(out_time-in_time) as total_time from employees group by event_day, emp_id
1873,case:计算特殊奖金
表: Employees +-------------+---------+ | 列名 | 类型 | +-------------+---------+ | employee_id | int | | name | varchar | | salary | int | +-------------+---------+ employee_id 是这个表的主键。 此表的每一行给出了雇员id ,名字和薪水。
题目:写出一个SQL 查询语句,计算每个雇员的奖金。如果一个雇员的id是奇数并且他的名字不是以'M'开头,那么他的奖金是他工资的100%,否则奖金为0。返回的结果集请按照
employee_id
排序。select employee_id , case when employee_id % 2 = 1 and name not like 'M%' then salary else salary*0 end as bonus from Employees order by employee_id
4,Update&Delete
196,级联:删除重复的电子邮箱
表: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | email | varchar | +-------------+---------+ id是该表的主键列。 该表的每一行包含一封电子邮件。电子邮件将不包含大写字母。
题目:编写一个 SQL 删除语句来 删除 所有重复的电子邮件,只保留一个id最小的唯一电子邮件。以 任意顺序 返回结果表。
delete u from Person u , Person v where v.id < u.id and u.email = v.email
627,case:变更性别
Salary 表: +-------------+----------+ | Column Name | Type | +-------------+----------+ | id | int | | name | varchar | | sex | ENUM | | salary | int | +-------------+----------+ id 是这个表的主键。 sex 这一列的值是 ENUM 类型,只能从 ('m', 'f') 中取。 本表包含公司雇员的信息。
题目:请你编写一个 SQL 查询来交换所有的 'f' 和 'm' (即,将所有 'f' 变为 'm' ,反之亦然),仅使用 单个 update 语句 ,且不产生中间临时表。注意,你必须仅使用一条 update 语句,且 不能 使用 select 语句。
update salary a set a.sex = case when a.sex = 'f' then 'm' when a.sex = 'm' then 'f' end