更新时间: 试题数量: 购买人数: 提供作者:

有效期: 个月

章节介绍: 共有个章节

收藏
搜索
题库预览
## LeetCode1393:股票的资本损益


题目链接:[1393. 股票的资本损益 - 力扣(Leetcode)](https://leetcode.cn/problems/capital-gainloss/)


`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查询来报告每支股票的资本损益。


股票的资本损益是一次或多次买卖股票后的全部收益或损失。


以任意顺序返回结果即可。


SQL查询结果的格式如下例所示:


```

Stocks 表:

+---------------+-----------+---------------+--------+

| stock_name    | operation | operation_day | price  |

+---------------+-----------+---------------+--------+

| Leetcode      | Buy       | 1             | 1000   |

| Corona Masks  | Buy       | 2             | 10     |

| Leetcode      | Sell      | 5             | 9000   |

| Handbags      | Buy       | 17            | 30000  |

| Corona Masks  | Sell      | 3             | 1010   |

| Corona Masks  | Buy       | 4             | 1000   |

| Corona Masks  | Sell      | 5             | 500    |

| Corona Masks  | Buy       | 6             | 1000   |

| Handbags      | Sell      | 29            | 7000   |

| Corona Masks  | Sell      | 10            | 10000  |

+---------------+-----------+---------------+--------+


Result 表:

+---------------+-------------------+

| stock_name    | capital_gain_loss |

+---------------+-------------------+

| Corona Masks  | 9500              |

| Leetcode      | 8000              |

| Handbags      | -23000            |

+---------------+-------------------+

Leetcode 股票在第一天以1000美元的价格买入,在第五天以9000美元的价格卖出。资本收益=9000-1000=8000美元。

Handbags 股票在第17天以30000美元的价格买入,在第29天以7000美元的价格卖出。资本损失=7000-30000=-23000美元。

Corona Masks 股票在第1天以10美元的价格买入,在第3天以1010美元的价格卖出。在第4天以1000美元的价格再次购买,在第5天以500美元的价格出售。最后,它在第6天以1000美元的价格被买走,在第10天以10000美元的价格被卖掉。资本损益是每次(’Buy'->'Sell')操作资本收益或损失的和=(1010-10)+(500-1000)+(10000-1000)=1000-500+9000=9500美元。

```


建表语句:


```mysql

Create Table If Not Exists Stocks (stock_name varchar(15), operation ENUM('Sell', 'Buy'), operation_day int, price int)

Truncate table Stocks

insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Buy', '1', '1000')

insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '2', '10')

insert into Stocks (stock_name, operation, operation_day, price) values ('Leetcode', 'Sell', '5', '9000')

insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Buy', '17', '30000')

insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '3', '1010')

insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '4', '1000')

insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '5', '500')

insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Buy', '6', '1000')

insert into Stocks (stock_name, operation, operation_day, price) values ('Handbags', 'Sell', '29', '7000')

insert into Stocks (stock_name, operation, operation_day, price) values ('Corona Masks', 'Sell', '10', '10000')

```


## LeetCode595:大的国家


题目链接:[595. 大的国家 - 力扣(Leetcode)](https://leetcode.cn/problems/big-countries/)


`World` 表:


```

+-------------+---------+

| Column Name | Type    |

+-------------+---------+

| name        | varchar |

| continent   | varchar |

| area        | int     |

| population  | int     |

| gdp         | int     |

+-------------+---------+

name 是这张表的主键。

这张表的每一行提供:国家名称、所属大陆、面积、人口和 GDP 值。

```


 


如果一个国家满足下述两个条件之一,则认为该国是 **大国** :


- 面积至少为 300 万平方公里(即,`3000000 km2`),或者

- 人口至少为 2500 万(即 `25000000`)


编写一个 SQL 查询以报告 **大国** 的国家名称、人口和面积。


按 **任意顺序** 返回结果表。


查询结果格式如下例所示。


 


**示例:**


```

输入:

World 表:

+-------------+-----------+---------+------------+--------------+

| name        | continent | area    | population | gdp          |

+-------------+-----------+---------+------------+--------------+

| Afghanistan | Asia      | 652230  | 25500100   | 20343000000  |

