20240104 SQL基础50题打卡

20240104 SQL基础50题打卡

619. 只出现一次的最大数字


MyNumbers 表:

+-------------+------+
| Column Name | Type |
+-------------+------+
| num         | int  |
+-------------+------+
该表可能包含重复项(换句话说,在SQL中,该表没有主键)。
这张表的每一行都含有一个整数。

单一数字 是在 MyNumbers 表中只出现一次的数字。

找出最大的 单一数字 。如果不存在 单一数字 ,则返回 null

查询结果如下例所示。

示例 1:

输入:
MyNumbers 表:
+-----+
| num |
+-----+
| 8   |
| 8   |
| 3   |
| 3   |
| 1   |
| 4   |
| 5   |
| 6   |
+-----+
输出:
+-----+
| num |
+-----+
| 6   |
+-----+
解释:单一数字有 1、4、5 和 6 。
6 是最大的单一数字,返回 6 。

示例 2:

输入:
MyNumbers table:
+-----+
| num |
+-----+
| 8   |
| 8   |
| 7   |
| 7   |
| 3   |
| 3   |
| 3   |
+-----+
输出:
+------+
| num  |
+------+
| null |
+------+
解释:输入的表中不存在单一数字,所以返回 null 。

题解:

# Write your MySQL query statement below
select max(num) as num
from (
    select num
    from MyNumbers
    group by num
    having count(*) = 1
) as nums

思路:通过 num 分组判断组内是否只有一个成员,如果只有一个那么就选出来,再选出来的集合中,重新求取最大值即可。

1045. 买下所有产品的客户


Customer 表:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| customer_id | int     |
| product_key | int     |
+-------------+---------+
该表可能包含重复的行。
customer_id 不为 NULL。
product_key 是 Product 表的外键(reference 列)。

Product 表:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| product_key | int     |
+-------------+---------+
product_key 是这张表的主键(具有唯一值的列)。

编写解决方案,报告 Customer 表中购买了 Product 表中所有产品的客户的 id。

返回结果表 无顺序要求

返回结果格式如下所示。

示例 1:

输入:
Customer 表:
+-------------+-------------+
| customer_id | product_key |
+-------------+-------------+
| 1           | 5           |
| 2           | 6           |
| 3           | 5           |
| 3           | 6           |
| 1           | 6           |
+-------------+-------------+
Product 表:
+-------------+
| product_key |
+-------------+
| 5           |
| 6           |
+-------------+
输出:
+-------------+
| customer_id |
+-------------+
| 1           |
| 3           |
+-------------+
解释:
购买了所有产品(5 和 6)的客户的 id 是 1 和 3 。

题解:

# Write your MySQL query statement below
select customer_id 
from Customer
group by customer_id
having count(distinct product_key) = (select count(*) from product);

1731. 每位经理的下属员工数量


Table: Employees

+-------------+----------+
| Column Name | Type     |
+-------------+----------+
| employee_id | int      |
| name        | varchar  |
| reports_to  | int      |
| age         | int      |
+-------------+----------+
employee_id 是这个表的主键.
该表包含员工以及需要听取他们汇报的上级经理的ID的信息。 有些员工不需要向任何人汇报(reports_to 为空)。

对于此问题,我们将至少有一个其他员工需要向他汇报的员工,视为一个经理。

编写SQL查询需要听取汇报的所有经理的ID、名称、直接向该经理汇报的员工人数,以及这些员工的平均年龄,其中该平均年龄需要四舍五入到最接近的整数。

返回的结果集需要按照 employee_id 进行排序。

查询结果的格式如下:

Employees table:
+-------------+---------+------------+-----+
| employee_id | name    | reports_to | age |
+-------------+---------+------------+-----+
| 9           | Hercy   | null       | 43  |
| 6           | Alice   | 9          | 41  |
| 4           | Bob     | 9          | 36  |
| 2           | Winston | null       | 37  |
+-------------+---------+------------+-----+

Result table:
+-------------+-------+---------------+-------------+
| employee_id | name  | reports_count | average_age |
+-------------+-------+---------------+-------------+
| 9           | Hercy | 2             | 39          |
+-------------+-------+---------------+-------------+
Hercy 有两个需要向他汇报的员工, 他们是 Alice and Bob. 他们的平均年龄是 (41+36)/2 = 38.5, 四舍五入的结果是 39.

题解:

# Write your MySQL query statement below
select employee_id,name,num as reports_count, ROUND( avgAge,0) as average_age
from Employees
inner join (
    select reports_to, count(*) as num , avg(age) as avgAge
    from Employees  
    group by reports_to
) as one
on Employees.employee_id = one.reports_to
order by employee_id;

大家好,我是xwhking,一名技术爱好者,目前正在全力学习 Java,前端也会一点,如果你有任何疑问请你评论,或者可以加我QQ(2837468248)说明来意!希望能够与你共同进步

相关推荐

  1. 20240104 SQL基础50

    2024-01-05 14:22:02       60 阅读
  2. 20240107 SQL基础50

    2024-01-05 14:22:02       68 阅读
  3. 20231228 SQL基础50

    2024-01-05 14:22:02       67 阅读
  4. 20231230 SQL基础50

    2024-01-05 14:22:02       49 阅读
  5. 【力扣 | 分享】高频 SQL 50 基础版)

    2024-01-05 14:22:02       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-05 14:22:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-05 14:22:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-05 14:22:02       87 阅读
  4. Python语言-面向对象

    2024-01-05 14:22:02       96 阅读

热门阅读

  1. SLAM学习入门--编程语言

    2024-01-05 14:22:02       59 阅读
  2. 自动驾驶货车编队行驶系统功能规范

    2024-01-05 14:22:02       65 阅读
  3. 51单片机点灯入门教程——2. 呼吸灯效果

    2024-01-05 14:22:02       58 阅读
  4. 14.9-时序和组合的混合逻辑——使用非阻塞赋值

    2024-01-05 14:22:02       57 阅读
  5. 【.NET Core】记录(Record)详解

    2024-01-05 14:22:02       51 阅读
  6. Android.bp 常用模块类型

    2024-01-05 14:22:02       46 阅读
  7. redis的springboot配置

    2024-01-05 14:22:02       55 阅读