数据库:基础SQL知识+SQL实验2

(1)基础知识:

1.JOIN(连接):

连接操作用于根据指定的条件将两个或多个表中的数据行合并在一起。JOIN 可以根据不同的条件和方式执行,包括等值连接、不等值连接等。

(1)EquiJoin(等值连接):

等值连接是 JOIN 的一种,它基于两个表中的列具有相等值的行进行连接。等值连接使用 = 来比较列的值。特点:连接条件是等值条件,即两个表中的特定列的值必须相等。

SELECT * FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;

Inner Join(内连接) 是一个通用的 JOIN 操作,它可以基于任意连接条件返回两个表中的匹配行。

Equi Join(等值连接) 是内连接的一种特殊形式,它限定连接条件为两个表中的列具有相等值。

SELECT * FROM   Emp E INNER JOIN dept D ON e.Deptno = d.Deptno

Similar to: SELECT *  FROM   emp E, dept D WHERE E.Deptno = d.Deptno;

SELECT Emp.Ename, emp.Deptno, DName FROM Employee INNER JOIN Department      USING(Deptno);

(1.1)Natural Join(自然连接):

自然连接是一种特殊的等值连接,它基于两个表中具有相同列名且相等值的列进行连接。自然连接省略了 ON 子句中的条件。特点:不需要指定连接条件,它自动匹配具有相同列名的列。

SELECT * FROM table1 NATURAL JOIN table2;

(1.2)Left Outer Join(左外连接):

左外连接返回左表中的所有行,以及右表中与左表中的匹配行。如果没有匹配,右表中的列将包含 NULL。特点:返回左表的所有行,与右表匹配的行包含在结果中,没有匹配的部分用 NULL 填充。

SELECT * FROM table1 LEFT OUTER JOIN table2 ON table1.column_name = table2.column_name;

(1.3)Right Outer Join(右外连接):

右外连接返回右表中的所有行,以及左表中与右表中的匹配行。如果没有匹配,左表中的列将包含 NULL。特点:返回右表的所有行,与左表匹配的行包含在结果中,没有匹配的部分用 NULL 填充。

SELECT * FROM table1 RIGHT OUTER JOIN table2 ON table1.column_name = table2.column_name;

(1.4)Full Outer Join(全外连接):

全外连接返回左表和右表中的所有行,如果没有匹配,将使用 NULL 值填充。特点:返回左表和右表的所有行,没有匹配的部分用 NULL 填充。

SELECT * FROM table1 FULL OUTER JOIN table2 ON table1.column_name = table2.column_name;

(2)Theta Join(θ连接):

θ连接是一种使用任意条件连接两个表的一般连接。连接条件可以是任何比较操作,而不仅仅是等值条件。特点:使用任意条件进行连接,条件不限于等值条件。例如:

SELECT * FROM table1, table2 WHERE table1.column_name < table2.column_name;

(3)Cross Join(交叉连接):

是一种 JOIN 操作,它返回两个表的笛卡尔积,即两个表中的每一行与另一个表中的每一行都进行组合。Cross Join 会返回一个新表,其中的行数等于两个表的行数的乘积。

SELECT * FROM table1 CROSS JOIN table2;(Similary to Select * from table1,table2)

2.UNION(结合):

(1)SELECT * FROM table1 UNION SELECT * FROM table2 能够去除重复行。

(2)SELECT * FROM table1 UNION  ALL SELECT * FROM table2 能够保留所有行。

