系统设计与数据库系统 作业二 Advanced SQL LAB(二)

简介: 系统设计与数据库系统 作业二 Advanced SQL LAB(二)
  1. Issue the same query as the previous one except that this time truncate (TRUNC) to the nearest penny rather than round.
SELECT 
    *
FROM
    (SELECT 
        FLOOR(SUM(SAL) / COUNT(*) / 22) AS 'daily salary'
    FROM
        emp2019284073
    WHERE
        DEPTNO = 30) temp1,
    (SELECT 
        FLOOR(SUM(SAL) / COUNT(*) / 22 / 8) AS 'hourly salary'
    FROM
        emp2019284073
    WHERE
        DEPTNO = 30) temp2;

5e019ac07a6e49cb8db5f2c46e9bc463.png

EXERCISES 4 DATES


  1. Select the name, job, and date of hire of the employees in department 20. (Format the hiredate column using a picture MM/DD/YY)
SELECT 
    ename, job, DATE_FORMAT(hiredate, '%M-%D-%Y')
FROM
    emp2019284073
WHERE
    deptno = 20;

f5784f278df045ffbd76d96b0b714b6f.png


Use a picture to format hiredate as DAY(day of the week), MONTH (name of the month, ) DD (day of the month) and YYYY(year)


SELECT 
    ename, job, DATE_FORMAT(hiredate, '%a-%M-%D-%Y')
FROM
    emp2019284073
WHERE
    deptno = 20;

47279168aa5e419f8fbb0563d4e9f655.png


  1. Which employees were hired in March?
SELECT 
    *
FROM
    emp2019284073
WHERE
    DATE_FORMAT(hiredate, '%M') LIKE '%March%'

a46dbff04abe4dd0a4183ba0433e5bd7.png


  1. Which employees were hired on a Tuesday?


SELECT 
    *
FROM
    emp2019284073
WHERE
    DATE_FORMAT(hiredate, '%W') LIKE '%Tuesday%'

722efd3543bb411b969ed13421a7a7a6.png


  1. Are there any employees who have worked more than 16 years for the company?
SELECT 
    *
FROM
    emp2019284073
WHERE
    TIMESTAMPDIFF(YEAR, hiredate, NOW()) > 16

ff36f64f6e1d482aae5b98cdd4a86c85.png



  1. Show the weekday of the first day of the month in which each employee was hired. (plus their names)
(SELECT 
    ENAME,
    WEEKDAY(DATE_ADD(HIREDATE,
                INTERVAL - DAY(HIREDATE) + 1 DAY))
FROM
    emp2019284073)


46bbb8efb146426abc65125e60c9e49b.png


  1. Show details of employee hiredates and the date of their first payday. (Paydays occur on the last Friday of each month) (plus their names)
set global log_bin_trust_function_creators=TRUE;
DELIMITER $$
DROP FUNCTION IF EXISTS last_friday $$
CREATE FUNCTION last_friday (dt DATE) RETURNS DATE
BEGIN
DECLARE last DATE;
SET last=LAST_DAY(dt);
WHILE DATE_FORMAT(last, "%W")!='Friday' DO
SET last=DATE_SUB(last, interval 1 day);
END WHILE;
RETURN last;
END $$
SELECT 
    ENAME, HIREDATE, LAST_FRIDAY(HIREDATE) first_payday
FROM
    emp2019284073;

46bbb8efb146426abc65125e60c9e49b.png


  1. Refine your answer to 7 such that it works even if an employee is hired after the last Friday of the month (cf Martin)
set global log_bin_trust_function_creators=TRUE;
DELIMITER $$
DROP FUNCTION IF EXISTS last_friday $$
CREATE FUNCTION last_friday (dt DATE) RETURNS DATE
BEGIN
DECLARE last DATE;
SET last=LAST_DAY(dt);
WHILE DATE_FORMAT(last, "%W")!='Friday' DO
SET last=DATE_SUB(last, interval 1 day);
END WHILE;
RETURN last;
END $$
SELECT 
    ENAME, HIREDATE, LAST_FRIDAY(HIREDATE) first_payday
FROM
    emp2019284073;

02170be89af243b29b64ec375340c0cb.png


EXERCISES 5 GROUP BY & HAVING


  1. List the department number and average salary of each department.


SELECT 
    DEPTNO, AVG(SAL)
FROM
    emp2019284073
GROUP BY DEPTNO


cefc6c50a7654d66b4ce78a348abcfde.png

Divide all employees into groups by department and by job within department. Count the employees in each group and compute each group’s average annual salary.

SELECT 
    DEPTNO, JOB, COUNT(ENAME), AVG(SAL)
FROM
    emp2019284073
GROUP BY DEPTNO , JOB

9e2d07ed915e4a89a824bbea295a698c.png

  1. Issue the same query as above except list the department name rather than the department number.
SELECT 
    DNAME, JOB, COUNT(*), AVG(SAL)
FROM
    (SELECT 
        *
    FROM
        emp2019284073 e
    JOIN dept2019284073 d USING (DEPTNO)) a
GROUP BY DNAME , JOB

fc0a2d8d13804dea8e7145f972508102.png

  1. List the average annual salary for all job groups having more than 2 employees in the group.
SELECT 
    JOB, AVG(SAL)
