美文网首页
sql刷题笔记(七)

sql刷题笔记(七)

作者: 顾子豪 | 来源:发表于2021-06-26 12:37 被阅读0次

    题目选自leetcode 上的题库

    可能不一定都是最优解,答案仅供参考

    每道题后面都应相应的难度等级,如果没时间做的话 可以在leetcode 按出题频率刷题

    祝大家面试取得好的成绩

    1468. 计算税后工资

    难度中等

    SQL架构

    Salaries 表:

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | company_id    | int     |
    | employee_id   | int     |
    | employee_name | varchar |
    | salary        | int     |
    +---------------+---------+
    (company_id, employee_id) 是这个表的主键
    这个表包括员工的company id, id, name 和 salary 
    

    写一条查询 SQL 来查找每个员工的税后工资

    每个公司的税率计算依照以下规则

    • 如果这个公司员工最高工资不到 1000 ,税率为 0%
    • 如果这个公司员工最高工资在 1000 到 10000 之间,税率为 24%
    • 如果这个公司员工最高工资大于 10000 ,税率为 49%

    按任意顺序返回结果,税后工资结果取整

    结果表格式如下例所示:

    Salaries 表:
    +------------+-------------+---------------+--------+
    | company_id | employee_id | employee_name | salary |
    +------------+-------------+---------------+--------+
    | 1          | 1           | Tony          | 2000   |
    | 1          | 2           | Pronub        | 21300  |
    | 1          | 3           | Tyrrox        | 10800  |
    | 2          | 1           | Pam           | 300    |
    | 2          | 7           | Bassem        | 450    |
    | 2          | 9           | Hermione      | 700    |
    | 3          | 7           | Bocaben       | 100    |
    | 3          | 2           | Ognjen        | 2200   |
    | 3          | 13          | Nyancat       | 3300   |
    | 3          | 15          | Morninngcat   | 1866   |
    +------------+-------------+---------------+--------+
    
    Result 表:
    +------------+-------------+---------------+--------+
    | company_id | employee_id | employee_name | salary |
    +------------+-------------+---------------+--------+
    | 1          | 1           | Tony          | 1020   |
    | 1          | 2           | Pronub        | 10863  |
    | 1          | 3           | Tyrrox        | 5508   |
    | 2          | 1           | Pam           | 300    |
    | 2          | 7           | Bassem        | 450    |
    | 2          | 9           | Hermione      | 700    |
    | 3          | 7           | Bocaben       | 76     |
    | 3          | 2           | Ognjen        | 1672   |
    | 3          | 13          | Nyancat       | 2508   |
    | 3          | 15          | Morninngcat   | 5911   |
    +------------+-------------+---------------+--------+
    对于公司 1 ,最高工资是 21300 ,其每个员工的税率为 49%
    对于公司 2 ,最高工资是 700 ,其每个员工税率为 0%
    对于公司 3 ,最高工资是 7777 ,其每个员工税率是 24%
    税后工资计算 = 工资 - ( 税率 / 100)*工资
    对于上述案例,Morninngcat 的税后工资 = 7777 - 7777 * ( 24 / 100) = 7777 - 1866.48 = 5910.52 ,取整为 5911
    
    select company_id,employee_id , employee_name,
    round(case when maxsalary<1000 then salary
           when maxsalary<10000 then salary*(1-0.24)
           else salary*(1-0.49) end ,0)salary
    from(
        select *,max(salary) over(partition by company_id ) maxsalary
        from Salaries 
    )t1
    

    1479. 周内每天的销售情况

    难度困难

    SQL架构

    表:Orders

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | order_id      | int     |
    | customer_id   | int     |
    | order_date    | date    | 
    | item_id       | varchar |
    | quantity      | int     |
    +---------------+---------+
    (order_id, item_id) 是该表主键
    该表包含了订单信息
    order_date 是id为 item_id 的商品被id为 customer_id 的消费者订购的日期.
    

    表:Items

    +---------------------+---------+
    | Column Name         | Type    |
    +---------------------+---------+
    | item_id             | varchar |
    | item_name           | varchar |
    | item_category       | varchar |
    +---------------------+---------+
    item_id 是该表主键
    item_name 是商品的名字
    item_category 是商品的类别
    

    你是企业主,想要获得分类商品和周内每天的销售报告。

    写一个SQL语句,报告 周内每天 每个商品类别下订购了多少单位。

    返回结果表单 按商品类别排序

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

    Orders 表:
    +------------+--------------+-------------+--------------+-------------+
    | order_id   | customer_id  | order_date  | item_id      | quantity    |
    +------------+--------------+-------------+--------------+-------------+
    | 1          | 1            | 2020-06-01  | 1            | 10          |
    | 2          | 1            | 2020-06-08  | 2            | 10          |
    | 3          | 2            | 2020-06-02  | 1            | 5           |
    | 4          | 3            | 2020-06-03  | 3            | 5           |
    | 5          | 4            | 2020-06-04  | 4            | 1           |
    | 6          | 4            | 2020-06-05  | 5            | 5           |
    | 7          | 5            | 2020-06-05  | 1            | 10          |
    | 8          | 5            | 2020-06-14  | 4            | 5           |
    | 9          | 5            | 2020-06-21  | 3            | 5           |
    +------------+--------------+-------------+--------------+-------------+
    
    Items 表:
    +------------+----------------+---------------+
    | item_id    | item_name      | item_category |
    +------------+----------------+---------------+
    | 1          | LC Alg. Book   | Book          |
    | 2          | LC DB. Book    | Book          |
    | 3          | LC SmarthPhone | Phone         |
    | 4          | LC Phone 2020  | Phone         |
    | 5          | LC SmartGlass  | Glasses       |
    | 6          | LC T-Shirt XL  | T-Shirt       |
    +------------+----------------+---------------+
    
    Result 表:
    +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    | Category   | Monday    | Tuesday   | Wednesday | Thursday  | Friday    | Saturday  | Sunday    |
    +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    | Book       | 20        | 5         | 0         | 0         | 10        | 0         | 0         |
    | Glasses    | 0         | 0         | 0         | 0         | 5         | 0         | 0         |
    | Phone      | 0         | 0         | 5         | 1         | 0         | 0         | 10        |
    | T-Shirt    | 0         | 0         | 0         | 0         | 0         | 0         | 0         |
    +------------+-----------+-----------+-----------+-----------+-----------+-----------+-----------+
    在周一(2020-06-01, 2020-06-08),Book分类(ids: 1, 2)下,总共销售了20个单位(10 + 10)
    在周二(2020-06-02),Book分类(ids: 1, 2)下,总共销售了5个单位
    在周三(2020-06-03),Phone分类(ids: 3, 4)下,总共销售了5个单位
    在周四(2020-06-04),Phone分类(ids: 3, 4)下,总共销售了1个单位
    在周五(2020-06-05),Book分类(ids: 1, 2)下,总共销售了10个单位,Glasses分类(ids: 5)下,总共销售了5个单位
    在周六, 没有商品销售
    在周天(2020-06-14, 2020-06-21),Phone分类(ids: 3, 4)下,总共销售了10个单位(5 + 5)
    没有销售 T-Shirt 类别的商品
    
    select item_category as category, 
    sum(case when num = 2 then quantity else 0 end) as Monday,
    sum(case when num = 3 then quantity else 0 end) as Tuesday,
    sum(case when num = 4 then quantity else 0 end) as Wednesday,
    sum(case when num = 5 then quantity else 0 end) as Thursday,
    sum(case when num = 6 then quantity else 0 end) as Friday,
    sum(case when num = 7 then quantity else 0 end) as Saturday,
    sum(case when num = 1 then quantity else 0 end) as Sunday
    from
    (select item_category, quantity,dayofweek(order_date) as num from 
    items i left join orders o 
    on i.item_id=o.item_id) t
    group by item_category
    order by item_category
    

    1485. 按日期分组销售产品

    难度简单

    SQL架构

    Activities

    +-------------+---------+
    | 列名         | 类型    |
    +-------------+---------+
    | sell_date   | date    |
    | product     | varchar |
    +-------------+---------+
    此表没有主键,它可能包含重复项。
    此表的每一行都包含产品名称和在市场上销售的日期。
    

    编写一个 SQL 查询来查找每个日期、销售的不同产品的数量及其名称。
    每个日期的销售产品名称应按词典序排列。
    返回按 sell_date 排序的结果表。

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

    Activities 表:
    +------------+-------------+
    | sell_date  | product     |
    +------------+-------------+
    | 2020-05-30 | Headphone   |
    | 2020-06-01 | Pencil      |
    | 2020-06-02 | Mask        |
    | 2020-05-30 | Basketball  |
    | 2020-06-01 | Bible       |
    | 2020-06-02 | Mask        |
    | 2020-05-30 | T-Shirt     |
    +------------+-------------+
    
    Result 表:
    +------------+----------+------------------------------+
    | sell_date  | num_sold | products                     |
    +------------+----------+------------------------------+
    | 2020-05-30 | 3        | Basketball,Headphone,T-shirt |
    | 2020-06-01 | 2        | Bible,Pencil                 |
    | 2020-06-02 | 1        | Mask                         |
    +------------+----------+------------------------------+
    对于2020-05-30,出售的物品是 (Headphone, Basketball, T-shirt),按词典序排列,并用逗号 ',' 分隔。
    对于2020-06-01,出售的物品是 (Pencil, Bible),按词典序排列,并用逗号分隔。
    对于2020-06-02,出售的物品是 (Mask),只需返回该物品名。
    
    select sell_date, count(distinct product) num_sold, 
        group_concat(distinct product order by product) products
    from Activities
    group by sell_date
    

    行转列

    1495. 上月播放的儿童适宜电影

    难度简单

    SQL架构

    表: TVProgram

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | program_date  | date    |
    | content_id    | int     |
    | channel       | varchar |
    +---------------+---------+
    (program_date, content_id) 是该表主键.
    该表包含电视上的节目信息.
    content_id 是电视一些频道上的节目的 id.
    

    表: Content

    +------------------+---------+
    | Column Name      | Type    |
    +------------------+---------+
    | content_id       | varchar |
    | title            | varchar |
    | Kids_content     | enum    |
    | content_type     | varchar |
    +------------------+---------+
    content_id 是该表主键.
    Kids_content 是枚举类型, 取值为('Y', 'N'), 其中: 
    'Y' 表示儿童适宜内容, 而'N'表示儿童不宜内容.
    content_type 表示内容的类型, 比如电影, 电视剧等.
    

    写一个 SQL 语句, 报告在 2020 年 6 月份播放的儿童适宜电影的去重电影名.

    返回的结果表单没有顺序要求.

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

    TVProgram 表:
    +--------------------+--------------+-------------+
    | program_date       | content_id   | channel     |
    +--------------------+--------------+-------------+
    | 2020-06-10 08:00   | 1            | LC-Channel  |
    | 2020-05-11 12:00   | 2            | LC-Channel  |
    | 2020-05-12 12:00   | 3            | LC-Channel  |
    | 2020-05-13 14:00   | 4            | Disney Ch   |
    | 2020-06-18 14:00   | 4            | Disney Ch   |
    | 2020-07-15 16:00   | 5            | Disney Ch   |
    +--------------------+--------------+-------------+
    
    Content 表:
    +------------+----------------+---------------+---------------+
    | content_id | title          | Kids_content  | content_type  |
    +------------+----------------+---------------+---------------+
    | 1          | Leetcode Movie | N             | Movies        |
    | 2          | Alg. for Kids  | Y             | Series        |
    | 3          | Database Sols  | N             | Series        |
    | 4          | Aladdin        | Y             | Movies        |
    | 5          | Cinderella     | Y             | Movies        |
    +------------+----------------+---------------+---------------+
    
    Result 表:
    +--------------+
    | title        |
    +--------------+
    | Aladdin      |
    +--------------+
    "Leetcode Movie" 是儿童不宜的电影.
    "Alg. for Kids" 不是电影.
    "Database Sols" 不是电影
    "Alladin" 是电影, 儿童适宜, 并且在 2020 年 6 月份播放.
    "Cinderella" 不在 2020 年 6 月份播放.
    
    select distinct title   
    from TVProgram t left join Content c
    on t.content_id  = c.content_id 
    where  Kids_content ='Y' 
    and date_format(program_date ,'%Y-%m')='2020-06'
    and content_type='Movies'
    

    LEFT()函数参见:https://www.begtut.com/sql/func-mysql-left.html
    REGEXP语法参见:
    https://www.cnblogs.com/timssd/p/5882742.html
    https://www.cnblogs.com/zhaopanpan/p/10133224.html
    DATE_FORMAT()函数参见:https://www.w3school.com.cn/sql/func_date_format.asp
    EXTRACT()函数参见:https://www.runoob.com/sql/func-extract.html
    DATEDIFF()函数参见:https://www.runoob.com/sql/func-datediff-mysql.html
    YEAR()函数参见:https://blog.csdn.net/moakun/article/details/82528829
    MONTH()函数参见:https://www.yiibai.com/mysql/month.html

    1501. 可以放心投资的国家

    难度中等

    SQL架构

    Person:

    +----------------+---------+
    | Column Name    | Type    |
    +----------------+---------+
    | id             | int     |
    | name           | varchar |
    | phone_number   | varchar |
    +----------------+---------+
    id 是该表主键.
    该表每一行包含一个人的名字和电话号码.
    电话号码的格式是:'xxx-yyyyyyy', 其中xxx是国家码(3个字符), yyyyyyy是电话号码(7个字符), x和y都表示数字. 同时, 国家码和电话号码都可以包含前导0.
    

    Country:

    +----------------+---------+
    | Column Name    | Type    |
    +----------------+---------+
    | name           | varchar |
    | country_code   | varchar |
    +----------------+---------+
    country_code是该表主键.
    该表每一行包含国家名和国家码. country_code的格式是'xxx', x是数字.
    

    Calls:

    +-------------+------+
    | Column Name | Type |
    +-------------+------+
    | caller_id   | int  |
    | callee_id   | int  |
    | duration    | int  |
    +-------------+------+
    该表无主键, 可能包含重复行.
    每一行包含呼叫方id, 被呼叫方id和以分钟为单位的通话时长. caller_id != callee_id
    

    一家电信公司想要投资新的国家. 该公司想要投资的国家是: 该国的平均通话时长要严格地大于全球平均通话时长.

    写一段 SQL, 找到所有该公司可以投资的国家.

    返回的结果表没有顺序要求.

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

    Person 表:
    +----+----------+--------------+
    | id | name     | phone_number |
    +----+----------+--------------+
    | 3  | Jonathan | 051-1234567  |
    | 12 | Elvis    | 051-7654321  |
    | 1  | Moncef   | 212-1234567  |
    | 2  | Maroua   | 212-6523651  |
    | 7  | Meir     | 972-1234567  |
    | 9  | Rachel   | 972-0011100  |
    +----+----------+--------------+
    
    Country 表:
    +----------+--------------+
    | name     | country_code |
    +----------+--------------+
    | Peru     | 051          |
    | Israel   | 972          |
    | Morocco  | 212          |
    | Germany  | 049          |
    | Ethiopia | 251          |
    +----------+--------------+
    
    Calls 表:
    +-----------+-----------+----------+
    | caller_id | callee_id | duration |
    +-----------+-----------+----------+
    | 1         | 9         | 33       |
    | 2         | 9         | 4        |
    | 1         | 2         | 59       |
    | 3         | 12        | 102      |
    | 3         | 12        | 330      |
    | 12        | 3         | 5        |
    | 7         | 9         | 13       |
    | 7         | 1         | 3        |
    | 9         | 7         | 1        |
    | 1         | 7         | 7        |
    +-----------+-----------+----------+
    
    Result 表:
    +----------+
    | country  |
    +----------+
    | Peru     |
    +----------+
    国家Peru的平均通话时长是 (102 + 102 + 330 + 330 + 5 + 5) / 6 = 145.666667
    国家Israel的平均通话时长是 (33 + 4 + 13 + 13 + 3 + 1 + 1 + 7) / 8 = 9.37500
    国家Morocco的平均通话时长是 (33 + 4 + 59 + 59 + 3 + 7) / 6 = 27.5000 
    全球平均通话时长 = (2 * (33 + 3 + 59 + 102 + 330 + 5 + 13 + 3 + 1 + 7)) / 20 = 55.70000
    所以, Peru是唯一的平均通话时长大于全球平均通话时长的国家, 也是唯一的推荐投资的国家.
    

    笛卡尔积

    select c2.name as country 
    from Calls c1,Person p,Country c2
    where (p.id=c1.caller_id or p.id=c1.callee_id) and c2.country_code=left(p.phone_number,3)
    group by c2.name 
    having avg(duration)>(select avg(duration) from Calls)
    

    思路更清晰

    with people_country as
    (
        select id, c.name country
        from Person p left join Country c
        on left(p.phone_number,3) = c.country_code
    )
    
    select country
    from
    (
        select country, avg(duration) avgtime
        from
        (
            select caller_id id, duration
            from Calls
            union all
            select callee_id, duration
            from Calls
        ) t left join people_country
        using(id)
        group by country
    ) temp
    where avgtime > 
        (
            select avg(duration) avgtime
            from
            (
                select caller_id, duration
                from Calls
                union all
                select callee_id, duration
                from Calls
            ) t
        )
    

    1511. 消费者下单频率

    难度简单

    SQL架构

    表: Customers

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | customer_id   | int     |
    | name          | varchar |
    | country       | varchar |
    +---------------+---------+
    customer_id 是该表主键.
    该表包含公司消费者的信息.
    

    表: Product

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | product_id    | int     |
    | description   | varchar |
    | price         | int     |
    +---------------+---------+
    product_id 是该表主键.
    该表包含公司产品的信息.
    price 是本产品的花销.
    

    表: Orders

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | order_id      | int     |
    | customer_id   | int     |
    | product_id    | int     |
    | order_date    | date    |
    | quantity      | int     |
    +---------------+---------+
    order_id 是该表主键.
    该表包含消费者下单的信息.
    customer_id 是买了数量为"quantity", id为"product_id"产品的消费者的 id.
    Order_date 是订单发货的日期, 格式为('YYYY-MM-DD').
    

    写一个 SQL 语句, 报告消费者的 id 和名字, 其中消费者在 2020 年 6 月和 7 月, 每月至少花费了$100.

    结果表无顺序要求.

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

    Customers
    +--------------+-----------+-------------+
    | customer_id  | name      | country     |
    +--------------+-----------+-------------+
    | 1            | Winston   | USA         |
    | 2            | Jonathan  | Peru        |
    | 3            | Moustafa  | Egypt       |
    +--------------+-----------+-------------+
    
    Product
    +--------------+-------------+-------------+
    | product_id   | description | price       |
    +--------------+-------------+-------------+
    | 10           | LC Phone    | 300         |
    | 20           | LC T-Shirt  | 10          |
    | 30           | LC Book     | 45          |
    | 40           | LC Keychain | 2           |
    +--------------+-------------+-------------+
    
    Orders
    +--------------+-------------+-------------+-------------+-----------+
    | order_id     | customer_id | product_id  | order_date  | quantity  |
    +--------------+-------------+-------------+-------------+-----------+
    | 1            | 1           | 10          | 2020-06-10  | 1         |
    | 2            | 1           | 20          | 2020-07-01  | 1         |
    | 3            | 1           | 30          | 2020-07-08  | 2         |
    | 4            | 2           | 10          | 2020-06-15  | 2         |
    | 5            | 2           | 40          | 2020-07-01  | 10        |
    | 6            | 3           | 20          | 2020-06-24  | 2         |
    | 7            | 3           | 30          | 2020-06-25  | 2         |
    | 9            | 3           | 30          | 2020-05-08  | 3         |
    +--------------+-------------+-------------+-------------+-----------+
    
    Result 表:
    +--------------+------------+
    | customer_id  | name       |  
    +--------------+------------+
    | 1            | Winston    |
    +--------------+------------+ 
    Winston 在2020年6月花费了$300(300 * 1), 在7月花费了$100(10 * 1 + 45 * 2).
    Jonathan 在2020年6月花费了$600(300 * 2), 在7月花费了$20(2 * 10).
    Moustafa 在2020年6月花费了$110 (10 * 2 + 45 * 2), 在7月花费了$0.
    
    select customer_id,name
    from Customers
    where customer_id in
    (select customer_id
        from
            (select customer_id, month(order_date) as month , sum(quantity*price) as total
            from Orders o left join Product p on o.product_id = p.product_id
            where month(order_date) = 6 or month(order_date)=7
            group by customer_id,month(order_date)
            ) as t1
        where total >=100
        group by customer_id
        having count(*)>=2
    )
    

    1517. Find Users With Valid E-Mails

    难度简单

    SQL架构

    Table: Users

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | user_id       | int     |
    | name          | varchar |
    | mail          | varchar |
    +---------------+---------+
    user_id is the primary key for this table.
    This table contains information of the users signed up in a website. Some e-mails are invalid.
    

    Write an SQL query to find the users who have valid emails.

    A valid e-mail has a prefix name and a domain where:

    • The prefix name is a string that may contain letters (upper or lower case), digits, underscore '_', period '.' and/or dash '-'. The prefix name must start with a letter.
    • The domain is '@leetcode.com'.

    Return the result table in any order.

    The query result format is in the following example.

    Users
    +---------+-----------+-------------------------+
    | user_id | name      | mail                    |
    +---------+-----------+-------------------------+
    | 1       | Winston   | winston@leetcode.com    |
    | 2       | Jonathan  | jonathanisgreat         |
    | 3       | Annabelle | bella-@leetcode.com     |
    | 4       | Sally     | sally.come@leetcode.com |
    | 5       | Marwan    | quarz#2020@leetcode.com |
    | 6       | David     | david69@gmail.com       |
    | 7       | Shapiro   | .shapo@leetcode.com     |
    +---------+-----------+-------------------------+
    
    Result table:
    +---------+-----------+-------------------------+
    | user_id | name      | mail                    |
    +---------+-----------+-------------------------+
    | 1       | Winston   | winston@leetcode.com    |
    | 3       | Annabelle | bella-@leetcode.com     |
    | 4       | Sally     | sally.come@leetcode.com |
    +---------+-----------+-------------------------+
    The mail of user 2 doesn't have a domain.
    The mail of user 5 has # sign which is not allowed.
    The mail of user 6 doesn't have leetcode domain.
    The mail of user 7 starts with a period.
    

    考察正则表达式的使用

    SELECT *
    FROM Users
    WHERE mail REGEXP '^[a-zA-Z]+[\\w_\\.\\-]*@leetcode.com$'   
    ORDER BY user_id;
    
    select * from Users
    where mail regexp '^[a-zA-Z]+[a-zA-Z0-9_\\./\\-]{0,}@leetcode.com$'
    order by user_id
    

    坑点:
    1、前缀可能是一个字母,比如“J@leetcode.com”,所以匹配非首字母外的前缀字符数量要用{0,}或*,不能用+。
    2、题意要求:underscore '', period '.' and/or dash '-',/没加单引号,不留神可能写漏/。
    3、后缀可能是“@leetcodeecom”,所以要对“.”加转义符号。
    4、后缀可能是“@LEETCODE.COM”,默认是不区分大小写匹配,所以要加上“BINARY”区分大小写。
    语法:
    1、https://www.cnblogs.com/timssd/p/5882742.html
    2、https://www.cnblogs.com/zhaopanpan/p/10133224.html
    3、"双反斜杠+w"表示字母、数字、下划线,相对"a-zA-Z0-9"的写法更简洁。

    1527. Patients With a Condition

    难度简单

    SQL架构

    Table: Patients

    +--------------+---------+
    | Column Name  | Type    |
    +--------------+---------+
    | patient_id   | int     |
    | patient_name | varchar |
    | conditions   | varchar |
    +--------------+---------+
    patient_id is the primary key for this table.
    'conditions' contains 0 or more code separated by spaces. 
    This table contains information of the patients in the hospital.
    

    Write an SQL query to report the patient_id, patient_name all conditions of patients who have Type I Diabetes. Type I Diabetes always starts with DIAB1 prefix

    Return the result table in any order.

    The query result format is in the following example.

    Patients
    +------------+--------------+--------------+
    | patient_id | patient_name | conditions   |
    +------------+--------------+--------------+
    | 1          | Daniel       | YFEV COUGH   |
    | 2          | Alice        |              |
    | 3          | Bob          | DIAB100 MYOP |
    | 4          | George       | ACNE DIAB100 |
    | 5          | Alain        | DIAB201      |
    +------------+--------------+--------------+
    
    Result table:
    +------------+--------------+--------------+
    | patient_id | patient_name | conditions   |
    +------------+--------------+--------------+
    | 3          | Bob          | DIAB100 MYOP |
    | 4          | George       | ACNE DIAB100 | 
    +------------+--------------+--------------+
    Bob and George both have a condition that starts with DIAB1.
    
    select  patient_id , patient_name ,conditions 
    from Patients
    where conditions like '%DIAB1%'
    

    1532. The Most Recent Three Orders

    难度中等

    SQL架构

    Table: Customers

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | customer_id   | int     |
    | name          | varchar |
    +---------------+---------+
    customer_id is the primary key for this table.
    This table contains information about customers.
    

    Table: Orders

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | order_id      | int     |
    | order_date    | date    |
    | customer_id   | int     |
    | cost          | int     |
    +---------------+---------+
    order_id is the primary key for this table.
    This table contains information about the orders made by customer_id.
    Each customer has one order per day.
    

    Write an SQL query to find the most recent 3 orders of each user. If a user ordered less than 3 orders return all of their orders.

    Return the result table sorted by customer_name in ascending order and in case of a tie by the customer_id in ascending order. If there still a tie, order them by the order_date in descending order.

    The query result format is in the following example:

    Customers
    +-------------+-----------+
    | customer_id | name      |
    +-------------+-----------+
    | 1           | Winston   |
    | 2           | Jonathan  |
    | 3           | Annabelle |
    | 4           | Marwan    |
    | 5           | Khaled    |
    +-------------+-----------+
    
    Orders
    +----------+------------+-------------+------+
    | order_id | order_date | customer_id | cost |
    +----------+------------+-------------+------+
    | 1        | 2020-07-31 | 1           | 30   |
    | 2        | 2020-07-30 | 2           | 40   |
    | 3        | 2020-07-31 | 3           | 70   |
    | 4        | 2020-07-29 | 4           | 100  |
    | 5        | 2020-06-10 | 1           | 1010 |
    | 6        | 2020-08-01 | 2           | 102  |
    | 7        | 2020-08-01 | 3           | 111  |
    | 8        | 2020-08-03 | 1           | 99   |
    | 9        | 2020-08-07 | 2           | 32   |
    | 10       | 2020-07-15 | 1           | 2    |
    +----------+------------+-------------+------+
    
    Result table:
    +---------------+-------------+----------+------------+
    | customer_name | customer_id | order_id | order_date |
    +---------------+-------------+----------+------------+
    | Annabelle     | 3           | 7        | 2020-08-01 |
    | Annabelle     | 3           | 3        | 2020-07-31 |
    | Jonathan      | 2           | 9        | 2020-08-07 |
    | Jonathan      | 2           | 6        | 2020-08-01 |
    | Jonathan      | 2           | 2        | 2020-07-30 |
    | Marwan        | 4           | 4        | 2020-07-29 |
    | Winston       | 1           | 8        | 2020-08-03 |
    | Winston       | 1           | 1        | 2020-07-31 |
    | Winston       | 1           | 10       | 2020-07-15 |
    +---------------+-------------+----------+------------+
    Winston has 4 orders, we discard the order of "2020-06-10" because it is the oldest order.
    Annabelle has only 2 orders, we return them.
    Jonathan has exactly 3 orders.
    Marwan ordered only one time.
    We sort the result table by customer_name in ascending order, by customer_id in ascending order and by order_date in descending order in case of a tie.
    

    Follow-up:
    Can you write a general solution for the most recent n orders?

    select name customer_name ,customer_id,order_id,order_date
    from (
    select  name ,o.customer_id,order_id,order_date ,rank()over(partition by o.customer_id order by order_date desc) rk
    from Orders o left join Customers c
    on o.customer_id=c.customer_id
    )t1
    where rk <=3
    order by customer_name ,customer_id,order_date desc
    

    1543. Fix Product Name Format

    难度简单

    SQL架构

    Table: Sales

    +--------------+---------+
    | Column Name  | Type    |
    +--------------+---------+
    | sale_id      | int     |
    | product_name | varchar |
    | sale_date    | date    |
    +--------------+---------+
    sale_id is the primary key for this table.
    Each row of this table contains the product name and the date it was sold.
    

    Since table Sales was filled manually in the year 2000, product_name may contain leading and/or trailing white spaces, also they are case-insensitive.

    Write an SQL query to report

    • product_name in lowercase without leading or trailing white spaces.
    • sale_date in the format ('YYYY-MM')
    • total the number of times the product was sold in this month.

    Return the result table ordered by product_name in ascending order, in case of a tie order it by sale_date in ascending order.

    The query result format is in the following example.

    Sales
    +------------+------------------+--------------+
    | sale_id    | product_name     | sale_date    |
    +------------+------------------+--------------+
    | 1          |      LCPHONE     | 2000-01-16   |
    | 2          |    LCPhone       | 2000-01-17   |
    | 3          |     LcPhOnE      | 2000-02-18   |
    | 4          |      LCKeyCHAiN  | 2000-02-19   |
    | 5          |   LCKeyChain     | 2000-02-28   |
    | 6          | Matryoshka       | 2000-03-31   | 
    +------------+------------------+--------------+
    
    Result table:
    +--------------+--------------+----------+
    | product_name | sale_date    | total    |
    +--------------+--------------+----------+
    | lcphone      | 2000-01      | 2        |
    | lckeychain   | 2000-02      | 2        | 
    | lcphone      | 2000-02      | 1        | 
    | matryoshka   | 2000-03      | 1        | 
    +--------------+--------------+----------+
    
    In January, 2 LcPhones were sold, please note that the product names are not case sensitive and may contain spaces.
    In Februery, 2 LCKeychains and 1 LCPhone were sold.
    In March, 1 matryoshka was sold.
    
    select trim(lower(product_name)) as product_name, 
            date_format(sale_date,'%Y-%m') as sale_date,
            count(*) as total 
    from Sales 
    group by trim(lower(product_name)), date_format(sale_date,'%Y-%m') 
    order by product_name asc, sale_date asc
    

    注意大小写、空格

    1549. The Most Recent Orders for Each Product

    难度中等

    SQL架构

    Table: Customers

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | customer_id   | int     |
    | name          | varchar |
    +---------------+---------+
    customer_id is the primary key for this table.
    This table contains information about the customers.
    

    Table: Orders

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | order_id      | int     |
    | order_date    | date    |
    | customer_id   | int     |
    | product_id    | int     |
    +---------------+---------+
    order_id is the primary key for this table.
    This table contains information about the orders made by customer_id.
    There will be no product ordered by the same user more than once in one day.
    

    Table: Products

    +---------------+---------+
    | Column Name   | Type    |
    +---------------+---------+
    | product_id    | int     |
    | product_name  | varchar |
    | price         | int     |
    +---------------+---------+
    product_id is the primary key for this table.
    This table contains information about the Products.
    

    Write an SQL query to find the most recent order(s) of each product.

    Return the result table sorted by product_name in ascending order and in case of a tie by the product_id in ascending order. If there still a tie, order them by the order_id in ascending order.

    The query result format is in the following example:

    Customers
    +-------------+-----------+
    | customer_id | name      |
    +-------------+-----------+
    | 1           | Winston   |
    | 2           | Jonathan  |
    | 3           | Annabelle |
    | 4           | Marwan    |
    | 5           | Khaled    |
    +-------------+-----------+
    
    Orders
    +----------+------------+-------------+------------+
    | order_id | order_date | customer_id | product_id |
    +----------+------------+-------------+------------+
    | 1        | 2020-07-31 | 1           | 1          |
    | 2        | 2020-07-30 | 2           | 2          |
    | 3        | 2020-08-29 | 3           | 3          |
    | 4        | 2020-07-29 | 4           | 1          |
    | 5        | 2020-06-10 | 1           | 2          |
    | 6        | 2020-08-01 | 2           | 1          |
    | 7        | 2020-08-01 | 3           | 1          |
    | 8        | 2020-08-03 | 1           | 2          |
    | 9        | 2020-08-07 | 2           | 3          |
    | 10       | 2020-07-15 | 1           | 2          |
    +----------+------------+-------------+------------+
    
    Products
    +------------+--------------+-------+
    | product_id | product_name | price |
    +------------+--------------+-------+
    | 1          | keyboard     | 120   |
    | 2          | mouse        | 80    |
    | 3          | screen       | 600   |
    | 4          | hard disk    | 450   |
    +------------+--------------+-------+
    
    Result table:
    +--------------+------------+----------+------------+
    | product_name | product_id | order_id | order_date |
    +--------------+------------+----------+------------+
    | keyboard     | 1          | 6        | 2020-08-01 |
    | keyboard     | 1          | 7        | 2020-08-01 |
    | mouse        | 2          | 8        | 2020-08-03 |
    | screen       | 3          | 3        | 2020-08-29 |
    +--------------+------------+----------+------------+
    keyboard's most recent order is in 2020-08-01, it was ordered two times this day.
    mouse's most recent order is in 2020-08-03, it was ordered only once this day.
    screen's most recent order is in 2020-08-29, it was ordered only once this day.
    The hard disk was never ordered and we don't include it in the result table.
    
    select product_name,product_id,order_id,order_date
    from
    (
    select product_name ,o.product_id ,order_id,order_date ,
        rank() over(partition by o.product_id order by order_date desc) rk
    from Orders o left join Products p
    on o.product_id =p.product_id 
    )t1
    where rk =1
    order by product_name,product_id,order_id
    

    相关文章

      网友评论

          本文标题:sql刷题笔记(七)

          本文链接:https://www.haomeiwen.com/subject/kxnsyltx.html