3.FUNCTIONS(部分函数):

  • LOWER: 将字符串转换为小写。

    SELECT LOWER('Hello World') AS LowercaseString; -- 结果:hello world

  • UPPER: 将字符串转换为大写。

    SELECT UPPER('Hello World') AS UppercaseString; -- 结果:HELLO WORLD

  • INITCAP: 将字符串每个单词的首字母转换为大写,其余字母转换为小写。

    SELECT INITCAP('hello world') AS CapitalizedString; -- 结果:Hello World

  • SUBSTR: 提取字符串的子串。

    SELECT SUBSTR('Hello World', 1, 5) AS Substring; -- 结果:Hello

  • LENGTH: 返回字符串的长度。

    SELECT LENGTH('Hello World') AS StringLength; -- 结果:11

  • LPAD: 在字符串左侧填充指定字符。

    SELECT LPAD('123', 5, '0') AS PaddedString; -- 结果:00123

  • RPAD: 在字符串右侧填充指定字符。

    SELECT RPAD('123', 5, '0') AS PaddedString; -- 结果:12300

  • LTRIM: 去除字符串左侧的空格。

    SELECT LTRIM(' Hello') AS TrimmedString; -- 结果:Hello

  • RTRIM: 去除字符串右侧的空格。

    SELECT RTRIM('Hello ') AS TrimmedString; -- 结果:Hello

  • REPLACE: 替换字符串中的子串。

    SELECT REPLACE('Hello World', 'Hello', 'Hi') AS ReplacedString; -- 结果:Hi World

  • CONCAT: 连接两个字符串。

    SELECT CONCAT('Hello', ' World') AS ConcatenatedString; -- 结果:Hello World

  • ROUND: 对数值进行四舍五入。

    SELECT ROUND(3.14159, 2) AS RoundedNumber; -- 结果:3.14

  • TRUNC: 截断数值为指定小数位数。

    SELECT TRUNC(3.14159, 2) AS TruncatedNumber; -- 结果:3.14

  • MONTHS_BETWEEN: 计算两个日期之间的月数差。

    SELECT MONTHS_BETWEEN(TO_DATE('2022-01-01', 'YYYY-MM-DD'), TO_DATE('2022-03-01', 'YYYY-MM-DD')) AS MonthDifference; -- 结果:-2

  • ADD_MONTHS: 在日期上添加指定的月数。

    SELECT ADD_MONTHS(TO_DATE('2022-01-01', 'YYYY-MM-DD'), 2) AS NewDate; -- 结果:2022-03-01

  • NEXT_DAY: 找到指定日期之后的第一个特定工作日。

    SELECT NEXT_DAY(TO_DATE('2022-01-01', 'YYYY-MM-DD'), 'MONDAY') AS NextMonday; -- 结果:2022-01-03

  • TO_DATE: 将字符串转换为日期。

    SELECT TO_DATE('2022-01-01', 'YYYY-MM-DD') AS ConvertedDate; -- 结果:2022-01-01

  • TO_CHAR: 将日期转换为字符串。

    SELECT TO_CHAR(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS FormattedDate; -- 结果:当前日期和时间的格式化字符串

  • NVL: 如果第一个表达式为 NULL,则返回第二个表达式;否则返回第一个表达式。

    SELECT NVL(NULL, 'Default') AS Result; -- 结果:Default

  • NVL2: 如果第一个表达式不为 NULL,则返回第二个表达式;否则返回第三个表达式。

    SELECT NVL2('Value', 'Not Null', 'Null') AS Result; -- 结果:Not Null

  • DECODE: 类似于 CASE 语句,根据条件返回不同的值。

    SELECT DECODE(1, 1, 'One', 2, 'Two', 'Other') AS Result; -- 结果:One

  • SOUNDEX: 返回字符串的 SOUNDEX 值,用于模糊匹配发音相似的字符串。

    SELECT SOUNDEX('hello') AS SoundexValue; -- 结果:H400

  • DATE.FORMAT: 将日期格式化为指定的格式。

    SELECT DATE_FORMAT(SYSDATE, 'YYYY-MM-DD HH24:MI:SS') AS FormattedDate; -- 结果:当前日期和时间的格式化字符串

  • COUNT: 计算行数或满足条件的行数。

    SELECT COUNT(*) AS RowCount FROM table_name; -- 结果:表中的行数

  • AVG: 计算数值列的平均值。

    SELECT AVG(column_name) AS AverageValue FROM table_name; -- 结果:数值列的平均值

  • MAX: 返回数值列的最大值。

    SELECT MAX(column_name) AS MaxValue FROM table_name; -- 结果:数值列的最大值

  • MIN: 返回数值列的最小值。

    SELECT MIN(column_name) AS MinValue FROM table_name; -- 结果:数值列的最小值

  • SUM: 计算数值列的总和。

    SELECT SUM(column_name) AS TotalSum FROM table_name; -- 结果:数值列的总和

(2)实验内容:

【1】JOIN:

1. Find the name and salary of employees in Luton.

2. Join the DEPT table to the EMP table and show in department number order.

3. List the names of all salesmen who work in SALES

4. List all departments that do not have any employees.

5.For each employee whose salary exceeds his manager's salary, list the employee's name and salary and the manager's name and salary.

6.List the employees who have BLAKE as their manager.

7. List all the employee Name and his Manager’s name, even if that employee doesn’t have a manager

【2】FUNCTIONS:

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

2 Compute the average annual salary plus commission for all salesmen

3 Find the highest and lowest salaries and the difference between them (single SELECT statement)

4 Find the number of characters in the longest department name

5 Count the number of people in department 30 who receive a salary and the number of people who receive a commission (single statement).

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

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

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

9.Issue the same query as the previous one except that this time truncate (TRUNC) to the nearest penny rather than round.

【3】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)    

