zoukankan      html  css  js  c++  java
  • leetcode Sql题目总结 第一弹~

    1.有一个courses 表 ,有: student (学生) 和 class (课程)。

    请列出所有超过或等于5名学生的课。

    select class from courses group by class having count(student)>=5;

    2.

    给定一个 salary 表,如下所示,有 m = 男性 和 f = 女性 的值。交换所有的 f 和 m 值(例如,将所有 f 值更改为 m,反之亦然)。要求只使用一个更新(Update)语句,并且没有中间的临时表。注意,您必只能写一个 Update 语句,请不要编写任何 Select 语句。

    方法1,update salary set sex = case sex when 'm'  then 'f' else 'm' end;

    方法2,update salary set sex = char(ascii('m') + ascii('f') - ascii(sex));

    3.小美是一所中学的信息科技老师,她有一张 seat 座位表,平时用来储存学生名字和与他们相对应的座位 id。其中纵列的 id 是连续递增的,小美想改变相邻俩学生的座位。

    select (case  when mod(id,2)=1 and id=(select count(*) from seat) then id

                            when mod(id,2)=1 then id+1

                            else id-1

                            end ) as id ,student from seat order by id

    4,找出所有影片描述为非 boring (不无聊) 的并且id 为奇数 的影片,结果请按等级 rating 排列

    select * from cinema where description !='boring' and mod(id,2)=1 order by rating desc;

    5.

    X 市建了一个新的体育馆,每日人流量信息被记录在这三列信息中:序号 (id)、日期 (visit_date)、 人流量 (people)。

    请编写一个查询语句,找出人流量的高峰期。高峰期时,至少连续三行记录中的人流量不少于100。

    例如,表 stadium:

    +------+------------+-----------+
    | id | visit_date | people |
    +------+------------+-----------+
    | 1 | 2017-01-01 | 10 |
    | 2 | 2017-01-02 | 109 |
    | 3 | 2017-01-03 | 150 |
    | 4 | 2017-01-04 | 99 |
    | 5 | 2017-01-05 | 145 |
    | 6 | 2017-01-06 | 1455 |
    | 7 | 2017-01-07 | 199 |
    | 8 | 2017-01-08 | 188 |
    +------+------------+-----------+
    对于上面的示例数据,输出为:

    +------+------------+-----------+
    | id | visit_date | people |
    +------+------------+-----------+
    | 5 | 2017-01-05 | 145 |
    | 6 | 2017-01-06 | 1455 |
    | 7 | 2017-01-07 | 199 |
    | 8 | 2017-01-08 | 188 |
    +------+------------+-----------+

    select distinct a.* from stadium a,stadium b,stadium c
    where a.people>=100 and b.people>=100 and c.people>=100
    and (
    (a.id = b.id-1 and b.id = c.id -1) or
    (a.id = b.id-1 and a.id = c.id +1) or
    (a.id = b.id+1 and b.id = c.id +1)
    ) order by a.id

    6.编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。如果不存在第二高的薪水,那么查询应返回 null

    select ifnull((select distinct Salary from Employee order by Salary desc limit 1 offset 1), null ) as SecondHighestSalary;

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/human-traffic-of-stadium
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

  • 相关阅读:
    js分享插件
    json格式
    事物TransactionScope
    CheckBox全选、取消全选
    JQuery中的prop和attr
    [转]javascript之数组操作
    pcntl_fork()函数说明
    从库因为sql错误导致主从同步被中断的问题解决
    查看进程的命令ps
    给mysql创建用户
  • 原文地址:https://www.cnblogs.com/cheng-cheng/p/11344127.html
Copyright © 2011-2022 走看看