数据分析学习笔记

练习SQL利器,牛客网SQL实战题库,57~61题

2019-04-09  本文已影响1人  今天有觉悟1

57.使用含有关键字exists查找未分配具体部门的员工的所有信息

SELECT *
FROM employees
WHERE NOT EXISTS(SELECT emp_no
                FROM dept_emp
                WHERE emp_no = employees.emp_no)

58.获取employees中的行数据,且这些行也存在于emp_v中

SELECT em.*
FROM employees AS em, emp_v AS ev
WHERE em.emp_no = ev.emp_no

59.获取有奖金的员工相关信息

SELECT em.emp_no, em.first_name, em.last_name, eb.btype, sal.salary, 
(CASE eb.btype 
 WHEN 1 THEN sal.salary * 0.1
 WHEN 2 THEN sal.salary * 0.2
 ELSE sal.salary * 0.3 END) AS bonus
FROM employees AS em INNER JOIN emp_bonus AS eb
ON em.emp_no = eb.emp_no
INNER JOIN salaries AS sal
ON em.emp_no = sal.emp_no
AND sal.to_date = '9999-01-01'

注:最好自己做一个连接图表示表与表之间的联系,这道题很明显没用到dept_emp表。

60.统计salary的累计和running_total

SELECT s1.emp_no, s1.salary, 
(SELECT SUM(s2.salary)
FROM salaries AS s2 
WHERE s2.emp_no <= s1.emp_no
AND s2.to_date = '9999-01-01') AS running_total
FROM salaries AS s1
WHERE s1.to_date = '9999-01-01'
ORDER BY s1.emp_no;

61.对于employees表中,给出奇数行first_name

SELECT first_name FROM ( 
    SELECT e2.first_name,
    (SELECT COUNT(*) 
     FROM employees e1
     WHERE e1.first_name <= e2.first_name ) AS rownum 
    FROM employees e2 
    WHERE rownum % 2 =1 
);
上一篇下一篇

猜你喜欢

热点阅读