| Albania     | Europe    | 28748   | 2831741    | 12960000000  |

| Algeria     | Africa    | 2381741 | 37100000   | 188681000000 |

| Andorra     | Europe    | 468     | 78115      | 3712000000   |

| Angola      | Africa    | 1246700 | 20609294   | 100990000000 |

+-------------+-----------+---------+------------+--------------+

输出:

+-------------+------------+---------+

| name        | population | area    |

+-------------+------------+---------+

| Afghanistan | 25500100   | 652230  |

| Algeria     | 37100000   | 2381741 |

+-------------+------------+---------+

```


建表语句:


```mysql

Create table If Not Exists World (name varchar(255), continent varchar(255), area int, population int, gdp int)

Truncate table World

insert into World (name, continent, area, population, gdp) values ('Afghanistan', 'Asia', '652230', '25500100', '20343000000')

insert into World (name, continent, area, population, gdp) values ('Albania', 'Europe', '28748', '2831741', '12960000000')

insert into World (name, continent, area, population, gdp) values ('Algeria', 'Africa', '2381741', '37100000', '188681000000')

insert into World (name, continent, area, population, gdp) values ('Andorra', 'Europe', '468', '78115', '3712000000')

insert into World (name, continent, area, population, gdp) values ('Angola', 'Africa', '1246700', '20609294', '100990000000')

```




## LeetCode1757:可回收且低脂的产品


题目链接:[1757. 可回收且低脂的产品 - 力扣(Leetcode)](https://leetcode.cn/problems/recyclable-and-low-fat-products/)


表:`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 语句,查找既是低脂又是可回收的产品编号。


返回结果 **无顺序要求** 。


查询结果格式如下例所示:


```

Products 表:

+-------------+----------+------------+

| product_id  | low_fats | recyclable |

+-------------+----------+------------+

| 0           | Y        | N          |

| 1           | Y        | Y          |

| 2           | N        | Y          |

| 3           | Y        | Y          |

| 4           | N        | N          |

+-------------+----------+------------+

Result 表:

+-------------+

| product_id  |

+-------------+

| 1           |

| 3           |

+-------------+

只有产品 id 为 1 和 3 的产品,既是低脂又是可回收的产品。

```


建表语句:


```mysql

Create table If Not Exists Products (product_id int, low_fats ENUM('Y', 'N'), recyclable ENUM('Y','N'))

Truncate table Products

insert into Products (product_id, low_fats, recyclable) values ('0', 'Y', 'N')

insert into Products (product_id, low_fats, recyclable) values ('1', 'Y', 'Y')

insert into Products (product_id, low_fats, recyclable) values ('2', 'N', 'Y')

insert into Products (product_id, low_fats, recyclable) values ('3', 'Y', 'Y')

insert into Products (product_id, low_fats, recyclable) values ('4', 'N', 'N')

```


LeetCode511:游戏玩法分析I

题目链接:511. 游戏玩法分析 I - 力扣(Leetcode)

活动表 Activity:

+--------------+---------+
| Column Name | Type |
+--------------+---------+
| player_id | int |
| device_id | int |
| event_date | date |
| games_played | int |
+--------------+---------+
表的主键是 (player_id, event_date)。
这张表展示了一些游戏玩家在游戏平台上的行为活动。
每行数据记录了一名玩家在退出平台之前,当天使用同一台设备登录平台后打开的游戏的数目(可能是 0 个)。

 

写一条 SQL 查询语句获取每位玩家 第一次登陆平台的日期。

查询结果的格式如下所示:

Activity 表:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1 | 2 | 2016-03-01 | 5 |
| 1 | 2 | 2016-05-02 | 6 |
| 2 | 3 | 2017-06-25 | 1 |
| 3 | 1 | 2016-03-02 | 0 |
| 3 | 4 | 2018-07-03 | 5 |
+-----------+-----------+------------+--------------+

Result 表:
+-----------+-------------+
| player_id | first_login |
+-----------+-------------+
| 1 | 2016-03-01 |
| 2 | 2017-06-25 |
| 3 | 2016-03-02 |
+-----------+-------------+

建表语句:

