diff --git a/codes/leetcode/README.md b/codes/leetcode/README.md new file mode 100644 index 0000000..7a57fd5 --- /dev/null +++ b/codes/leetcode/README.md @@ -0,0 +1,3 @@ +# Leetcode 数据库篇题解 + +> [题库地址](https://leetcode-cn.com/problemset/database/) diff --git a/codes/leetcode/big-countries.sql b/codes/leetcode/big-countries.sql new file mode 100644 index 0000000..530301e --- /dev/null +++ b/codes/leetcode/big-countries.sql @@ -0,0 +1,27 @@ +-- 这里有张 World 表 +-- +-- +-----------------+------------+------------+--------------+---------------+ +-- | name | continent | area | population | gdp | +-- +-----------------+------------+------------+--------------+---------------+ +-- | Afghanistan | Asia | 652230 | 25500100 | 20343000 | +-- | Albania | Europe | 28748 | 2831741 | 12960000 | +-- | Algeria | Africa | 2381741 | 37100000 | 188681000 | +-- | Andorra | Europe | 468 | 78115 | 3712000 | +-- | Angola | Africa | 1246700 | 20609294 | 100990000 | +-- +-----------------+------------+------------+--------------+---------------+ +-- 如果一个国家的面积超过300万平方公里,或者人口超过2500万,那么这个国家就是大国家。 +-- +-- 编写一个SQL查询,输出表中所有大国家的名称、人口和面积。 +-- +-- 例如,根据上表,我们应该输出: +-- +-- +--------------+-------------+--------------+ +-- | name | population | area | +-- +--------------+-------------+--------------+ +-- | Afghanistan | 25500100 | 652230 | +-- | Algeria | 37100000 | 2381741 | +-- +--------------+-------------+--------------+ + +SELECT name, population, area FROM World +WHERE area > 3000000 OR population > 25000000; + diff --git a/codes/leetcode/combine-two-tables.sql b/codes/leetcode/combine-two-tables.sql new file mode 100644 index 0000000..e69de29 diff --git a/codes/leetcode/duplicate-emails.sql b/codes/leetcode/duplicate-emails.sql new file mode 100644 index 0000000..d9a9825 --- /dev/null +++ b/codes/leetcode/duplicate-emails.sql @@ -0,0 +1,21 @@ +-- 编写一个 SQL 查询,查找 Person 表中所有重复的电子邮箱。 +-- +-- 示例: +-- +-- +----+---------+ +-- | Id | Email | +-- +----+---------+ +-- | 1 | a@b.com | +-- | 2 | c@d.com | +-- | 3 | a@b.com | +-- +----+---------+ +-- 根据以上输入,你的查询应返回以下结果: +-- +-- +---------+ +-- | Email | +-- +---------+ +-- | a@b.com | +-- +---------+ +-- 说明:所有电子邮箱都是小写字母。 + +SELECT Email FROM Person GROUP BY Email HAVING COUNT(*) > 1; diff --git a/codes/leetcode/not-boring-movies.sql b/codes/leetcode/not-boring-movies.sql new file mode 100644 index 0000000..869ab3f --- /dev/null +++ b/codes/leetcode/not-boring-movies.sql @@ -0,0 +1,29 @@ +-- 某城市开了一家新的电影院,吸引了很多人过来看电影。该电影院特别注意用户体验,专门有个 LED显示板做电影推荐,上面公布着影评和相关电影描述。 +-- +-- 作为该电影院的信息部主管,您需要编写一个 SQL查询,找出所有影片描述为非 boring (不无聊) 的并且 id 为奇数 的影片,结果请按等级 rating 排列。 +-- +-- +-- +-- 例如,下表 cinema: +-- +-- +---------+-----------+--------------+-----------+ +-- | id | movie | description | rating | +-- +---------+-----------+--------------+-----------+ +-- | 1 | War | great 3D | 8.9 | +-- | 2 | Science | fiction | 8.5 | +-- | 3 | irish | boring | 6.2 | +-- | 4 | Ice song | Fantacy | 8.6 | +-- | 5 | House card| Interesting| 9.1 | +-- +---------+-----------+--------------+-----------+ +-- 对于上面的例子,则正确的输出是为: +-- +-- +---------+-----------+--------------+-----------+ +-- | id | movie | description | rating | +-- +---------+-----------+--------------+-----------+ +-- | 5 | House card| Interesting| 9.1 | +-- | 1 | War | great 3D | 8.9 | +-- +---------+-----------+--------------+-----------+ + +SELECT * FROM cinema +WHERE description != 'boring' AND id % 2 = 1 +ORDER BY rating DESC; diff --git a/codes/leetcode/swap-salary.sql b/codes/leetcode/swap-salary.sql new file mode 100644 index 0000000..954e760 --- /dev/null +++ b/codes/leetcode/swap-salary.sql @@ -0,0 +1,27 @@ +-- 给定一个 salary表,如下所示,有m=男性 和 f=女性的值 。 +-- 交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求使用一个更新查询,并且没有中间临时表。 +-- +-- 例如: +-- +-- | id | name | sex | salary | +-- |----|------|-----|--------| +-- | 1 | A | m | 2500 | +-- | 2 | B | f | 1500 | +-- | 3 | C | m | 5500 | +-- | 4 | D | f | 500 | +-- 运行你所编写的查询语句之后,将会得到以下表: +-- +-- | id | name | sex | salary | +-- |----|------|-----|--------| +-- | 1 | A | f | 2500 | +-- | 2 | B | m | 1500 | +-- | 3 | C | f | 5500 | +-- | 4 | D | m | 500 | + +UPDATE salary +SET sex = + CASE sex + WHEN 'm' + THEN 'f' + ELSE 'm' + END;