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

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


7208d7eca57341ff94b075b5f1f853c3.png


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

14f0ac1659924547869f2dc2739c6e08.png



  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'


0d3bd597e0194a14afe538c938ba09a5.png


  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)


da88849b1fbd4578a8454f4eca95047a.png

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;

09e448880f38441ba1a88bb6a4b07c5b.png



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


6f0eb35efc9a40a885b2f1e0df615a0d.png

  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

38e615beacd94a4f8932997b4fddc4e8.png


EXERCISES 3 FUNCTIONS


  1. Find how many employees have a title of manager without listing the


SELECT 
    COUNT(temp.JOB)
FROM
    (SELECT 
        e.JOB
    FROM
        emp2019284073 e
    WHERE
        e.JOB = 'MANAGER') temp;

bace795fb042405e82bd94ea4fb31bea.png


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

52edbd263f3f416ea66073c0ba6151cc.png


  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

8a912b7221b6473d86e7990af10ad6e9.png

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

eceeae3418d94c0193ddc6edabaa21d2.png

  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;


0493f82ea0d044acb07083838f6b97e1.png



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

ff728eafc66d4b7fa8df1e750ce8a3fe.png



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;

c0ea5ae4599449638ca299c1bd529236.png

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;

a09aba723c234d478ee99beeaea3b29e.png

相关文章
|
8月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS费用价格:MySQL、SQL Server、PostgreSQL和MariaDB引擎收费标准
阿里云RDS数据库支持MySQL、SQL Server、PostgreSQL、MariaDB,多种引擎优惠上线!MySQL倚天版88元/年,SQL Server 2核4G仅299元/年,PostgreSQL 227元/年起。高可用、可弹性伸缩,安全稳定。详情见官网活动页。
1424 152
|
8月前
|
关系型数据库 MySQL 数据库
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎
阿里云数据库RDS支持MySQL、SQL Server、PostgreSQL和MariaDB引擎,提供高性价比、稳定安全的云数据库服务,适用于多种行业与业务场景。
1034 156
|
SQL 存储 关系型数据库
第二篇:关系型数据库的核心概念与 SQL 基础
本篇内容深入浅出地讲解了关系型数据库的核心概念与SQL基础,适合有一定计算机基础的学习者。文章涵盖数据库的基本操作(CRUD)、数据类型、表的创建与管理等内容,并通过实例解析SELECT、INSERT、UPDATE、DELETE等语句的用法。此外,还推荐了多种学习资源与实践建议,帮助读者巩固知识。学完后,你将掌握基础数据库操作,为后续高级学习铺平道路。
755 1
|
11月前
|
SQL 关系型数据库 MySQL
Go语言数据库编程:使用 `database/sql` 与 MySQL/PostgreSQL
Go语言通过`database/sql`标准库提供统一数据库操作接口,支持MySQL、PostgreSQL等多种数据库。本文介绍了驱动安装、连接数据库、基本增删改查操作、预处理语句、事务处理及错误管理等内容,涵盖实际开发中常用的技巧与注意事项,适合快速掌握Go语言数据库编程基础。
1384 213
|
SQL 数据库 数据安全/隐私保护
数据库数据恢复——sql server数据库被加密的数据恢复案例
SQL server数据库数据故障: SQL server数据库被加密,无法使用。 数据库MDF、LDF、log日志文件名字被篡改。 数据库备份被加密,文件名字被篡改。
|
8月前
|
SQL 人工智能 Linux
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
709 5
SQL Server 2025 RC1 发布 - 从本地到云端的 AI 就绪企业数据库
|
7月前
|
SQL 存储 监控
SQL日志优化策略:提升数据库日志记录效率
通过以上方法结合起来运行调整方案, 可以显著地提升SQL环境下面向各种搜索引擎服务平台所需要满足标准条件下之数据库登记作业流程综合表现; 同时还能确保系统稳健运行并满越用户体验预期目标.
399 6
|
8月前
|
关系型数据库 分布式数据库 数据库
阿里云数据库收费价格:MySQL、PostgreSQL、SQL Server和MariaDB引擎费用整理
阿里云数据库提供多种类型,包括关系型与NoSQL,主流如PolarDB、RDS MySQL/PostgreSQL、Redis等。价格低至21元/月起,支持按需付费与优惠套餐,适用于各类应用场景。
|
8月前
|
SQL Oracle 关系型数据库
Oracle数据库创建表空间和索引的SQL语法示例
以上SQL语法提供了一种标准方式去组织Oracle数据库内部结构,并且通过合理使用可以显著改善查询速度及整体性能。需要注意,在实际应用过程当中应该根据具体业务需求、系统资源状况以及预期目标去合理规划并调整参数设置以达到最佳效果。
583 8
|
9月前
|
SQL 人工智能 Java
用 LangChain4j+Ollama 打造 Text-to-SQL AI Agent,数据库想问就问
本文介绍了如何利用AI技术简化SQL查询操作,让不懂技术的用户也能轻松从数据库中获取信息。通过本地部署PostgreSQL数据库和Ollama模型,结合Java代码,实现将自然语言问题自动转换为SQL查询,并将结果以易懂的方式呈现。整个流程简单直观,适合初学者动手实践,同时也展示了AI在数据查询中的潜力与局限。
1242 8