数据库:基础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.

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

SAP BAPI 客户主数据创建:cmd_ei_api=>maintain_bapi

BAPI函数&#xff1a;cmd_ei_api>maintain_bapi 事物代码&#xff1a;XD01/XD02 客户主数据创建、修改、拓展功能开发 数据结构定义&#xff1a; 基本视图信息 公司代码信息结构&#xff1a; 销售视图信息结构: 客户主数据税分类信息结构&#xff1a; 代码参考 详细代码…

C++面向对象编程与泛型编程(GP)

C既支持面向对象编程&#xff0c;又支持泛型编程 1.面向对象编程 将数据结构与处理方法&#xff08;容器与算法&#xff09;组成对象封装在一个类中&#xff0c;通过类的封装隐藏内部细节&#xff0c;可以使用继承&#xff0c;多态等方法。 注意&#xff1a;list容器本身带有…

计算机毕业设计——SpringBoot 招投标 任务发布网站(附源码)

1&#xff0c;绪论 在市场范围内&#xff0c;任务发布网站很受欢迎&#xff0c;有很多开发者以及其他领域的牛人&#xff0c;更倾向于选择工作时间、工作场景更自由的零工市场寻求零散单子来补贴家用。 如今市场上&#xff0c;任务发布网站鱼龙混杂&#xff0c;用户需要找一个…

CH341StreamI2C参数解释

总体思路&#xff1a; 第一步&#xff1a;使用EEPROM写入相应的数据 第二步&#xff1a;使用EEPROM读取相应的数据 第三步&#xff1a;使用CH341StreamI2C函数还原读取过程 每一步需要逻辑分析仪进行对比。 第一步&#xff1a;数据写入CH341WriteEEPROM 使用CH341WriteE…

性能测试之Mysql数据库调优

一、前言 性能调优前提&#xff1a;无监控不调优&#xff0c;对于mysql性能的监控前几天有文章提到过&#xff0c;有兴趣的朋友可以去看一下 二、Mysql性能指标及问题分析和定位 1、我们在监控图表中关注的性能指标大概有这么几个&#xff1a;CPU、内存、连接数、io读写时间…

【网络安全】【密码学】常见数据加(解)密算法及Python实现(一)

一、Base64编码 1、算法简介 Base64是一种常见的编&#xff08;解&#xff09;码方法&#xff0c;用于传输少量二进制数据。该编码方式较为简短&#xff0c;并不具有可读性&#xff0c;对敏感数据可以起到较好的保护作用。 2、Python实现&#xff08;调库&#xff09; &…

每日一道算法题day-one(备战蓝桥杯)

从今天开始博主会每天做一道算法题备战蓝桥杯&#xff0c;并分享博主做题的思路&#xff0c;有兴趣就加入我把&#xff01; 算法题目&#xff1a; 有一个长度为 N 的字符串 S &#xff0c;其中的每个字符要么是 B&#xff0c;要么是 E。 我们规定 S 的价值等于其中包含的子…

C++模板(泛型)

1. 模板 1.1 知识点 模板&#xff1a;template 泛型编程&#xff1a; 是指数据的类型是广泛&#xff0c;任意的数据类型 模板&#xff1a;可以将一个函数或类描述成一个模板&#xff0c;例如&#xff1a;画画&#xff0c;给一个人物模型上色彩&#xff0c;根据用户上的色彩是什…

Bean如何诞生与消亡:生命周期探秘【beans 二】

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Bean如何诞生与消亡&#xff1a;生命周期探秘【beans 二】 前言bean的创建过程bean的初始化阶段1. 实现InitializingBean接口&#xff1a;2. 使用PostConstruct注解&#xff1a; bean的属性注入1. Set…

视频倒放软件,看视频如何演绎“逆袭”之旅

你是否厌倦了日复一日的平淡生活&#xff0c;渴望时光倒流&#xff0c;重温那些逝去的精彩瞬间&#xff1f;在数字技术的世界里&#xff0c;这样的愿望或许不再遥不可及。视频倒放仿佛让时光倒流&#xff0c;给我们的视觉带来了全新的冲击&#xff0c;今天&#xff0c;让我们一…

