系统设计与数据库系统 作业二 Advanced SQL LAB
Posted 上山打老虎D
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了系统设计与数据库系统 作业二 Advanced SQL LAB相关的知识,希望对你有一定的参考价值。
Advanced SQL command LAB
Purpose of Experiment purpose and Requirements
Answers to the following questions must include:
- SQL Query command ( 60 Points)
- Screenshot of your SQL command result (30 Points)
Note: Oral Question in LAB ( 10 points)
EXERCISES 2 JOINS
- 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'
- 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
- 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'
- 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)
- 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;
- 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';
- 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
- 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;
- Compute the average annual salary plus commission for all salesmen.
SELECT
AVG(SAL + COMM)
FROM
emp2019284073
- 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
- Find the number of characters in the longest department name.
SELECT
MAX(length(LOC)) AS MAXLENGTH
FROM
dept2019284073
- 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;
- 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
- 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;
- 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;
- 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
- 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;
- 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;
- Which employees were hired in March?
SELECT
*
FROM
emp2019284073
WHERE
DATE_FORMAT(hiredate, '%M') LIKE '%March%'
- Which employees were hired on a Tuesday?
SELECT
*
FROM
emp2019284073
WHERE
DATE_FORMAT(hiredate, '%W') LIKE '%Tuesday%'
- Are there any employees who have worked more than 16 years for the company?
SELECT
*
FROM
emp2019284073
WHERE
TIMESTAMPDIFF(YEAR, hiredate, NOW()) > 16
- 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)
- 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;
- 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
- List the department number and average salary of each department.
SELECT
DEPTNO, AVG(SAL)
FROM
emp2019284073
GROUP BY DEPTNO
- 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
- 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
- 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))
- 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
- 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);
- 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))
- 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
- 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.
- 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'
- 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;
- 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');
- 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');
- 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;
- 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;
- 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');
- 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
- 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 )
)
- Insert the following data
LNO | EMPNO | TYPE | AMNT |
---|---|---|---|
23 | 7499 | M | 20000.00 |
42 | 7499 | C | 2000.00 |
65 | 7844 | M | 3564.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);
- Check that you have created 3 new records in Loans
SELECT
*
FROM
loan2019284073;
- The Loans table must be altered to include another column OUTST NUMBER (8,2)
ALTER TABLE loan2019284073 ADD(OUTSET DECIMAL(8,2))
- Add 10% interest to all M type loans
UPDATE loan2019284073
SET
AMNT = AMNT * 1.1
WHERE
TYPE = 'M' AND LNO > 0;
- Remove all loans less than £3000.00
DELETE FROM loan2019284073
WHERE
AMNT < 3000 AND LNO > 0;
- Change the name of loans table to accounts
ALTER TABLE loan2019284073 RENAME ACCOUNT2019284073;
- Change the name of column LNO to LOANNO
ALTER TABLE `dong2019284073`以上是关于系统设计与数据库系统 作业二 Advanced SQL LAB的主要内容,如果未能解决你的问题,请参考以下文章
201621123018《java程序设计》第14周作业总结