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

Posted 上山打老虎D

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统设计与数据库系统 作业二 Advanced SQL LAB相关的知识,希望对你有一定的参考价值。

Purpose of Experiment purpose and Requirements

Answers to the following questions must include:

  1. SQL Query command ( 60 Points)
  2. Screenshot of your SQL command result (30 Points)
    Note: Oral Question in LAB ( 10 points)

EXERCISES 2 JOINS

  1. Find the name and salary of employees in Luton.
SELECT 
    ENAME, SAL
FROM
    emp2019284073 e
        JOIN
    dept2019284073 d ON e.DEPTNO = d.DEPTNO
WHERE
    LOC = 'LUTON'

在这里插入图片描述

  1. Join the DEPT table to the EMP table and show in department number order.
SELECT 
    *
FROM
    emp2019284073 e
        JOIN
    dept2019284073 d ON e.DEPTNO = d.DEPTNO
ORDER BY e.DEPTNO

在这里插入图片描述

  1. List the names of all salesmen who work in SALES
SELECT 
    ENAME
FROM
    emp2019284073 e
        JOIN
    dept2019284073 d ON e.DEPTNO = d.DEPTNO
WHERE
    JOB = 'SALESMAN' AND DNAME = 'SALES'

在这里插入图片描述

  1. List all departments that do not have any employees.
SELECT 
    dept2019284073.DEPTNO
FROM
    dept2019284073
WHERE
    dept2019284073.DEPTNO NOT IN (SELECT 
            dept2019284073.DEPTNO
        FROM
            dept2019284073
                JOIN
            emp2019284073 ON dept2019284073.DEPTNO = emp2019284073.DEPTNO)

在这里插入图片描述

  1. For each employee whose salary exceeds his manager’s salary, list the employee’s name and salary and the manager’s name and salary.
SELECT 
    worker.ENAME, worker.SAL, manager.ENAME, manager.SAL
FROM
    emp2019284073 worker
        JOIN
    emp2019284073 manager ON worker.MGR = manager.EMPNO
WHERE
    worker.SAL > manager.SAL;

在这里插入图片描述

  1. List the employees who have BLAKE as their manager.
SELECT 
    worker.ENAME, manager.ENAME
FROM
    emp2019284073 worker,
    emp2019284073 manager
WHERE
    worker.MGR = manager.EMPNO
        AND manager.ENAME = 'BLAKE';

在这里插入图片描述

  1. List all the employee Name and his Manager’s name, even if that employee doesn’t have a manager.
SELECT 
    worker.ENAME AS 'workername',
    manager.ENAME AS 'manager name'
FROM
    emp2019284073 worker
        LEFT JOIN
    emp2019284073 manager ON worker.MGR = manager.EMPNO

在这里插入图片描述

EXERCISES 3 FUNCTIONS

  1. Find how many employees have a title of manager without listing them.
SELECT 
    COUNT(temp.JOB)
FROM
    (SELECT 
        e.JOB
    FROM
        emp2019284073 e
    WHERE
        e.JOB = 'MANAGER') temp;

在这里插入图片描述

  1. Compute the average annual salary plus commission for all salesmen.
SELECT 
    AVG(SAL + COMM)
FROM
    emp2019284073

在这里插入图片描述

  1. Find the highest and lowest salaries and the difference between them (single SELECT statement)
SELECT 
    MAX(SAL) AS MAX,
    MIN(SAL) AS MIN,
    MAX(SAL) - MIN(SAL) AS DIFFERENCE
FROM
    emp2019284073

在这里插入图片描述

  1. Find the number of characters in the longest department name.
SELECT 
    MAX(length(LOC)) AS MAXLENGTH
FROM
    dept2019284073

在这里插入图片描述

  1. Count the number of people in department 30 who receive a salary and the number of people who receive a commission (single statement).
SELECT 
    *
FROM
    (SELECT 
        COUNT(*)
    FROM
        (SELECT 
        e.EMPNO
    FROM
        emp2019284073 e
    WHERE
        e.DEPTNO = 30 AND e.SAL IS NOT NULL) temp1) tempa,
    (SELECT 
        COUNT(*)
    FROM
        (SELECT 
        e.EMPNO
    FROM
        emp2019284073 e
    WHERE
        e.SAL IS NOT NULL) temp2) tempb;