新手第一次在linux上用git上传代码到仓库全过程

目录 背景&#xff1a; 过程&#xff1a; -1.去github.com自己的账号先建个仓库repository 0.命令行输入 git version 看下有无安装git 1.git init 初始化了一个Git仓库&#xff0c;你可以 ls -a 看见这个隐藏的目录 3.git add . 添加要上传的文件到Git的暂存区&#xff0…

windows对微信及小程序抓包:Burp+Fiddler+Proxifier

本文由掌控安全学院 - zbs 投稿 话不多说&#xff0c;直接先上个效果图&#xff1a; 新新的版本哈&#xff1b; 好好的抓包哈&#xff1b; 然后直接说我如何配置的&#xff1a; 准备好三个工具&#xff1a;bp、fiddler、proxifier【也可以用其他的进行代理】 bp、proxifie…

ELement UI时间控件el-date-picker误差8小时解决办法

一、问题描述&#xff1a; 在项目中引用了elementui中的date-picker组件&#xff0c;选中的时间跟实际相差八小时&#xff0c;且格式不是自己想要的格式 <el-date-pickertype"date"placeholder"选择日期"format"yyyy/M/d"v-model"form…

R304S 指纹识别模块的硬件接口说明

一.外部接口尺寸图 二.串行通讯 R304S 指纹模块通讯接口定义&#xff1a; 引脚号名称定义描述15V电源输入电源正输入端 DC 4.2--6V2GND电源和信号地电源和信号地3TXD数据发送串行数据输出&#xff0c;TTL 逻辑电平4RXD数据接收串行数据输入&#xff0c;TTL 逻辑电平 三.USB通…

EtherCAT的COE报文

本文主要用于记录工作中需要学习的内容&#xff0c;如有冒犯请私信&#xff01; COE协议 下面我们介绍以下CANOpen在EtherCAT中的应用。 COE的对象字典 COE协议是完全遵循CANopen协议的&#xff0c;但针对EtherCAT通信做了一些扩展&#xff0c;索引为0x1c00~0x1c4f&#xff0…

Java中关键词strictfp有什么作用?

在Java中&#xff0c;关键词strictfp用于声明一个方法、类或接口是严格遵守浮点数计算规范的。 具体作用包括&#xff1a; 保证浮点数计算的结果在不同平台上是一致的&#xff0c;避免由于浮点数计算的不精确性导致的结果不确定性。 指定了严格的浮点数计算规则&#xff0c;禁…

LeetCode刷题---矩阵置零

解题思路&#xff1a; 本题要求原地置换元素 对矩阵进行第一轮遍历&#xff0c;使用第一行第一列来充当该行该列是否要置换为0的标记位&#xff0c;如果第一行或第一列本身就含有零元素&#xff0c;我们使用colZero和rowZero变量来对其标记。如果第i行第j列的那个元素为0&#…

产品经理如何选择城市?

年底&#xff0c;全国性的人口大迁徙即将开始。选择城市&#xff0c;堪称年轻人的“二次投胎”&#xff0c;族望留原籍&#xff0c;家贫走他乡。 古人在选择城市时&#xff0c;主要的考量因素是家族势力&#xff0c;这一点放在当代&#xff0c;大致也成立&#xff0c;如果在老…

Cell 文章图复现

多组差异火山图复现 参考文章: A Spatiotemporal Organ-Wide Gene Expression and Cell Atlas of the Developing Human Heart Figure 2. H 图里主要是单细胞数据不同cluster之间的差异火山图, 所以说白了就是散点图和柱状图的结合, 散点图用差异基因绘制, 柱状图利用logFC最…

关于MIPS上手应知应会-如何把C语言改写为MIPS!

文章目录 寄存器指令使用技巧翻译C/Cif/else语句switch语句for循环while 循环do...while循环一维数组定义与使用二维数组定义与使用例 &#xff1a;哈密顿回路 注意立即数被符号位扩展 参考链接 寄存器 NameReg. NumUsage z e r o zero zero0constant value 0(恒为0) a t at a…