Leetcode中的SQL题目练习(一)

Leetcode中的SQL题目练习(一)

595. Big Countries

https://leetcode.com/problems/big-countries/description/

Description

namecontinentareapopulationgdp
AfghanistanAsia6522302550010020343000
AlbaniaEurope28748283174112960000
AlgeriaAfrica238174137100000188681000
AndorraEurope468781153712000
AngolaAfrica124670020609294100990000

A country is big if it has an area of bigger than 3 million square km or a population of more than 25 million.
Write a SQL solution to output big countries’ name, population and area.(查找面积超过 3,000,000 或者人口数超过 25,000,000 的国家。)
For example, according to the above table, we should output:

namepopulationarea
Afghanistan25500100652230
Algeria371000002381741

Solution

SELECT name,
    population,
    area
FROM
    World
WHERE
    area > 3000000
    OR population > 25000000;

627. Swap Salary

https://leetcode.com/problems/swap-salary/description/

Description

idnamesexsalary
1Am2500
2Bf1500
3Cm5500
4Df500

Given a table salary, such as the one below, that has m=male and f=female values. Swap all f and m values (i.e., change all f values to m and vice versa) with a single update query and no intermediate temp table.(只用一个 SQL 查询,将 sex 字段反转。)
After running your query, the above salary table should have the following rows:

idnamesexsalary
1Af2500
2Bm1500
3Cf5500
4Dm500

Solution:

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

620. Not Boring Movies

https://leetcode.com/problems/not-boring-movies/description/

Description

idmoviedescriptionrating
1Wargreat 3D8.9
2Sciencefiction8.5
3irishboring6.2
4Ice songFantacy8.6
5House cardInteresting9.1

查找 id 为奇数,并且 description 不是 boring 的电影,按 rating 降序。

idmoviedescriptionrating
5House cardInteresting9.1
1Wargreat 3D8.9

Solution:

SELECT
    *
FROM
    cinema
WHERE
    id % 2 = 1
    AND description != 'boring'
ORDER BY
    rating DESC;

596. Classes More Than 5 Students

Description

studentclass
AMath
BEnglish
CMath
DBiology
EMath
FComputer
GMath
HMath
IMath

查找有五名及以上 student 的 class。

class
Math

Solution:

SELECT
    class
FROM
    courses
GROUP BY
    class
HAVING
    count( DISTINCT student ) >= 5;

182. Duplicate Emails

https://leetcode.com/problems/duplicate-emails/description/

IdEmail
1a@b.com
2c@d.com
3a@b.com

查找重复的邮件地址:

Email
a@b.com

Solution:

select Email
from Person 
group by Email 
having count(Email)>=2

196. Delete Duplicate Emails ?

https://leetcode.com/problems/delete-duplicate-emails/description/

Description:

IdEmail
1john@example.com
2bob@example.com
3john@example.com

删除重复的邮件地址

IdEmail
1john@example.com
2bob@example.com

Solution:

(1)

delete p1
from Person p1,Person p2
where p1.Email =p2.Email  
and p1.Id > p2.Id            

(2)

DELETE
FROM
    Person
WHERE
    id NOT IN ( SELECT id FROM ( SELECT min( id ) AS id FROM Person GROUP BY email ) AS m );
posted @ 2018-12-24 16:39 流氓小伙子 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值