2.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)

3.Which employees were hired in March?

4.Which employees were hired on a Tuesday?

5.Are there any employees who have worked more than 16 years for the company?

6.Show the weekday of the first day of the month in which each employee was hired. (plus their names)

7.Show details of employee hiredates and the date of their first payday. (Paydays occur on the last Friday of each month) (plus their names) 

【4】GROUP BY & HAVING

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

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

3.Issue the same query as above except list the department name rather than the department number.

4 List the average annual salary for all job groups having more than 2 employees in the group.

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

6.Find each department's average annual salary for all its employees except the managers and the president.

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

8. List the ID, Name, number of employee managed by the Manager who manages most employee.

9. List the name of all the Manager who manages atleast 2 employees

【5】SUB QUERIES

1 List the name and job of employees who have the same job as Jones.

2 Find all the employees in Department 10 that have a job that is the same as anyone in department 30.

3 List the name, job, and department of employees who have the same job as Jones or a salary greater than or equal to Ford.

4 Find all employees in department 10 that have a job that is the same as anyone in the Sales department

5 Find the employees located in Liverpool who have the same job as Allen. Return the results in alphabetical order by employee name.

6 Find all the employees that earn more than the average salary of employees in their department.

7 Find all the employees that earn more than JONES, using temporary labels to abbreviate table names.

【6】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)

2 Insert the following data

3 Check that you have created 3 new records in Loans

4 The Loans table must be altered to include another column OUTST NUMBER(8,2)

5 Add 10% interest to all M type loans

6 Remove all loans less than  £3000.00

7 Change the name of loans table to accounts

8 Change the name of column LNO to LOANNO

9 Create a view for use by personnel in department 30 showing employee name, number, job and hiredate

10 Use the view to show employees in department 30 having jobs which are not salesman

11 Create a view which shows summary information of total salary and no of employees for each department.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/598322.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

自制Java镜像发布到dockerhub公网使用

文章目录 问题现象解决制作Java镜像发布使用 问题现象 书接上回&#xff0c;上周处理了一个docker问题&#xff0c;写了篇博客&#xff1a;自定义docker镜像&#xff0c;ubuntu安装命令并导出我们使用谷歌的jib插件打包&#xff0c;详情可以参考这篇文章&#xff1a;Spring Bo…

联想M7400加粉后如何清零

