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

简介: 系统设计与数据库系统 作业二 Advanced SQL LAB(三)
  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


16ee9d272de54d8d9adf15ad2f342a36.png

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'

af70f13b435a4114b2148e582acd3691.png

  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;


2d012b6044f4416eb1784708ac5d12a7.png

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


c3a3b3cd8a30417688fd0e91f3679c48.png

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

7f9bf0479b204d3d98716c3b1c4ef66a.png

  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;

96412d3789e848b69efcaf1fc678bee1.png

  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;

9e4d898a260740faace29b8549040814.png

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


0b8a2f470b994b9cbf6df78cb7c5292f.png


  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

83763574f58645a39b3c3f2f8ae012dd.png


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 )
)
  1. Insert the following data


5111d677da3e46bca11de7fc4e79ccdf.png

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;

9e5f684d1244413dbb4ad121f928c9d6.png

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

50771b68758b4feb857c6d0843f87038.png


  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`.`account2019284073` CHANGE COLUMN `LNO` `LOANNO` DECIMAL(3,0) NOT NULL ;

f1703a330bec4082a8a1ab7b80e84258.png


  1. Create a view for use by personnel in department 30 showing employee name, number, job and hiredate.
CREATE VIEW view2019284073 AS
    SELECT 
        ENAME, EMPNO, JOB, hiredate
    FROM
        emp2019284073
    WHERE
        deptno = 30


4574d17bc8e7432c9323f3a106e6e992.png


  1. Create a view which shows summary information for each department.
CREATE VIEW view2019284073 AS
    SELECT 
        *
    FROM
        dept2019284073

b202ced5f6814cfdbcc1c5040dea398c.png

Experiment process and content


From this experiment of MySQL, I learned how to select different kinds of data from a database, how to edit values by language of MySQL. I also learn the use of “JOIN”, “GROUP BY”, “HAVING”. Now, I can write some uncomplex functions to show data.


During this experiment, errors occur sometimes. With the help of my

classmates and knowledge in Internet, I finally finished this experiment

successfully. For example, in Exercise7 question 5 and question6, the select don’t need to edit or compare the primary key, but in safe mode, the comparison of primary key is necessary. Thus, the script “LNO > 0” is necessary.

相关文章
|
3月前
|
SQL 开发框架 .NET
ASP.NET连接SQL数据库:详细步骤与最佳实践指南ali01n.xinmi1009fan.com
随着Web开发技术的不断进步,ASP.NET已成为一种非常流行的Web应用程序开发框架。在ASP.NET项目中,我们经常需要与数据库进行交互,特别是SQL数据库。本文将详细介绍如何在ASP.NET项目中连接SQL数据库,并提供最佳实践指南以确保开发过程的稳定性和效率。一、准备工作在开始之前,请确保您
345 3
|
29天前
|
SQL Oracle 数据库
使用访问指导(SQL Access Advisor)优化数据库业务负载
本文介绍了Oracle的SQL访问指导(SQL Access Advisor)的应用场景及其使用方法。访问指导通过分析给定的工作负载,提供索引、物化视图和分区等方面的优化建议,帮助DBA提升数据库性能。具体步骤包括创建访问指导任务、创建工作负载、连接工作负载至访问指导、设置任务参数、运行访问指导、查看和应用优化建议。访问指导不仅针对单条SQL语句,还能综合考虑多条SQL语句的优化效果,为DBA提供全面的决策支持。
68 11
|
2月前
|
SQL 缓存 监控
大厂面试高频:4 大性能优化策略(数据库、SQL、JVM等)
本文详细解析了数据库、缓存、异步处理和Web性能优化四大策略,系统性能优化必知必备,大厂面试高频。关注【mikechen的互联网架构】,10年+BAT架构经验倾囊相授。
大厂面试高频:4 大性能优化策略(数据库、SQL、JVM等)
|
1月前
|
SQL 关系型数据库 MySQL
MySQL导入.sql文件后数据库乱码问题
本文分析了导入.sql文件后数据库备注出现乱码的原因,包括字符集不匹配、备注内容编码问题及MySQL版本或配置问题,并提供了详细的解决步骤,如检查和统一字符集设置、修改客户端连接方式、检查MySQL配置等,确保导入过程顺利。
|
1月前
|
SQL 监控 安全
SQL Servers审核提高数据库安全性
SQL Server审核是一种追踪和审查SQL Server上所有活动的机制,旨在检测潜在威胁和漏洞,监控服务器设置的更改。审核日志记录安全问题和数据泄露的详细信息,帮助管理员追踪数据库中的特定活动,确保数据安全和合规性。SQL Server审核分为服务器级和数据库级,涵盖登录、配置变更和数据操作等事件。审核工具如EventLog Analyzer提供实时监控和即时告警,帮助快速响应安全事件。
|
2月前
|
SQL 关系型数据库 MySQL
体验使用DAS实现数据库SQL优化,完成任务可得羊羔绒加厚坐垫!
本实验介绍如何通过数据库自治服务DAS对RDS MySQL高可用实例进行SQL优化,包含购买RDS实例并创建数据库、数据导入、生成并优化慢SQL、执行优化后的SQL语句等实验步骤。完成任务,即可领取羊羔绒加厚坐垫,限量500个,先到先得。
211 13
|
2月前
|
SQL 存储 BI
gbase 8a 数据库 SQL合并类优化——不同数据统计周期合并为一条SQL语句
gbase 8a 数据库 SQL合并类优化——不同数据统计周期合并为一条SQL语句
|
2月前
|
SQL 数据库
gbase 8a 数据库 SQL优化案例-关联顺序优化
gbase 8a 数据库 SQL优化案例-关联顺序优化
|
2月前
|
SQL 存储 Linux
从配置源到数据库初始化一步步教你在CentOS 7.9上安装SQL Server 2019
【11月更文挑战第16天】本文介绍了在 CentOS 7.9 上安装 SQL Server 2019 的详细步骤,包括配置系统源、安装 SQL Server 2019 软件包以及数据库初始化,确保 SQL Server 正常运行。
110 4
|
2月前
|
SQL 存储 Linux
从配置源到数据库初始化一步步教你在CentOS 7.9上安装SQL Server 2019
【11月更文挑战第8天】本文介绍了在 CentOS 7.9 上安装 SQL Server 2019 的详细步骤,包括系统准备、配置安装源、安装 SQL Server 软件包、运行安装程序、初始化数据库以及配置远程连接。通过这些步骤,您可以顺利地在 CentOS 系统上部署和使用 SQL Server 2019。
139 1