美文网首页数据库SQL实战
数据库SQL实战|SQL答案集合及解析(21-30)

数据库SQL实战|SQL答案集合及解析(21-30)

作者: 蓝白绛 | 来源:发表于2019-04-14 01:01 被阅读38次

    牛客数据库SQL实战题(21-30题)

    21、查找所有员工自入职以来的薪水涨幅情况,给出员工编号emp_no以及其对应的薪水涨幅growth,并按照growth进行升序

    CREATE TABLE employees (
    emp_no int(11) NOT NULL,
    birth_date date NOT NULL,
    first_name varchar(14) NOT NULL,
    last_name varchar(16) NOT NULL,
    gender char(1) NOT NULL,
    hire_date date NOT NULL,
    PRIMARY KEY (emp_no));
    
    CREATE TABLE salaries (
    emp_no int(11) NOT NULL,
    salary int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,from_date));
    

    答案

    select now.emp_no, (now.salary-pre.salary) as growth
    from (select em.emp_no, sa.salary 
          from employees as em
          join salaries as sa
          on em.emp_no=sa.emp_no
          where sa.to_date='9999-01-01') as now
    join (select em.emp_no, sa.salary
          from employees as em 
          join salaries as sa 
          on em.emp_no=sa.emp_no 
          where em.hire_date=sa.from_date) as pre
    on now.emp_no=pre.emp_no
    order by growth;
    

    分别建两张表,一张表是emp_no和对应的开始工资,一张表是emp_no和对应的当前工资。然后将两张表连接起来,做减法。

    22、统计各个部门对应员工涨幅的次数总和,给出部门编码dept_no、部门名称dept_name以及次数sum

    CREATE TABLE departments (
    dept_no char(4) NOT NULL,
    dept_name varchar(40) NOT NULL,
    PRIMARY KEY (dept_no));
    
    CREATE TABLE dept_emp (
    emp_no int(11) NOT NULL,
    dept_no char(4) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_no));
    
    CREATE TABLE salaries (
    emp_no int(11) NOT NULL,
    salary int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,from_date));
    

    答案

    select de.dept_no, dename.dept_name, count(sa.salary) as sum
    from (dept_emp as de
    join departments as dename on dename.dept_no=de.dept_no)
    join salaries as sa on de.emp_no=sa.emp_no
    group by de.dept_no;
    

    23、对所有员工的当前(to_date='9999-01-01')薪水按照salary进行按照1-N的排名,相同salary并列且按照emp_no升序排列

    CREATE TABLE salaries (
    emp_no int(11) NOT NULL,
    salary int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,from_date));
    

    答案

    select sa1.emp_no, sa1.salary, count(distinct sa2.salary) as rank
    from salaries as sa1
    left join salaries as sa2
    on sa2.salary>=sa1.salary
    where sa1.to_date='9999-01-01' and sa2.to_date='9999-01-01'
    group by sa1.emp_no
    order by sa1.salary desc, sa1.emp_no;
    

    排序这个字段需要造出一个字段并count,为了构造出rank,我们可以复用salaries表,对salaries1表中的每一个salary,对大于或等于s1.salary的sa.salary进行count。
    参考牛客网,在支持ROW_NUMBER、RANK、DENSE_RANK等函数的SQL Server数据库中,有以下参考代码,可惜在本题的SQLite数据库中不支持。

    SELECT emp_no, salaries, DENSE_RANK() OVER(ORDER BY salary DESC) AS rank
    WHERE to_date ='9999-01-01'
    ORDER BY salary DESC, emp_no ASC
    

    24、获取所有非manager员工当前的薪水情况,给出dept_no、emp_no以及salary ,当前表示to_date='9999-01-01'

    CREATE TABLE dept_emp (
    emp_no int(11) NOT NULL,
    dept_no char(4) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_no));
    
    CREATE TABLE dept_manager (
    dept_no char(4) NOT NULL,
    emp_no int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_no));
    
    CREATE TABLE employees (
    emp_no int(11) NOT NULL,
    birth_date date NOT NULL,
    first_name varchar(14) NOT NULL,
    last_name varchar(16) NOT NULL,
    gender char(1) NOT NULL,
    hire_date date NOT NULL,
    PRIMARY KEY (emp_no));
    
    CREATE TABLE salaries (
    emp_no int(11) NOT NULL,
    salary int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,from_date));
    

    答案

    select dema.dept_no, em.emp_no, sa.salary
    from employees as em
    join dept_emp as de
    on em.emp_no=de.emp_no
    join dept_manager as dema
    on de.dept_no=dema.dept_no
    join salaries as sa
    on em.emp_no=sa.emp_no
    where sa.to_date='9999-01-01' and em.emp_no<>dema.emp_no
    

    25、获取员工其当前的薪水比其manager当前薪水还高的相关信息,当前表示to_date='9999-01-01'

    结果第一列给出员工的emp_no,
    第二列给出其manager的manager_no,
    第三列给出该员工当前的薪水emp_salary,
    第四列给该员工对应的manager当前的薪水manager_salary

    CREATE TABLE dept_emp (
    emp_no int(11) NOT NULL,
    dept_no char(4) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_no));
    
    CREATE TABLE dept_manager (
    dept_no char(4) NOT NULL,
    emp_no int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_no));
    
    CREATE TABLE salaries (
    emp_no int(11) NOT NULL,
    salary int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,from_date));
    

    答案

    select em.emp_no, ma.emp_no as manager_no, sa1.salary as emp_salary, sa2.salary as manager_salary
    from dept_emp as em
    join dept_manager as ma
    on em.dept_no=ma.dept_no 
        and em.to_date='9999-01-01' 
        and ma.to_date='9999-01-01'
        and em.emp_no<>ma.emp_no
    join salaries as sa1
    on em.emp_no=sa1.emp_no and sa1.to_date='9999-01-01'
    join salaries as sa2
    on ma.emp_no=sa2.emp_no and sa2.to_date='9999-01-01' 
    where sa1.salary>sa2.salary
    

    重复利用salary。

    26、汇总各个部门当前员工的title类型的分配数目

    结果给出部门编号dept_no、dept_name、其当前员工所有的title以及该类型title对应的数目count

    CREATE TABLE departments (
    dept_no char(4) NOT NULL,
    dept_name varchar(40) NOT NULL,
    PRIMARY KEY (dept_no));
    
    CREATE TABLE dept_emp (
    emp_no int(11) NOT NULL,
    dept_no char(4) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,dept_no));
    
    CREATE TABLE IF NOT EXISTS titles (
    emp_no int(11) NOT NULL,
    title varchar(50) NOT NULL,
    from_date date NOT NULL,
    to_date date DEFAULT NULL);
    

    答案

    select de.dept_no, name.dept_name, ti.title, count(ti.title)
    from dept_emp as de
    join departments as name
    on de.dept_no=name.dept_no and de.to_date='9999-01-01'
    join titles as ti
    on de.emp_no=ti.emp_no and ti.to_date='9999-01-01'
    group by de.dept_no, ti.title
    

    27、给出每个员工每年薪水涨幅超过5000的员工编号emp_no

    薪水变更开始日期from_date以及薪水涨幅值salary_growth,并按照salary_growth逆序排列。
    提示:在sqlite中获取datetime时间对应的年份函数为strftime('%Y', to_date)

    CREATE TABLE salaries (
    emp_no int(11) NOT NULL,
    salary int(11) NOT NULL,
    from_date date NOT NULL,
    to_date date NOT NULL,
    PRIMARY KEY (emp_no,from_date));
    

    答案

    select s2.emp_no, s2.from_date, (s2.salary - s1.salary) as salary_growth
    from salaries as s1, salaries as s2
    where s1.emp_no = s2.emp_no 
    and salary_growth > 5000
    and (strftime("%Y",s2.to_date) - strftime("%Y",s1.to_date) = 1 
         or strftime("%Y",s2.from_date) - strftime("%Y",s1.from_date) = 1 )
    order by salary_growth desc
    

    每年薪水涨幅的定义并不是非常明确。

    28、查找描述信息中包括robot的电影对应的分类名称以及电影数目,而且还需要该分类对应电影数量>=5部

    CREATE TABLE IF NOT EXISTS film (
    film_id smallint(5)  NOT NULL DEFAULT '0',
    title varchar(255) NOT NULL,
    description text,
    PRIMARY KEY (film_id));
    
    CREATE TABLE category  (
    category_id  tinyint(3)  NOT NULL ,
    name  varchar(25) NOT NULL, 
    last_update timestamp,
    PRIMARY KEY ( category_id ));
    
    CREATE TABLE film_category  (
    film_id  smallint(5)  NOT NULL,
    category_id  tinyint(3)  NOT NULL, 
    last_update timestamp);
    

    答案

    select c.name, count(fc.film_id)
    from (select category_id, count(film_id) as amount
          from film_category
          group by category_id
          having amount>=5) as cc, 
          film as f, category as c, film_category as fc
    where f.description like '%robot%'
    and f.film_id=fc.film_id
    and fc.category_id=c.category_id
    and c.category_id=cc.category_id;
    

    首先,此题所说的该分类对应电影数量>=5,是该分类下所有电影的数量大>=5,而不是筛选了描述中有robot字段之后电影数量依然>=5的分类。
    其次,这题的代码还是让人摸不着头脑,第一行的select c.name, count(fc.film_id)中的count(fc.film_id)很奇怪,还没有group by。用join各表的方法写的代码无法通过。作者才疏学浅,如果你有好的想法请联系我。

    29、使用join查询方式找出没有分类的电影id以及名称

    CREATE TABLE IF NOT EXISTS film (
    film_id smallint(5)  NOT NULL DEFAULT '0',
    title varchar(255) NOT NULL,
    description text,
    PRIMARY KEY (film_id));
    
    CREATE TABLE category  (
    category_id  tinyint(3)  NOT NULL ,
    name  varchar(25) NOT NULL,
    last_update timestamp,
    PRIMARY KEY ( category_id ));
    
    CREATE TABLE film_category  (
    film_id  smallint(5)  NOT NULL,
    category_id  tinyint(3)  NOT NULL, 
    last_update timestamp);
    

    答案

    select f.film_id, f.title
    from film as f
    left join film_category as fc
    on f.film_id=fc.film_id
    where fc.category_id is null;
    

    30、使用子查询的方式找出属于Action分类的所有电影对应的title,description

    CREATE TABLE IF NOT EXISTS film (
    film_id smallint(5)  NOT NULL DEFAULT '0',
    title varchar(255) NOT NULL,
    description text,
    PRIMARY KEY (film_id));
    
    CREATE TABLE category  (
    category_id  tinyint(3)  NOT NULL ,
    name  varchar(25) NOT NULL, `last_update` timestamp,
    PRIMARY KEY ( category_id ));
    
    CREATE TABLE film_category  (
    film_id  smallint(5)  NOT NULL,
    category_id  tinyint(3)  NOT NULL, `last_update` timestamp);
    

    答案

    子查询方式:

    select f.title, f.description
    from film as f
    where f.film_id 
    in (select fc.film_id 
        from film_category as fc 
        where fc.category_id 
        in (select c.category_id 
            from category as c 
            where name='Action'))
    

    非子查询方式:

    select f.title,f.description
    from film as f 
    inner join film_category as fc 
    on f.film_id = fc.film_id
    inner join category as c 
    on c.category_id = fc.category_id
    where c.name = 'Action';
    

    结尾

    如果您发现我的文章有任何错误,或对我的文章有什么好的建议,请联系我!如果您喜欢我的文章,请点喜欢~*我是蓝白绛,感谢你的阅读!

    相关文章

      网友评论

        本文标题:数据库SQL实战|SQL答案集合及解析(21-30)

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