联想M7400黑白激光多功能打印一体机加粉后清零方法&#xff1a; 吴中函 加粉后&#xff0c;确保硒鼓已经被正确安装并且机器已经通电。 1、打开前盖&#xff0c;以便进行后续的操作。 2、按下“清除/返回”键&#xff0c;这会触发一个屏幕提示&#xff1a;提示内容为“更换…

VM安装虚拟机及初始化操作

一、VM下载及暗转 虚拟机指通过软件模拟的具有完整硬件系统功能的、运行在一个完全隔离环境中的完整计算机系统&#xff0c;在实体计算机中能够完成的工作在虚拟机中都能够实现。VMware 是一款功能强大的桌面虚拟计算机软件&#xff0c;提供用户可在单一的桌面上同时运行不同的…

python毕设选题 - flink大数据淘宝用户行为数据实时分析与可视化

文章目录 0 前言1、环境准备1.1 flink 下载相关 jar 包1.2 生成 kafka 数据1.3 开发前的三个小 tip 2、flink-sql 客户端编写运行 sql2.1 创建 kafka 数据源表2.2 指标统计&#xff1a;每小时成交量2.2.1 创建 es 结果表&#xff0c; 存放每小时的成交量2.2.2 执行 sql &#x…

463岛屿周长

题目 给定一个 row x col 的二维网格地图 grid &#xff0c;其中&#xff1a;grid[i][j] 1 表示陆地&#xff0c; grid[i][j] 0 表示水域。 网格中的格子 水平和垂直 方向相连&#xff08;对角线方向不相连&#xff09;。整个网格被水完全包围&#xff0c;但其中恰好有一个…

自动驾驶HWP的功能定义

一、功能定义 高速路自动驾驶功能HWP是指在一般畅通高速公路或城市快速路上驾驶员可以放开双手双脚&#xff0c;同时注意力可在较长时间内从驾驶环境中转移&#xff0c;做一些诸如看手机、接电话、看风景等活动&#xff0c;该系统最低工作速度为60kph。 如上两种不同环境和速度…

力扣题:字符串变换-1.5

力扣题-1.5 [力扣刷题攻略] Re&#xff1a;从零开始的力扣刷题生活 力扣题1&#xff1a;482. 密钥格式化 解题思想&#xff1a;首先先将破折号去除,并将所有字母转换为大写,然后计算第一组的长度,进行结果字符串的拼接,如果第一组的长度为0,则需要删除开头的’-符号 class S…

互联网加竞赛 基于CNN实现谣言检测 - python 深度学习 机器学习

文章目录 1 前言1.1 背景 2 数据集3 实现过程4 CNN网络实现5 模型训练部分6 模型评估7 预测结果8 最后 1 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是 基于CNN实现谣言检测 该项目较为新颖&#xff0c;适合作为竞赛课题方向&#xff0c;学长非常推荐&am…

BUUCTF crypto做题记录(5)新手向

一、传统知识古典密码 加上一个数&#xff0c;就有点移位加密的味道&#xff0c;很有可能就是凯撒加密 辛卯28&#xff0c;癸巳30&#xff0c;丙戌23&#xff0c;辛未8&#xff0c;庚辰17&#xff0c;癸酉10&#xff0c;己卯16&#xff0c;癸巳30 加1之后&#xff0c;28变29&a…

详解编码与调制

编码与调制是现代通信领域的重要概念。在信息传输过程中&#xff0c;编码和调制起着至关重要的作用&#xff0c;它们帮助将数字信号转化为模拟信号&#xff0c;从而实现高效、可靠的数据传输。本文将从编码和调制的基本概念、常见的编码和调制技术以及其在通信领域的应用等方面…

微商城怎么弄才能开通呢?

​微商城的开通&#xff0c;对于许多商家来说&#xff0c;是进入移动电商领域的重要一步。它不仅能帮助你扩大销售渠道&#xff0c;还能让你更好地管理和服务你的客户。那么&#xff0c;微商城怎么弄才能开通呢&#xff1f; 1、注册微信公众号&#xff1a;首先&#xff0c;你需…