在这里插入图片描述

  1. List the average commission of employees who receive a commission, and the average commission of all employees (assume employees who do not receive a commission attract zero commission)
SELECT 
    SUM(COMM) / COUNT(*) AS AVERAGE
FROM
    emp2019284073

在这里插入图片描述

  1. List the average salary of employees that receive a salary, the average commission of employees that receive a commission, the average salary plus commission of only those employees that receive a commission and the average salary plus commission of all employees including those that do not receive a commission. (single statement)
SELECT 
    *
FROM
    (SELECT 
        AVG(SAL)
    FROM
        emp2019284073) temp1,
    (SELECT 
        AVG(COMM)
    FROM
        emp2019284073) temp2,
    (SELECT 
        AVG(COMM + SAL)
    FROM
        emp2019284073
    WHERE
        COMM IS NOT NULL) temp3,
    (SELECT 
        SUM(COMM + SAL)/COUNT(*)
    FROM
        emp2019284073
    WHERE
        COMM IS NOT NULL) temp4;

在这里插入图片描述

  1. Compute the daily and hourly salary for employees in department 30, round to the nearest penny. Assume there are 22 working days in a month and 8 working hours in a day.
SELECT 
    *
FROM
    (SELECT 
        ROUND(SUM(SAL) / COUNT(*) / 22) AS 'daily salary'
    FROM
        emp2019284073
    WHERE
        DEPTNO = 30) temp1,
    (SELECT 
        ROUND(SUM(SAL) / COUNT(*) / 22 / 8) AS 'hourly salary'
    FROM
        emp2019284073
    WHERE
        DEPTNO = 30) temp2;

在这里插入图片描述

  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;

在这里插入图片描述

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;

在这里插入图片描述

  1. 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;

在这里插入图片描述

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

在这里插入图片描述

  1. Which employees were hired on a Tuesday?
SELECT 
    *
FROM
    emp2019284073
WHERE
    DATE_FORMAT(hiredate, '%W') LIKE '%Tuesday%'


在这里插入图片描述

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

在这里插入图片描述

  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)

在这里插入图片描述

  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;

在这里插入图片描述

  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;

在这里插入图片描述

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

在这里插入图片描述

  1. 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

在这里插入图片描述

  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

在这里插入图片描述

  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

在这里插入图片描述
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))

在这里插入图片描述

  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

在这里插入图片描述

  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);

在这里插入图片描述

  1. 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

在这里插入图片描述

  1. List the name of all the Manager who manages at least 2 employees.
SELECT 
    ENAME, NUM
FROM
    (SELECT 
        e1.ENAME, COUNT(*) NUM
    FROM
        emp2019284073 e1
    JOIN emp2019284073 e2 ON e1.EMPNO = e2.MGR
    GROUP BY e1.ENAME) e3
WHERE
    NUM >= 2

在这里插入图片描述

EXERCISES 6 SUB QUERIES.

  1. List the name and job of employees who have the same job as Jones.
SELECT 
    ENAME, JOB
FROM
    emp2019284073
WHERE
    JOB = (SELECT 
            JOB
        FROM
            emp2019284073
        WHERE
            ENAME = 'JONES')
        AND ENAME != 'JONES'

在这里插入图片描述

  1. Find all the employees in Department 10 that have a job that is the same as anyone in department 30.
SELECT 
    e.ENAME
FROM
    emp2019284073 e,
    emp2019284073 m
WHERE
    e.deptno != m.deptno AND e.deptno = 10
        AND m.deptno = 30
        AND e.job = m.job;

在这里插入图片描述

  1. List the name, job, and department of employees who have the same job as Jones or a salary greater than or equal to Ford.
SELECT 
    ename, job, deptno
FROM
    emp2019284073
WHERE
    (job = (SELECT 
            job
        FROM
            emp2019284073
        WHERE
            ename = 'JONES')
        AND ename != 'JONES')
        OR sal >= (SELECT 
            sal
        FROM
            emp2019284073
        WHERE
            ename = 'FORD' AND ename != 'FORD');