FROM
    emp2019284073
GROUP BY JOB
HAVING COUNT(*) > 2

886b4683d5784efeb500e8ba239b5d52.png

5. Find all departments with an average commission greater than 25% of average salary.

SELECT 
    DEPTNO
FROM
    emp2019284073
GROUP BY DEPTNO
HAVING (AVG(COMM) > 0.25 * AVG(SAL))

301e8722d81443428fa6aff5248e6738.png

  1. Find each department’s average annual salary for all its employees except the managers and the president.
SELECT 
    DEPTNO, COUNT(*), AVG(SAL)
FROM
    emp2019284073
WHERE
    JOB != 'MANAGER' AND JOB != 'PRESIDENT'
GROUP BY DEPTNO

adb564efa93843068896ac2eaf480481.png

  1. List the Department ID and Name where there are at least one Manager and two clerks.
SELECT 
    DEPTNO, DNAME
FROM
    dept2019284073
WHERE
    DEPTNO = ANY (SELECT 
            DEPTNO
        FROM
            emp2019284073
        WHERE
            JOB = 'MANAGER'
        GROUP BY DEPTNO , JOB
        HAVING COUNT(*) >= 1)
        AND DEPTNO = ANY (SELECT 
            DEPTNO
        FROM
            emp2019284073
        WHERE
            JOB = 'CLERK'
        GROUP BY DEPTNO , JOB
        HAVING COUNT(*) >= 2);

676686a9dae7438eadfbe6971ea6c88d.png

List the Department ID and Name where there are at least one Manager and two clerk and whose average salary is greater that the company’s average salary.

SELECT 
    DEPTNO, DNAME
FROM
    dept2019284073
WHERE
    DEPTNO = ANY (SELECT 
            DEPTNO
        FROM
            emp2019284073
        WHERE
            JOB = 'MANAGER'
        GROUP BY DEPTNO , JOB
        HAVING COUNT(*) >= 1)
        AND DEPTNO = ANY (SELECT 
            DEPTNO
        FROM
            emp2019284073
        WHERE
            JOB = 'CLERK'
        GROUP BY DEPTNO , JOB
        HAVING COUNT(*) >= 2)
        AND DEPTNO = ANY (SELECT 
            DEPTNO
        FROM
            emp2019284073
        GROUP BY DEPTNO , JOB
        HAVING AVG(SAL) > (SELECT 
                AVG(SAL)
            FROM
                emp2019284073))
  1. List the name of the Manager who manages most employee
SELECT 
    ENAME, NUM
FROM
    (SELECT 
        e1.ENAME, COUNT(*) NUM
    FROM
        emp2019284073 e1
    JOIN emp2019284073 e2 ON e1.EMPNO = e2.MGR
    GROUP BY e1.ENAME
    ORDER BY COUNT(*) DESC) e3
LIMIT 1

948ccc5c326349fa987f8209cb781cc3.png

相关文章
|
8月前
|
JavaScript Java 测试技术
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
大学生体质测试|基于Springboot+vue的大学生体质测试管理系统设计与实现(源码+数据库+文档)
126 0
|
5月前
|
SQL 关系型数据库 数据库
【python】python社交交友平台系统设计与实现(源码+数据库)【独一无二】
【python】python社交交友平台系统设计与实现(源码+数据库)【独一无二】
184 10
|
7月前
|
SQL 存储 运维
Flink⼤状态作业调优实践指南:Flink SQL 作业篇
本文整理自俞航翔、陈婧敏、黄鹏程老师所撰写的大状态作业调优实践指南。由于内容丰富,本文中篇内容分享 Flink SQL 作业大状态导致反压的调优原理与方法。
69805 7
Flink⼤状态作业调优实践指南:Flink SQL 作业篇
|
6月前
|
关系型数据库 MySQL Go
mysql,数据库Server returns invalid timezone. Go to ‘Advanced‘ tab 数据库的行和列没有出现怎么办p
mysql,数据库Server returns invalid timezone. Go to ‘Advanced‘ tab 数据库的行和列没有出现怎么办p
|
8月前
|
JavaScript Java 关系型数据库
大学生就业招聘|基于Springboot和vue的大学生就业招聘系统设计与实现(源码+数据库+文档)
大学生就业招聘|基于Springboot和vue的大学生就业招聘系统设计与实现(源码+数据库+文档)
121 1
|
7月前
|
关系型数据库 MySQL 数据库
MySQL数据库作业设计之豆瓣音乐
MySQL数据库作业设计之豆瓣音乐
39 0
|
7月前
|
关系型数据库 MySQL 项目管理
数据库大作业——基于qt开发的图书管理系统(四)项目目录的整理与绘制登录页面
数据库大作业——基于qt开发的图书管理系统(四)项目目录的整理与绘制登录页面
|
7月前
|
SQL 关系型数据库 MySQL
数据库大作业——基于qt开发的图书管理系统(三)Qt连接Mysql数据库
数据库大作业——基于qt开发的图书管理系统(三)Qt连接Mysql数据库
168 0
|
7月前
|
SQL 数据可视化 关系型数据库
数据库大作业——基于qt开发的图书管理系统(二) 相关表结构的设计
数据库大作业——基于qt开发的图书管理系统(二) 相关表结构的设计