麒麟KYLINOS操作系统上扩容系统盘

原文链接&#xff1a;麒麟KYLINOS操作系统上扩容系统盘 hello&#xff0c;大家好啊&#xff01;继之前我们讨论了如何在统信UOS上扩容数据盘之后&#xff0c;今天我要给大家带来的是在麒麟KYLINOS操作系统上扩容系统盘与数据盘的方法。随着数据的不断增长&#xff0c;系统盘或数…

微软 Power Platform 使用Power Automate发送邮件以Dataverse作为数据源的附件File Column

微软Power Platform使用Power Automate发送邮件添加Power Apps以Dataverse作为数据源的附件File Column方式 目录 微软Power Platform使用Power Automate发送邮件添加Power Apps以Dataverse作为数据源的附件File Column方式1、需求背景介绍2、附件列File Column介绍3、如何在Po…

分布式之任务调度Elastic-Job学习二

4 Spring 集成与分片详解 ejob-springboot 工程 4.1 pom 依赖 <properties><elastic-job.version>2.1.5</elastic-job.version> </properties> <dependency><groupId>com.dangdang</groupId><artifactId>elastic-job-lite-…

安全cdn有哪些优势

1. 免备案&#xff1a;在中国大陆地区&#xff0c;进行网站建设需要先进行备案手续&#xff0c;而安全cdn可以避免这一繁琐的步骤&#xff0c;节省时间和精力。 2. 精品线路&#xff1a;安全cdn使用的是覆盖范围更广、速度更快的香港CN2 GIA优化线路。 3. 高速稳定&#xff1a…

在vue3中使用Cesium保姆篇

1.首先新建一个vue项目 Vue.js - 渐进式 JavaScript 框架 | Vue.js 可以直接到管网中查看命令通过npm来创建一个vue3的项目 然后通过命令下载1.99的版本的cesium和plugin npm i cesium1.99 vite-plugin-cesium 下载完了以后 2.引入cesium 首先找到vue的vite.config.js …

[LitCTF 2023]这是什么?SQL !注一下 !

[LitCTF 2023]这是什么&#xff1f;SQL &#xff01;注一下 &#xff01; wp 题目描述&#xff1a;为了安全起见多带了几个套罢了o(▽)q 页面内容&#xff08;往下滑&#xff09;&#xff1a; SQL 语句已给出&#xff0c;无非是更换了闭合方式。 先输个 1 试试&#xff1a; …

【unity】基于Obi的绳/杆蓝图、绳杆区别及其创建方法

绳索 是通过使用距离和弯曲约束将粒子连接起来而形成的。由于规则粒子没有方向(只有位置)&#xff0c;因此无法模拟扭转效应(维基百科)&#xff0c;绳子也无法保持其静止形状。然而&#xff0c;与杆不同的是&#xff0c;绳索可以被撕裂/劈开&#xff0c;并且可以在运行时改变其…

轻量对象存储 LighthouseCOS 用户实践征文

产品使用攻略、上云技术实践&#xff0c;有奖征集&#xff0c;多重好礼等您带回家&#xff5e; 存储桶一键挂载轻量应用服务器&#xff0c;简单易用&#xff0c;腾讯云轻量对象存储用户实践征文活动特惠&#xff1a;腾讯云轻量云专场特惠活动。 投稿说明 1. 注册/登录腾讯云账…

【EI会议征稿通知】第三届智能电网与绿色能源国际学术会议(ICSGGE 2024)

第三届智能电网与绿色能源国际学术会议&#xff08;ICSGGE 2024&#xff09; 2024 3rd International Conference on Smart Grid and Green Energy 2024年第三届智能电网与绿色能源国际学术会议&#xff08;ICSGGE 2024&#xff09;将于2024年4月19-21日在中国成都举行。会议…