在这里插入图片描述

  1. Find all employees in department 10 that have a job that is the same as anyone in the Sales department
SELECT 
    ENAME
FROM
    emp2019284073
WHERE
    deptno = 10
        AND job = (SELECT 
            job
        FROM
            dept2019284073
        WHERE
            dname = 'SALES');

在这里插入图片描述

  1. Find the employees located in Liverpool who have the same job as Allen. Return the results in alphabetical order by employee name.
SELECT 
    ename
FROM
    emp2019284073 e,
    dept2019284073 d
WHERE
    d.loc = 'LIVERPOOL'
        AND e.deptno = d.deptno
        AND job = (SELECT 
            job
        FROM
            emp2019284073
        WHERE
            ename = 'ALLEN')
        AND ename != 'ALLEN'
ORDER BY ename;

在这里插入图片描述

  1. Find all the employees that earn more than the average salary of employees in their department.
SELECT 
    e.ename
FROM
    emp2019284073 e,
    (SELECT 
        deptno, AVG(sal) salVal
    FROM
        emp2019284073
    GROUP BY deptno) d
WHERE
e.deptno = d.deptno AND e.sal > d.salVal;

在这里插入图片描述

  1. Find all the employees that earn more than JONES, using temporary labels to abbreviate table names.
SELECT 
    e.ename
FROM
    emp2019284073 e
WHERE
    sal > (SELECT 
            sal
        FROM
            emp2019284073
        WHERE
            ename = 'JONES');

在这里插入图片描述

  1. List the Name of all employees who earn Highest salary and Second Highest salary.
SELECT 
    ENAME
FROM
    (SELECT 
        ENAME
    FROM
        emp2019284073
    ORDER BY SAL DESC) a
LIMIT 2

在这里插入图片描述

EXERCISES 7 Data Manipulation

  1. Create a new table called loans with columns named LNO NUMBER (3), EMPNO NUMBER (4), TYPE CHAR (1), AMNT NUMBER (8,2), Create all constraints, such as Primary Key, Foreign Key, Check
CREATE TABLE loan2019284073 (
    LNO DECIMAL(3) PRIMARY KEY,
    EMPNO DECIMAL(4),
    TYPE CHAR(1),
    AMNT DECIMAL(8 , 2 )
)

  1. Insert the following data
LNOEMPNOTYPEAMNT
237499M20000.00
427499C2000.00
657844M3564.00
INSERT INTO loan2019284073 VALUES(23,7499,'M',20000.00);
INSERT INTO loan2019284073 VALUES(42,7499,'C',2000.00);
INSERT INTO loan2019284073 VALUES(65,7844,'M',3564.00);

  1. Check that you have created 3 new records in Loans
SELECT 
    *
FROM
    loan2019284073;

在这里插入图片描述

  1. The Loans table must be altered to include another column OUTST NUMBER (8,2)
ALTER TABLE loan2019284073 ADD(OUTSET DECIMAL(8,2))
  1. Add 10% interest to all M type loans
UPDATE loan2019284073 
SET 
    AMNT = AMNT * 1.1
WHERE
    TYPE = 'M' AND LNO > 0;

在这里插入图片描述

  1. Remove all loans less than £3000.00
DELETE FROM loan2019284073 
WHERE
    AMNT < 3000 AND LNO > 0;

在这里插入图片描述

  1. Change the name of loans table to accounts
ALTER TABLE loan2019284073 RENAME ACCOUNT2019284073;
  1. Change the name of column LNO to LOANNO
ALTER TABLE `dong2019284073`以上是关于系统设计与数据库系统 作业二 Advanced SQL LAB的主要内容,如果未能解决你的问题,请参考以下文章

201621123018《java程序设计》第14周作业总结

作业五之系统设计时所实现的质量属性战术

团队作业4—项目系统设计与数据库设计

系统设计与数据库系统 作业三 Normalisation

基于asp.net作业批改及提交系统的设计与实现.rar(毕业设计+毕业论文+开题报告+答辩PPT)

系统分析与设计——作业5