Create table If Not Exists Activity (player_id int, device_id int, event_date date, games_played int)
Truncate table Activity
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-03-01', '5')
insert into Activity (player_id, device_id, event_date, games_played) values ('1', '2', '2016-05-02', '6')
insert into Activity (player_id, device_id, event_date, games_played) values ('2', '3', '2017-06-25', '1')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '1', '2016-03-02', '0')
insert into Activity (player_id, device_id, event_date, games_played) values ('3', '4', '2018-07-03', '5')


LeetCode608:树节点

题目链接:608. 树节点 - 力扣(Leetcode)

给定一个表 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 |
+----+------+

 

解释

节点 '1' 是根节点,因为它的父节点是 NULL ,同时它有孩子节点 '2' 和 '3' 。

节点 '2' 是内部节点,因为它有父节点 '1' ,也有孩子节点 '4' 和 '5' 。

节点 '3', '4' 和 '5' 都是叶子节点,因为它们都有父节点同时没有孩子节点。

样例中树的形态如下:

 

              1
/ \
2 3
/ \
                  4       5

 

注意

如果树中只有一个节点,你只需要输出它的根属性。


建表语句:

Create table If Not Exists Tree (id int, p_id int)
Truncate table Tree
insert into Tree (id, p_id) values ('1', 'None')
insert into Tree (id, p_id) values ('2', '1')
insert into Tree (id, p_id) values ('3', '1')
insert into Tree (id, p_id) values ('4', '2')
insert into Tree (id, p_id) values ('5', '2')


LeetCode185:部门工资前三高的所有员工

题目链接:185. 部门工资前三高的所有员工 - 力扣(Leetcode)

表: Employee

+--------------+---------+
| Column Name | Type |
+--------------+---------+
| id | int |
| name | varchar |
| salary | int |
| departmentId | int |
+--------------+---------+
Id是该表的主键列。
departmentId是Department表中ID的外键。
该表的每一行都表示员工的ID、姓名和工资。它还包含了他们部门的ID。

 

表: Department

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| id | int |
| name | varchar |
+-------------+---------+
Id是该表的主键列。
该表的每一行表示部门ID和部门名。

 

公司的主管们感兴趣的是公司每个部门中谁赚的钱最多。一个部门的 高收入者 是指一个员工的工资在该部门的 不同 工资中 排名前三 。

编写一个SQL查询,找出每个部门中 收入高的员工 。

以 任意顺序 返回结果表。

查询结果格式如下所示。

 

示例 1:

输入:
Employee 表:
+----+-------+--------+--------------+
| id | name | salary | departmentId |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
| 7 | Will | 70000 | 1 |
+----+-------+--------+--------------+
Department 表:
+----+-------+
| id | name |
+----+-------+
| 1 | IT |
| 2 | Sales |
+----+-------+
输出:
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Joe | 85000 |
| IT | Randy | 85000 |
| IT | Will | 70000 |
| Sales | Henry | 80000 |
| Sales | Sam | 60000 |
+------------+----------+--------+
解释:
在IT部门:
- Max的工资最高
- 兰迪和乔都赚取第二高的独特的薪水
- 威尔的薪水是第三高的

在销售部:
- 亨利的工资最高
- 山姆的薪水第二高
- 没有第三高的工资,因为只有两名员工


建表语句:

Create table If Not Exists Employee (id int, name varchar(255), salary int, departmentId int)
Create table If Not Exists Department (id int, name varchar(255))
Truncate table Employee
insert into Employee (id, name, salary, departmentId) values ('1', 'Joe', '85000', '1')
insert into Employee (id, name, salary, departmentId) values ('2', 'Henry', '80000', '2')
insert into Employee (id, name, salary, departmentId) values ('3', 'Sam', '60000', '2')
insert into Employee (id, name, salary, departmentId) values ('4', 'Max', '90000', '1')
insert into Employee (id, name, salary, departmentId) values ('5', 'Janet', '69000', '1')
insert into Employee (id, name, salary, departmentId) values ('6', 'Randy', '85000', '1')
insert into Employee (id, name, salary, departmentId) values ('7', 'Will', '70000', '1')
Truncate table Department
insert into Department (id, name) values ('1', 'IT')
insert into Department (id, name) values ('2', 'Sales')


1