MySQL数据库练习【一】

news2024/11/18 9:39:41

MySQL数据库练习【一】

  • 一、建库建表-数据准备
  • 二、习题
    • 2.1. 查询部门编号为30的部门的员工详细信息
    • 2.2.查询从事clerk工作的员工的编号、姓名以及其部门号
    • 2.3.查询奖金多于基本工资的员工的信息、查询奖金小于基本工资的员工的信息
    • 2.4.查询奖金多于基本工资60%的员工的信息
    • 2.5.查询部门编号为10的部门经理 (job=manager)
    • 2.6.查询部门编号为20的在部门中工作为CLERK的职员信息
    • 2.7.查询部门编号为10的部门经理 (job=manager)或部门编号为20的部门工作为CLERK的职员信息
    • 2.8.查询部门编号为10的部门经理或部门编号为20的部门工作为CLERK的职员信息或者既不是经理也不是CLERK但是工资高于2000的员工信息
    • 2.9.查询获得奖金的员工的信息
    • 2.10.查询奖金少于100或者没有获得奖金的员工的信息
    • 2.11.MySQL LEFT函数【语法】
    • 2.12.查询姓名以A、B、S开头的员工的信息【LEFT函数】
    • 2.13.查询找到姓名长度为6个字符的员工的信息【length函数】
    • 2.14.查询姓名中不包含R字符的员工信息【not like】
    • 2.15.查询员工的详细信息并按姓名排序【asc、desc】
    • 2.16.查询员工的信息并按工作岗位降序、工资升序排列
    • 2.17.计算员工的日薪(按30天计)【ROUND】
    • 2.18.查询姓名中包含字符A的员工的信息
    • 2.19.查询拥有员工的部门的部门名称和部门编号【自连接 inner join】
    • 2.20.查询工资多于smith的员工信息
    • 2.21.查询员工和及其所属经理的姓名(同一个部门,job=manager)
    • 2.22.DATEDIFF函数与TIMESTAMPDIFF函数【语法】
    • 2.23.查询员工雇佣日期早于其经理雇佣日期的员工及其经理姓名
    • 2.24.查询员工姓名及其所在的部门名称
    • 2.25.查询工作为clerk的员工姓名及其所在部门名称
    • 2.26.查询各部门编号及其部门中的最低工资【group by】
    • 2.27.查询销售部sales的所有员工的姓名
    • 2.28.子查询【语法】
    • 2.29.列出薪金高于公司平均薪金的所有员工,要求使用子查询实现【子查询】
    • 2.29.查询工资比编号为7566雇员工资高的雇员姓名
    • 2.30.显示和雇员scott同部门的雇员姓名、工资和部门编号
    • 2.31.显示和雇员SCOTT从事相同工作,并且工资大于JAMES的雇员姓名、工作岗位和工资
    • 2.32.显示工资最低的雇员姓名、工作和工资
    • 2.33.显示工资最高的雇员姓名、工作和工资
    • 2.34.显示工资高于平均工资的雇员姓名、工作、工资和工资等级
    • 2.35.查询部门内的最低工资比20号部门的最低工资要高的部门的编号及部门内最低工资【having】
    • 2.36.查询和SMITH从事相同工作的雇员姓名和工作
    • 2.37.查询有下属的雇员姓名、工作、工资、部门号
    • 2.38.查询没有下属的雇员姓名、工作、工资和部门号
    • 2.39.查询各部门中工资最低的员工姓名、工作、工资和部门号
    • 2.40.查询部门中工资最高的员工姓名、工作、工资和部门号
    • 2.41.查询与销售部门(SALES)工作岗位相同的其它部门雇员姓名、工作、工资和部门名称
    • 2.42.使用any运算符的多行子查询【语法】
    • 2.43.查询工资低于某个文员(CLERK)雇员工资,但不从事文员工作的员工编号、姓名、工作岗位和工资
    • 2.44.查询工资高于某个文员(CLERK)雇员工资,但不从事文员工作的员工编号、姓名、工作岗位和工资
    • 2.45.查询工资高于部门编号是30的部门内某个雇员工资,但不在该部门工作的员工姓名、工作岗位、工资和部门编号
    • 2.46.查询工资低于部门名称是SALES的部门内某个雇员工资,但不在该部门工作的员工姓名、工作岗位、工资和部门编号和部门名称
    • 2.47.使用all运算符的多行子查询【语法】
    • 2.48.查询高于所有部门平均工资的雇员姓名、工作、工资和部门编号
    • 2.49.查询低于所有部门平均工资的雇员姓名、工作、工资和部门编号
    • 2.50.查询工资高于部门编号为30的部门内所有员工工资的雇员姓名、工作、工资和部门编号
    • 2.51.查询工资等级为4的雇员姓名、工作、工资、部门编号和工资等级,同时满足该雇员工资高于部门编号为30的部门内所有员工工资
    • 2.52.查询各部门中工资等级最高的雇员姓名、工作、工资、工资等级和部门号
    • 2.53.查询各部门中工资等级最高的雇员姓名、工作、工资、工资等级和部门名称
    • 2.54.查询工资与30部门中任意一个雇员的工资相等,同时奖金也与30部门中任意一个雇员奖金相等的雇员姓名、工资、奖金、部门编号,但该雇员不是来自30号部门
    • 2.55.查询在纽约(NEW YORK)工作的雇员姓名、工作岗位、工资和奖金
    • 2.56.显示工资高于部门平均工资的雇员姓名、工作、工资和部门号
    • 2.57.查询各部门中工资等级最高的雇员姓名、工作、工资、工资等级和部门号
    • 2.58.查询平均工资最低的工种名称及其平均工资
    • 2.59.查询与部门编号为30的员工工资水平相同的员工信息
    • 2.60.查询部门号、部门名、部门所在位置及其每个部门的员工总数
    • 2.61.计算员工的年薪并且以年薪排序
  • endl

一、建库建表-数据准备

create database company;

use company;

-- 创建部门表
DROP TABLE IF EXISTS dept; 
CREATE TABLE `dept` (
  `deptno` int(11) PRIMARY KEY NOT NULL COMMENT '部门编号',
  `dname` varchar(14) NOT NULL COMMENT '部门名称',
  `loc` varchar(13) NOT NULL COMMENT '部门所在地'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='部门表';
 
-- 向部门表插入数据
INSERT INTO dept VALUES (10,'ACCOUNTING','NEW YORK'),
(20,'RESEARCH','DALLAS'),
(30,'SALES','CHICAGO'),
(40,'OPERATIONS','BOSTON');
 
-- 创建员工表
DROP TABLE IF EXISTS emp;
CREATE TABLE `emp` (
  `empno` int(11) PRIMARY KEY NOT NULL COMMENT '员工编号',
  `ename` varchar(10) DEFAULT NULL COMMENT '员工姓名',
  `job` varchar(9) DEFAULT NULL COMMENT '工作岗位',
  `mgr` int(11) DEFAULT NULL COMMENT '直属领导',
  `hiredate` date DEFAULT NULL COMMENT '入职时间',
  `sal` double DEFAULT NULL COMMENT '工资',
  `comm` double DEFAULT NULL COMMENT '奖金',
  `deptno` int(11) DEFAULT NULL COMMENT '所属部门'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='员工表';

-- 向员工表插入数据
INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,"1980-12-17",800,NULL,20),
(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30),
(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30),
(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20),
(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30),
(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30),
(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10),
(7788,'SCOTT','ANALYST',7566,'1987-07-03',3000,NULL,20),
(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10),
(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30),
(7876,'ADAMS','CLERK',7788,'1987-07-13',1100,NULL,20),
(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30),
(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20),
(7934,'MILLER','CLERK',7782,'1981-01-23',1300,NULL,10);
 
 
-- 创建工资等级表
DROP TABLE IF EXISTS salgrade;
CREATE TABLE `salgrade` (
  `grade` int(11) DEFAULT NULL COMMENT '等级',
  `losal` double DEFAULT NULL COMMENT '最低工资',
  `hisal` double DEFAULT NULL COMMENT '最高工资'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='工资等级表';
 
-- 向工资等级表插入数据
INSERT INTO salgrade VALUES (1,700,1200),(2,1201,1400),(3,1401,2000),(4,2001,3000),(5,3001,9999);
-- 为员工表表添加外键约束
-- ALTER TABLE emp ADD CONSTRAINT FOREIGN KEY EMP(deptno) REFERENCES dept (deptno);

在这里插入图片描述

二、习题

2.1. 查询部门编号为30的部门的员工详细信息

mysql> select * from emp where deptno=30;
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7499 | ALLEN  | SALESMAN | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7521 | WARD   | SALESMAN | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7698 | BLAKE  | MANAGER  | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7900 | JAMES  | CLERK    | 7698 | 1981-12-03 |  950 | NULL |     30 |
+-------+--------+----------+------+------------+------+------+--------+

2.2.查询从事clerk工作的员工的编号、姓名以及其部门号

mysql> select empno,ename,deptno from emp where job='clerk';
+-------+--------+--------+
| empno | ename  | deptno |
+-------+--------+--------+
|  7369 | SMITH  |     20 |
|  7876 | ADAMS  |     20 |
|  7900 | JAMES  |     30 |
|  7934 | MILLER |     10 |
+-------+--------+--------+
4 rows in set (0.00 sec)

2.3.查询奖金多于基本工资的员工的信息、查询奖金小于基本工资的员工的信息

mysql> select * from emp where comm > sal;
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
+-------+--------+----------+------+------------+------+------+--------+
1 row in set (0.00 sec)

mysql> select * from emp where comm is null or comm < sal;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 |
+-------+--------+-----------+------+------------+------+------+--------+
13 rows in set (0.00 sec)

2.4.查询奖金多于基本工资60%的员工的信息

mysql> select * from emp where comm > sal * 0.6;
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
+-------+--------+----------+------+------------+------+------+--------+
1 row in set (0.00 sec)

2.5.查询部门编号为10的部门经理 (job=manager)

mysql> select * from emp where deptno = 10 and job = 'manager';
+-------+-------+---------+------+------------+------+------+--------+
| empno | ename | job     | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+---------+------+------------+------+------+--------+
|  7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL |     10 |
+-------+-------+---------+------+------------+------+------+--------+
1 row in set (0.00 sec)

2.6.查询部门编号为20的在部门中工作为CLERK的职员信息

mysql> select * from emp where deptno = 20 and job = 'clerk';
+-------+-------+-------+------+------------+------+------+--------+
| empno | ename | job   | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+-------+------+------------+------+------+--------+
|  7369 | SMITH | CLERK | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7876 | ADAMS | CLERK | 7788 | 1987-07-13 | 1100 | NULL |     20 |
+-------+-------+-------+------+------------+------+------+--------+
2 rows in set (0.00 sec)

2.7.查询部门编号为10的部门经理 (job=manager)或部门编号为20的部门工作为CLERK的职员信息

mysql> select * from emp where (deptno = 10 and job = 'manager') or (deptno = 20 and job = 'clerk');
+-------+-------+---------+------+------------+------+------+--------+
| empno | ename | job     | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+---------+------+------------+------+------+--------+
|  7369 | SMITH | CLERK   | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7782 | CLARK | MANAGER | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7876 | ADAMS | CLERK   | 7788 | 1987-07-13 | 1100 | NULL |     20 |
+-------+-------+---------+------+------------+------+------+--------+
3 rows in set (0.00 sec)

2.8.查询部门编号为10的部门经理或部门编号为20的部门工作为CLERK的职员信息或者既不是经理也不是CLERK但是工资高于2000的员工信息

mysql> select * from emp where (deptno = 10 and job = 'manager') 
    -> or (deptno = 20 and job = 'clerk') 
    -> or (job not in ('manager','clerk') AND sal > 2000);
+-------+-------+-----------+------+------------+------+------+--------+
| empno | ename | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+-----------+------+------------+------+------+--------+
|  7369 | SMITH | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7782 | CLARK | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7788 | SCOTT | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7876 | ADAMS | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7902 | FORD  | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
+-------+-------+-----------+------+------------+------+------+--------+
6 rows in set (0.00 sec)

2.9.查询获得奖金的员工的信息

mysql> select * from emp where comm is not null and comm > 0;
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7499 | ALLEN  | SALESMAN | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7521 | WARD   | SALESMAN | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
+-------+--------+----------+------+------------+------+------+--------+
3 rows in set (0.00 sec)

2.10.查询奖金少于100或者没有获得奖金的员工的信息

mysql> select * from emp where comm is null or comm < 100;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 |
+-------+--------+-----------+------+------------+------+------+--------+
11 rows in set (0.00 sec)

2.11.MySQL LEFT函数【语法】

LEFT()函数是一个字符串函数,它返回具有指定长度的字符串的左边部分。

LEFT()函数的语法

LEFT(str,length);

LEFT()函数接受两个参数:

str是要提取子字符串的字符串。
length是一个正整数,指定将从左边返回的字符数。
LEFT()函数返回str字符串中最左边的长度字符。
  • 如果str或length参数为NULL,则返回NULL值。
  • 如果length为0或为负,则LEFT函数返回一个空字符串。
  • 如果length大于str字符串的长度,则LEFT函数返回整个str字符串。
mysql> SELECT LEFT('MySQL LEFT', 5);
+-----------------------+
| LEFT('MySQL LEFT', 5) |
+-----------------------+
| MySQL                 |
+-----------------------+
1 row in set (0.00 sec)

2.12.查询姓名以A、B、S开头的员工的信息【LEFT函数】

LEFT(ename,1) 表示取名字的第一个字符

IN ('A','B','C') 取出姓名第一个字符与之相匹配的员工信息
mysql> select * from emp where ename like 'A%' or ename like 'B%' or ename like 'C%';
+-------+-------+----------+------+------------+------+------+--------+
| empno | ename | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+----------+------+------------+------+------+--------+
|  7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK | MANAGER  | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7876 | ADAMS | CLERK    | 7788 | 1987-07-13 | 1100 | NULL |     20 |
+-------+-------+----------+------+------------+------+------+--------+
4 rows in set (0.01 sec)

mysql> select * from emp  WHERE LEFT(ename,1) IN ('A','B','C');
+-------+-------+----------+------+------------+------+------+--------+
| empno | ename | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+----------+------+------------+------+------+--------+
|  7499 | ALLEN | SALESMAN | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7698 | BLAKE | MANAGER  | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK | MANAGER  | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7876 | ADAMS | CLERK    | 7788 | 1987-07-13 | 1100 | NULL |     20 |
+-------+-------+----------+------+------------+------+------+--------+
4 rows in set (0.00 sec)

2.13.查询找到姓名长度为6个字符的员工的信息【length函数】

mysql> select * from emp where length(ename) = 6;
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7934 | MILLER | CLERK    | 7782 | 1981-01-23 | 1300 | NULL |     10 |
+-------+--------+----------+------+------------+------+------+--------+
3 rows in set (0.00 sec)

2.14.查询姓名中不包含R字符的员工信息【not like】

mysql> select * from emp where ename not like '%R%';
+-------+-------+-----------+------+------------+------+------+--------+
| empno | ename | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+-----------+------+------------+------+------+--------+
|  7369 | SMITH | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7499 | ALLEN | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7566 | JONES | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7698 | BLAKE | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7788 | SCOTT | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7876 | ADAMS | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7900 | JAMES | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
+-------+-------+-----------+------+------------+------+------+--------+
8 rows in set (0.00 sec)

mysql> select * from emp where ename like '%R%';
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7521 | WARD   | SALESMAN | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7782 | CLARK  | MANAGER  | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7902 | FORD   | ANALYST  | 7566 | 1981-12-03 | 3000 | NULL |     20 |
|  7934 | MILLER | CLERK    | 7782 | 1981-01-23 | 1300 | NULL |     10 |
+-------+--------+----------+------+------------+------+------+--------+
6 rows in set (0.00 sec)

2.15.查询员工的详细信息并按姓名排序【asc、desc】

mysql> select * from emp order by ename asc;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 |
+-------+--------+-----------+------+------------+------+------+--------+
14 rows in set (0.00 sec)

mysql> select * from emp order by ename desc;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
+-------+--------+-----------+------+------------+------+------+--------+
14 rows in set (0.00 sec)

2.16.查询员工的信息并按工作岗位降序、工资升序排列

mysql> select * from emp order by job desc,sal asc;
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
+-------+--------+-----------+------+------------+------+------+--------+
14 rows in set (0.00 sec)

2.17.计算员工的日薪(按30天计)【ROUND】

mysql round()函数的第二个参数表示需要保留的小数位数,如果不指定第二个参数,则默认保留零位小数。如果第二个参数为负数,则会将数字整数部分进行四舍五入。

ROUND(sal/30,2),2表示小数点后面两位后面的数进行四舍五入
ROUND(sal/30,-1) ,-1表示小数点前面一位
例如ROUND(316.15926,-1),小数点的前面一位6前面的2保留,然后6进1,结果为320

mysql> select ROUND(314.15926,2);
+--------------------+
| ROUND(314.15926,2) |
+--------------------+
|             314.16 |
+--------------------+
1 row in set (0.00 sec)

mysql> select ROUND(314.15926,-1);
+---------------------+
| ROUND(314.15926,-1) |
+---------------------+
|                 310 |
+---------------------+
1 row in set (0.00 sec)

mysql> select ROUND(316.15926,-1);
+---------------------+
| ROUND(316.15926,-1) |
+---------------------+
|                 320 |
+---------------------+
1 row in set (0.00 sec)

mysql> select ename 员工姓名,sal 月薪,ROUND(sal/30,2) 日薪 from emp;
+--------------+--------+--------+
| 员工姓名     | 月薪   | 日薪   |
+--------------+--------+--------+
| SMITH        |    800 |  26.67 |
| ALLEN        |   1600 |  53.33 |
| WARD         |   1250 |  41.67 |
| JONES        |   2975 |  99.17 |
| MARTIN       |   1250 |  41.67 |
| BLAKE        |   2850 |  95.00 |
| CLARK        |   2450 |  81.67 |
| SCOTT        |   3000 | 100.00 |
| KING         |   5000 | 166.67 |
| TURNER       |   1500 |  50.00 |
| ADAMS        |   1100 |  36.67 |
| JAMES        |    950 |  31.67 |
| FORD         |   3000 | 100.00 |
| MILLER       |   1300 |  43.33 |
+--------------+--------+--------+
14 rows in set (0.00 sec)

2.18.查询姓名中包含字符A的员工的信息

mysql> select * from emp where ename like '%A%';
+-------+--------+----------+------+------------+------+------+--------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+----------+------+------------+------+------+--------+
|  7499 | ALLEN  | SALESMAN | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7521 | WARD   | SALESMAN | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7654 | MARTIN | SALESMAN | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7698 | BLAKE  | MANAGER  | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK  | MANAGER  | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7876 | ADAMS  | CLERK    | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7900 | JAMES  | CLERK    | 7698 | 1981-12-03 |  950 | NULL |     30 |
+-------+--------+----------+------+------------+------+------+--------+
7 rows in set (0.00 sec)

2.19.查询拥有员工的部门的部门名称和部门编号【自连接 inner join】

-- 查询拥有员工的部门的部门名称和部门编号【自连接 inner join】  -- INNER JOIN ...  ON  ...
-- 表结构: emp,dept
-- 连接条件: emp.deptno = dept.deptno

select distinct dname 部门名称,dept.deptno 部门编号
from dept
inner join emp
on dept.deptno = emp.deptno;
mysql> select distinct dname 部门名称,dept.deptno 部门编号
    -> from dept
    -> inner join emp
    -> on dept.deptno = emp.deptno;
+--------------+--------------+
| 部门名称     | 部门编号     |
+--------------+--------------+
| RESEARCH     |           20 |
| SALES        |           30 |
| ACCOUNTING   |           10 |
+--------------+--------------+
3 rows in set (0.00 sec)

2.20.查询工资多于smith的员工信息

mysql> select * from emp where ename = 'smith';
+-------+-------+-------+------+------------+------+------+--------+
| empno | ename | job   | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+-------+------+------------+------+------+--------+
|  7369 | SMITH | CLERK | 7902 | 1980-12-17 |  800 | NULL |     20 |
+-------+-------+-------+------+------------+------+------+--------+
1 row in set (0.00 sec)

mysql> select * from emp where sal > (select sal from emp where ename = 'smith');
+-------+--------+-----------+------+------------+------+------+--------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+--------+-----------+------+------------+------+------+--------+
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 |
+-------+--------+-----------+------+------------+------+------+--------+
13 rows in set (0.00 sec)

2.21.查询员工和及其所属经理的姓名(同一个部门,job=manager)

-- 查询员工和及其所属经理的姓名(同一个部门,job=manager)  -- INNER JOIN ...  ON  ...
-- 表结构: emp e,emp t
-- 连接条件: e.deptno = t.deptno
-- where: e.job!='manager' and t.job='manager'

select e.*,t.ename 经理姓名 
from emp e 
inner join emp t 
on e.deptno = t.deptno 
where e.job!='manager' and t.job='manager' 
order by e.empno asc;
mysql> select e.*,t.ename 经理姓名 
    -> from emp e 
    -> inner join emp t 
    -> on e.deptno = t.deptno 
    -> where e.job!='manager' and t.job='manager' 
    -> order by e.empno asc;
+-------+--------+-----------+------+------------+------+------+--------+--------------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno | 经理姓名     |
+-------+--------+-----------+------+------------+------+------+--------+--------------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 | JONES        |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 | BLAKE        |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 | BLAKE        |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 | BLAKE        |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 | JONES        |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 | CLARK        |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 | BLAKE        |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 | JONES        |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 | BLAKE        |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 | JONES        |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 | CLARK        |
+-------+--------+-----------+------+------------+------+------+--------+--------------+
11 rows in set (0.00 sec)

2.22.DATEDIFF函数与TIMESTAMPDIFF函数【语法】

MySQL DATEDIFF()函数是MySQL中常见的日期函数之一,它主要用于计算两个日期之间的差值,单位可以是天、周、月、季度和年。

DATEDIFF(date1, date2)

其中,date1和date2是两个日期,可以是日期型、时间型或时间戳型数据。

-- 1
SELECT DATEDIFF('2022-04-30','2022-04-29');
-- 0
SELECT DATEDIFF('2022-04-30','2022-04-30');
-- -1
SELECT DATEDIFF('2022-04-29','2022-04-30');
 
-- 0
SELECT DATEDIFF('2022-04-30','2022-04-30 14:00:00');
-- 1
SELECT DATEDIFF('2022-04-30 13:00:00','2022-04-29 14:00:00');
-- 10
SELECT DATEDIFF('2017-06-25 09:34:21', '2017-06-15');

-- -2
SELECT DATEDIFF('2022-04-28', '2022-04-30');
-- 2
SELECT TIMESTAMPDIFF(DAY,'2022-04-28', '2022-04-30');

DATEDIFF函数与TIMESTAMPDIFF函数的区别

  1. DATEDIFF函数仅用于返回两个日期的天数,TIMESTAMPDIFF函数用于返回计算两个日期指定单位的时间差(指定单位可以是年,季度,月,星期,天数,小时,分钟,秒等等)

  2. 对日期差值的计算方式相反

  • DATEDIFF函数的语法格式: DATEDIFF(start,end)

  • DATEDIFF函数返回start - end的计算结果

  • TIMESTAMPDIFF函数的语法格式: TIMESTAMPDIFF(DAY,start,end)

  • TIMESTAMPDIFF函数返回end - start的计算结果

2.23.查询员工雇佣日期早于其经理雇佣日期的员工及其经理姓名

-- 查询雇佣日期早于其经理雇佣日期的员工及其经理姓名  -- INNER JOIN ...  ON  ...
-- 表结构: emp e,emp t
-- 连接条件: e.deptno = t.deptno
-- where条件: e.job!='manager' and t.job='manager' and datediff(e.hiredate,t.hiredate)<0

select e.ename 员工姓名,e.hiredate 员工雇佣日期,t.ename 经理姓名,t.hiredate 经理雇佣日期
from emp e
inner join emp t
on e.deptno = t.deptno
where e.job!='manager' and t.job='manager' and datediff(e.hiredate,t.hiredate)<0;
mysql> select e.ename 员工姓名,e.hiredate 员工雇佣日期,t.ename 经理姓名,t.hiredate 经理雇佣日期
    -> from emp e
    -> inner join emp t
    -> on e.deptno = t.deptno
    -> where e.job!='manager' and t.job='manager' and datediff(e.hiredate,t.hiredate)<0;
+--------------+--------------------+--------------+--------------------+
| 员工姓名     | 员工雇佣日期       | 经理姓名     | 经理雇佣日期       |
+--------------+--------------------+--------------+--------------------+
| SMITH        | 1980-12-17         | JONES        | 1981-04-02         |
| ALLEN        | 1981-02-20         | BLAKE        | 1981-05-01         |
| WARD         | 1981-02-22         | BLAKE        | 1981-05-01         |
| MILLER       | 1981-01-23         | CLARK        | 1981-06-09         |
+--------------+--------------------+--------------+--------------------+
4 rows in set (0.00 sec)

2.24.查询员工姓名及其所在的部门名称

-- 查询员工姓名及其所在的部门名称  -- INNER JOIN ...  ON  ...
-- 表结构: emp e,dept d
-- 连接条件: e.deptno = d.deptno

select e.ename,d.dname
from emp e
inner join dept d
on e.deptno = d.deptno;

select e.ename,d.dname from emp e,dept d where e.deptno = d.deptno;
mysql> select e.ename,d.dname
    -> from emp e
    -> inner join dept d
    -> on e.deptno = d.deptno;
+--------+------------+
| ename  | dname      |
+--------+------------+
| SMITH  | RESEARCH   |
| ALLEN  | SALES      |
| WARD   | SALES      |
| JONES  | RESEARCH   |
| MARTIN | SALES      |
| BLAKE  | SALES      |
| CLARK  | ACCOUNTING |
| SCOTT  | RESEARCH   |
| KING   | ACCOUNTING |
| TURNER | SALES      |
| ADAMS  | RESEARCH   |
| JAMES  | SALES      |
| FORD   | RESEARCH   |
| MILLER | ACCOUNTING |
+--------+------------+
14 rows in set (0.00 sec)

2.25.查询工作为clerk的员工姓名及其所在部门名称

-- 查询工作为clerk的员工姓名及其所在部门名称  -- INNER JOIN ...  ON  ...
-- 表结构: emp e,dept d
-- 连接条件: e.deptno = d.deptno
-- where条件: e.job = 'clerk'
select e.ename 员工姓名,d.dname 部门名称
from emp e
inner join dept d
on e.deptno = d.deptno
where e.job = 'clerk';
mysql> select e.ename 员工姓名,d.dname 部门名称
    -> from emp e
    -> inner join dept d
    -> on e.deptno = d.deptno
    -> where e.job = 'clerk';
+--------------+--------------+
| 员工姓名     | 部门名称     |
+--------------+--------------+
| SMITH        | RESEARCH     |
| ADAMS        | RESEARCH     |
| JAMES        | SALES        |
| MILLER       | ACCOUNTING   |
+--------------+--------------+
4 rows in set (0.00 sec)

2.26.查询各部门编号及其部门中的最低工资【group by】

mysql> select deptno,min(sal) from emp group by deptno;
+--------+----------+
| deptno | min(sal) |
+--------+----------+
|     10 |     1300 |
|     20 |      800 |
|     30 |      950 |
+--------+----------+
3 rows in set (0.01 sec)

2.27.查询销售部sales的所有员工的姓名

-- 查询销售部sales的所有员工的姓名  -- INNER JOIN ...  ON  ...
-- 表结构: emp e,dept d
-- 连接条件: e.deptno = d.deptno
-- where条件: d.dname = 'sales'

select e.ename,d.dname
from emp e
inner join dept d
on e.deptno = d.deptno
where d.dname = 'sales';
mysql> select e.ename,d.dname
    -> from emp e
    -> inner join dept d
    -> on e.deptno = d.deptno
    -> where d.dname = 'sales';
+--------+-------+
| ename  | dname |
+--------+-------+
| ALLEN  | SALES |
| WARD   | SALES |
| MARTIN | SALES |
| BLAKE  | SALES |
| TURNER | SALES |
| JAMES  | SALES |
+--------+-------+
6 rows in set (0.00 sec)

2.28.子查询【语法】

子查询是嵌套在一个select语句中的另一个select语句。

当需要从一个表中检索信息,检索条件值又是来自该表本身的内部数据时,子查询非常有用。

子查询可以嵌入以下SQL子句中:where子句、having子句和from子句。

where子句使用单行子查询返回结果要采用单行比较运算符(=>>=<<=<>

说明:

  • 子查询要用括号括起来;
  • 将子查询放在比较运算符的右边;
  • 不要在子查询中使用order by子句,select语句中只能有一个order by子句,并且它只能是主select语句的最后一个子句。

2.29.列出薪金高于公司平均薪金的所有员工,要求使用子查询实现【子查询】

mysql> select * from emp where sal > (select avg(sal) from emp);
+-------+-------+-----------+------+------------+------+------+--------+
| empno | ename | job       | mgr  | hiredate   | sal  | comm | deptno |
+-------+-------+-----------+------+------------+------+------+--------+
|  7566 | JONES | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 |
|  7698 | BLAKE | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 |
|  7782 | CLARK | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 |
|  7788 | SCOTT | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 |
|  7839 | KING  | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 |
|  7902 | FORD  | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 |
+-------+-------+-----------+------+------------+------+------+--------+
6 rows in set (0.00 sec)

2.29.查询工资比编号为7566雇员工资高的雇员姓名

select ename 
from emp 
where sal > (select sal from emp where empno = 7566)
order by ename;
mysql> select ename 
    -> from emp 
    -> where sal > (select sal from emp where empno = 7566)
    -> order by ename;
+-------+
| ename |
+-------+
| FORD  |
| KING  |
| SCOTT |
+-------+
3 rows in set (0.00 sec)

2.30.显示和雇员scott同部门的雇员姓名、工资和部门编号

select ename,sal,deptno
from emp
where deptno = (select deptno from emp where ename = 'scott');
mysql> select ename,sal,deptno
    -> from emp
    -> where deptno = (select deptno from emp where ename = 'scott');
+-------+------+--------+
| ename | sal  | deptno |
+-------+------+--------+
| SMITH |  800 |     20 |
| JONES | 2975 |     20 |
| SCOTT | 3000 |     20 |
| ADAMS | 1100 |     20 |
| FORD  | 3000 |     20 |
+-------+------+--------+
5 rows in set (0.00 sec)

2.31.显示和雇员SCOTT从事相同工作,并且工资大于JAMES的雇员姓名、工作岗位和工资

select ename,job,sal
from emp
where job = (select job from emp where ename = 'scott')
and sal > (select sal from emp where ename = 'james');
mysql> select ename,job,sal
    -> from emp
    -> where job = (select job from emp where ename = 'scott')
    -> and sal > (select sal from emp where ename = 'james');
+-------+---------+------+
| ename | job     | sal  |
+-------+---------+------+
| SCOTT | ANALYST | 3000 |
| FORD  | ANALYST | 3000 |
+-------+---------+------+
2 rows in set (0.00 sec)

2.32.显示工资最低的雇员姓名、工作和工资

select ename,job,sal
from emp
where sal = (select min(sal) from emp);
mysql> select ename,job,sal
    -> from emp
    -> where sal = (select min(sal) from emp);
+-------+-------+------+
| ename | job   | sal  |
+-------+-------+------+
| SMITH | CLERK |  800 |
+-------+-------+------+
1 row in set (0.00 sec)

2.33.显示工资最高的雇员姓名、工作和工资

select ename,job,sal
from emp
where sal = (select max(sal) from emp);
mysql> select ename,job,sal
    -> from emp
    -> where sal = (select max(sal) from emp);
+-------+-----------+------+
| ename | job       | sal  |
+-------+-----------+------+
| KING  | PRESIDENT | 5000 |
+-------+-----------+------+
1 row in set (0.01 sec)

2.34.显示工资高于平均工资的雇员姓名、工作、工资和工资等级

select e.ename,e.job,e.sal,s.grade
from emp e,salgrade s
where e.sal > (select avg(sal) from emp)
and e.sal between s.losal and s.hisal;
mysql> select e.ename,e.job,e.sal,s.grade
    -> from emp e,salgrade s
    -> where e.sal > (select avg(sal) from emp)
    -> and e.sal between s.losal and s.hisal;
+-------+-----------+------+-------+
| ename | job       | sal  | grade |
+-------+-----------+------+-------+
| JONES | MANAGER   | 2975 |     4 |
| BLAKE | MANAGER   | 2850 |     4 |
| CLARK | MANAGER   | 2450 |     4 |
| SCOTT | ANALYST   | 3000 |     4 |
| FORD  | ANALYST   | 3000 |     4 |
| KING  | PRESIDENT | 5000 |     5 |
+-------+-----------+------+-------+
6 rows in set (0.00 sec)

2.35.查询部门内的最低工资比20号部门的最低工资要高的部门的编号及部门内最低工资【having】

-- 按部门显示部门编号、部门最低工资
select deptno 部门编号,min(sal) 最低工资
from emp
group by deptno;

select deptno 部门编号,min(sal) 最低工资
from emp
group by deptno
having min(sal)>(select min(sal) from emp where deptno = 20);
mysql> select deptno 部门编号,min(sal) 最低工资
    -> from emp
    -> group by deptno;
+--------------+--------------+
| 部门编号     | 最低工资     |
+--------------+--------------+
|           10 |         1300 |
|           20 |          800 |
|           30 |          950 |
+--------------+--------------+
3 rows in set (0.00 sec)

mysql> select deptno 部门编号,min(sal) 最低工资
    -> from emp
    -> group by deptno
    -> having min(sal)>(select min(sal) from emp where deptno = 20);
+--------------+--------------+
| 部门编号     | 最低工资     |
+--------------+--------------+
|           10 |         1300 |
|           30 |          950 |
+--------------+--------------+
2 rows in set (0.00 sec)

2.36.查询和SMITH从事相同工作的雇员姓名和工作

select ename,job
from emp
where job = (select job from emp where ename = 'smith');
mysql> select ename,job
    -> from emp
    -> where job = (select job from emp where ename = 'smith');
+--------+-------+
| ename  | job   |
+--------+-------+
| SMITH  | CLERK |
| ADAMS  | CLERK |
| JAMES  | CLERK |
| MILLER | CLERK |
+--------+-------+
4 rows in set (0.00 sec)

2.37.查询有下属的雇员姓名、工作、工资、部门号

select ename,job,sal,deptno,mgr
from emp
where empno in (select mgr from emp where mgr is not NULL);
mysql> select ename,job,sal,deptno,mgr
    -> from emp
    -> where empno in (select mgr from emp where mgr is not NULL);
+-------+-----------+------+--------+------+
| ename | job       | sal  | deptno | mgr  |
+-------+-----------+------+--------+------+
| FORD  | ANALYST   | 3000 |     20 | 7566 |
| BLAKE | MANAGER   | 2850 |     30 | 7839 |
| KING  | PRESIDENT | 5000 |     10 | NULL |
| JONES | MANAGER   | 2975 |     20 | 7839 |
| SCOTT | ANALYST   | 3000 |     20 | 7566 |
| CLARK | MANAGER   | 2450 |     10 | 7839 |
+-------+-----------+------+--------+------+
6 rows in set (0.00 sec)

2.38.查询没有下属的雇员姓名、工作、工资和部门号

select ename,job,sal,deptno,mgr
from emp
where empno not in (select mgr from emp where mgr is not null);
mysql> select ename,job,sal,deptno,mgr
    -> from emp
    -> where empno not in (select mgr from emp where mgr is not null);
+--------+----------+------+--------+------+
| ename  | job      | sal  | deptno | mgr  |
+--------+----------+------+--------+------+
| SMITH  | CLERK    |  800 |     20 | 7902 |
| ALLEN  | SALESMAN | 1600 |     30 | 7698 |
| WARD   | SALESMAN | 1250 |     30 | 7698 |
| MARTIN | SALESMAN | 1250 |     30 | 7698 |
| TURNER | SALESMAN | 1500 |     30 | 7698 |
| ADAMS  | CLERK    | 1100 |     20 | 7788 |
| JAMES  | CLERK    |  950 |     30 | 7698 |
| MILLER | CLERK    | 1300 |     10 | 7782 |
+--------+----------+------+--------+------+

2.39.查询各部门中工资最低的员工姓名、工作、工资和部门号

select ename,job,sal,deptno
from emp
where sal in (select min(sal) from emp group by deptno);
mysql> select ename,job,sal,deptno
    -> from emp
    -> where sal in (select min(sal) from emp group by deptno);
+--------+-------+------+--------+
| ename  | job   | sal  | deptno |
+--------+-------+------+--------+
| SMITH  | CLERK |  800 |     20 |
| JAMES  | CLERK |  950 |     30 |
| MILLER | CLERK | 1300 |     10 |
+--------+-------+------+--------+
3 rows in set (0.01 sec)

2.40.查询部门中工资最高的员工姓名、工作、工资和部门号

select ename,job,sal,deptno
from emp
where sal in (select max(sal) from emp group by deptno);
mysql> select ename,job,sal,deptno
    -> from emp
    -> where sal in (select max(sal) from emp group by deptno);
+-------+-----------+------+--------+
| ename | job       | sal  | deptno |
+-------+-----------+------+--------+
| BLAKE | MANAGER   | 2850 |     30 |
| SCOTT | ANALYST   | 3000 |     20 |
| KING  | PRESIDENT | 5000 |     10 |
| FORD  | ANALYST   | 3000 |     20 |
+-------+-----------+------+--------+
4 rows in set (0.00 sec)

2.41.查询与销售部门(SALES)工作岗位相同的其它部门雇员姓名、工作、工资和部门名称

select e.ename,e.job,e.sal,d.dname
from emp e,dept d
where e.deptno = d.deptno
and d.dname<>'sales'
and job in (select distinct e.job from emp e,dept d where e.deptno = d.deptno and d.dname='sales');
mysql> select e.ename,e.job,e.sal,d.dname
    -> from emp e,dept d
    -> where e.deptno = d.deptno
    -> and d.dname<>'sales'
    -> and job in (select distinct e.job from emp e,dept d where e.deptno = d.deptno and d.dname='sales');
+--------+---------+------+------------+
| ename  | job     | sal  | dname      |
+--------+---------+------+------------+
| SMITH  | CLERK   |  800 | RESEARCH   |
| JONES  | MANAGER | 2975 | RESEARCH   |
| CLARK  | MANAGER | 2450 | ACCOUNTING |
| ADAMS  | CLERK   | 1100 | RESEARCH   |
| MILLER | CLERK   | 1300 | ACCOUNTING |
+--------+---------+------+------------+
5 rows in set (0.00 sec)

2.42.使用any运算符的多行子查询【语法】

  • Any运算符将和内部查询返回的结果逐个比较,与单行操作符配合使用。
  • <any:表示比子查询返回结果中的最大值小;
  • =any:表示可以是子查询返回结果中的任意一个值;
  • >any:表示比子查询返回结果中的最小值大。

2.43.查询工资低于某个文员(CLERK)雇员工资,但不从事文员工作的员工编号、姓名、工作岗位和工资

select empno,ename,job,sal
from emp
where sal <any (select sal from emp where job = 'clerk')
and job<>'clerk';
mysql> select empno,ename,job,sal
    -> from emp
    -> where sal <any (select sal from emp where job = 'clerk')
    -> and job<>'clerk';
+-------+--------+----------+------+
| empno | ename  | job      | sal  |
+-------+--------+----------+------+
|  7521 | WARD   | SALESMAN | 1250 |
|  7654 | MARTIN | SALESMAN | 1250 |
+-------+--------+----------+------+
2 rows in set (0.00 sec)

2.44.查询工资高于某个文员(CLERK)雇员工资,但不从事文员工作的员工编号、姓名、工作岗位和工资

select empno,ename,job,sal
from emp
where sal >any (select sal from emp where job = 'clerk')
and job<>'clerk';
mysql> select empno,ename,job,sal
    -> from emp
    -> where sal >any (select sal from emp where job = 'clerk')
    -> and job<>'clerk';
+-------+--------+-----------+------+
| empno | ename  | job       | sal  |
+-------+--------+-----------+------+
|  7499 | ALLEN  | SALESMAN  | 1600 |
|  7521 | WARD   | SALESMAN  | 1250 |
|  7566 | JONES  | MANAGER   | 2975 |
|  7654 | MARTIN | SALESMAN  | 1250 |
|  7698 | BLAKE  | MANAGER   | 2850 |
|  7782 | CLARK  | MANAGER   | 2450 |
|  7788 | SCOTT  | ANALYST   | 3000 |
|  7839 | KING   | PRESIDENT | 5000 |
|  7844 | TURNER | SALESMAN  | 1500 |
|  7902 | FORD   | ANALYST   | 3000 |
+-------+--------+-----------+------+
10 rows in set (0.00 sec)

2.45.查询工资高于部门编号是30的部门内某个雇员工资,但不在该部门工作的员工姓名、工作岗位、工资和部门编号

select empno,ename,job,sal
from emp
where sal >any (select sal from emp where deptno = 30)
and deptno<>30;
mysql> select empno,ename,job,sal
    -> from emp
    -> where sal >any (select sal from emp where deptno = 30)
    -> and deptno<>30;
+-------+--------+-----------+------+
| empno | ename  | job       | sal  |
+-------+--------+-----------+------+
|  7566 | JONES  | MANAGER   | 2975 |
|  7782 | CLARK  | MANAGER   | 2450 |
|  7788 | SCOTT  | ANALYST   | 3000 |
|  7839 | KING   | PRESIDENT | 5000 |
|  7876 | ADAMS  | CLERK     | 1100 |
|  7902 | FORD   | ANALYST   | 3000 |
|  7934 | MILLER | CLERK     | 1300 |
+-------+--------+-----------+------+
7 rows in set (0.00 sec)

2.46.查询工资低于部门名称是SALES的部门内某个雇员工资,但不在该部门工作的员工姓名、工作岗位、工资和部门编号和部门名称

select e.empno,e.ename,e.job,e.sal,d.dname
from emp e,dept d
where e.deptno = d.deptno
and d.dname<>'sales'
and e.sal <any (select distinct e.sal from emp e,dept d where e.deptno = d.deptno and dname = 'sales');
mysql> select e.empno,e.ename,e.job,e.sal,d.dname
    -> from emp e,dept d
    -> where e.deptno = d.deptno
    -> and d.dname<>'sales'
    -> and e.sal <any (select distinct e.sal from emp e,dept d where e.deptno = d.deptno and dname = 'sales');
+-------+--------+---------+------+------------+
| empno | ename  | job     | sal  | dname      |
+-------+--------+---------+------+------------+
|  7369 | SMITH  | CLERK   |  800 | RESEARCH   |
|  7782 | CLARK  | MANAGER | 2450 | ACCOUNTING |
|  7876 | ADAMS  | CLERK   | 1100 | RESEARCH   |
|  7934 | MILLER | CLERK   | 1300 | ACCOUNTING |
+-------+--------+---------+------+------------+
4 rows in set (0.00 sec)

2.47.使用all运算符的多行子查询【语法】

All运算符将和内部查询返回的每个结果比较。

  • >all:比最大的大;
  • <all:比最小的小。

2.48.查询高于所有部门平均工资的雇员姓名、工作、工资和部门编号

select ename,job,sal,deptno
from emp
where sal >all (select avg(sal) from emp group by deptno);
mysql> select ename,job,sal,deptno
    -> from emp
    -> where sal >all (select avg(sal) from emp group by deptno);
+-------+-----------+------+--------+
| ename | job       | sal  | deptno |
+-------+-----------+------+--------+
| JONES | MANAGER   | 2975 |     20 |
| SCOTT | ANALYST   | 3000 |     20 |
| KING  | PRESIDENT | 5000 |     10 |
| FORD  | ANALYST   | 3000 |     20 |
+-------+-----------+------+--------+
4 rows in set (0.00 sec)

2.49.查询低于所有部门平均工资的雇员姓名、工作、工资和部门编号

select ename,job,sal,deptno
from emp
where sal <all (select avg(sal) from emp group by deptno);
mysql> select ename,job,sal,deptno
    -> from emp
    -> where sal <all (select avg(sal) from emp group by deptno);
+--------+----------+------+--------+
| ename  | job      | sal  | deptno |
+--------+----------+------+--------+
| SMITH  | CLERK    |  800 |     20 |
| WARD   | SALESMAN | 1250 |     30 |
| MARTIN | SALESMAN | 1250 |     30 |
| TURNER | SALESMAN | 1500 |     30 |
| ADAMS  | CLERK    | 1100 |     20 |
| JAMES  | CLERK    |  950 |     30 |
| MILLER | CLERK    | 1300 |     10 |
+--------+----------+------+--------+
7 rows in set (0.00 sec)

2.50.查询工资高于部门编号为30的部门内所有员工工资的雇员姓名、工作、工资和部门编号

select ename,job,sal,deptno
from emp
where sal >all (select distinct sal from emp where deptno = 30);
mysql> select ename,job,sal,deptno
    -> from emp
    -> where sal >all (select distinct sal from emp where deptno = 30);
+-------+-----------+------+--------+
| ename | job       | sal  | deptno |
+-------+-----------+------+--------+
| JONES | MANAGER   | 2975 |     20 |
| SCOTT | ANALYST   | 3000 |     20 |
| KING  | PRESIDENT | 5000 |     10 |
| FORD  | ANALYST   | 3000 |     20 |
+-------+-----------+------+--------+
4 rows in set (0.00 sec)

2.51.查询工资等级为4的雇员姓名、工作、工资、部门编号和工资等级,同时满足该雇员工资高于部门编号为30的部门内所有员工工资

select e.ename,e.job,e.sal,e.deptno,s.grade
from emp e,salgrade s
where s.grade = 4
and e.sal between s.losal and s.hisal
and e.sal >all (select distinct sal from emp where deptno = 30);
mysql> select e.ename,e.job,e.sal,e.deptno,s.grade
    -> from emp e,salgrade s
    -> where s.grade = 4
    -> and e.sal between s.losal and s.hisal
    -> and e.sal >all (select distinct sal from emp where deptno = 30);
+-------+---------+------+--------+-------+
| ename | job     | sal  | deptno | grade |
+-------+---------+------+--------+-------+
| JONES | MANAGER | 2975 |     20 |     4 |
| SCOTT | ANALYST | 3000 |     20 |     4 |
| FORD  | ANALYST | 3000 |     20 |     4 |
+-------+---------+------+--------+-------+
3 rows in set (0.00 sec)

2.52.查询各部门中工资等级最高的雇员姓名、工作、工资、工资等级和部门号

select max(s.grade),e.deptno
from emp e,salgrade s
where e.sal between s.losal and s.hisal
group by e.deptno;
                           
select e.ename,e.job,e.sal,s.grade,e.deptno
from emp e,salgrade s
where e.sal between s.losal and s.hisal
and (s.grade,e.deptno) in (
	select max(s.grade),e.deptno
	from emp e,salgrade s
	where e.sal between s.losal and s.hisal
	group by e.deptno)
order by e.deptno;
mysql> select max(s.grade),e.deptno
    -> from emp e,salgrade s
    -> where e.sal between s.losal and s.hisal
    -> group by e.deptno;
+--------------+--------+
| max(s.grade) | deptno |
+--------------+--------+
|            5 |     10 |
|            4 |     20 |
|            4 |     30 |
+--------------+--------+
3 rows in set (0.00 sec)

mysql> select e.ename,e.job,e.sal,s.grade,e.deptno
    -> from emp e,salgrade s
    -> where e.sal between s.losal and s.hisal
    -> and (s.grade,e.deptno) in (
    -> select max(s.grade),e.deptno
    -> from emp e,salgrade s
    -> where e.sal between s.losal and s.hisal
    -> group by e.deptno)
    -> order by e.deptno;
+-------+-----------+------+-------+--------+
| ename | job       | sal  | grade | deptno |
+-------+-----------+------+-------+--------+
| KING  | PRESIDENT | 5000 |     5 |     10 |
| FORD  | ANALYST   | 3000 |     4 |     20 |
| SCOTT | ANALYST   | 3000 |     4 |     20 |
| JONES | MANAGER   | 2975 |     4 |     20 |
| BLAKE | MANAGER   | 2850 |     4 |     30 |
+-------+-----------+------+-------+--------+
5 rows in set (0.00 sec)

2.53.查询各部门中工资等级最高的雇员姓名、工作、工资、工资等级和部门名称

select max(s.grade),e.deptno
from emp e,salgrade s
where e.sal between s.losal and s.hisal
group by e.deptno;

select e.ename,e.job,e.sal,s.grade,d.dname
from emp e,dept d,salgrade s
where e.sal between s.losal and s.hisal
and e.deptno = d.deptno
and (s.grade,e.deptno) in (
	select max(s.grade),e.deptno
	from emp e,salgrade s
	where e.sal between s.losal and s.hisal
	group by e.deptno)
order by e.deptno;
mysql> select e.ename,e.job,e.sal,s.grade,d.dname
    -> from emp e,dept d,salgrade s
    -> where e.sal between s.losal and s.hisal
    -> and e.deptno = d.deptno
    -> and (s.grade,e.deptno) in (
    -> select max(s.grade),e.deptno
    -> from emp e,salgrade s
    -> where e.sal between s.losal and s.hisal
    -> group by e.deptno)
    -> order by e.deptno;
+-------+-----------+------+-------+------------+
| ename | job       | sal  | grade | dname      |
+-------+-----------+------+-------+------------+
| KING  | PRESIDENT | 5000 |     5 | ACCOUNTING |
| JONES | MANAGER   | 2975 |     4 | RESEARCH   |
| SCOTT | ANALYST   | 3000 |     4 | RESEARCH   |
| FORD  | ANALYST   | 3000 |     4 | RESEARCH   |
| BLAKE | MANAGER   | 2850 |     4 | SALES      |
+-------+-----------+------+-------+------------+
5 rows in set (0.00 sec)

2.54.查询工资与30部门中任意一个雇员的工资相等,同时奖金也与30部门中任意一个雇员奖金相等的雇员姓名、工资、奖金、部门编号,但该雇员不是来自30号部门

select ename,sal,comm,deptno
from emp
where sal in (select sal from emp where deptno = 30)
and comm in (select comm from emp where deptno = 30 and comm is not null)
and comm is not null
and comm > 0;
mysql> select ename,sal,comm,deptno
    -> from emp
    -> where sal in (select sal from emp where deptno = 30)
    -> and comm in (select comm from emp where deptno = 30 and comm is not null)
    -> and comm is not null
    -> and comm > 0;
+--------+------+------+--------+
| ename  | sal  | comm | deptno |
+--------+------+------+--------+
| ALLEN  | 1600 |  300 |     30 |
| WARD   | 1250 |  500 |     30 |
| MARTIN | 1250 | 1400 |     30 |
+--------+------+------+--------+
3 rows in set (0.00 sec)

2.55.查询在纽约(NEW YORK)工作的雇员姓名、工作岗位、工资和奖金

select ename,job,sal,comm
from emp
where deptno = (select deptno from dept where loc = 'new york');
mysql> select ename,job,sal,comm
    -> from emp
    -> where deptno = (select deptno from dept where loc = 'new york');
+--------+-----------+------+------+
| ename  | job       | sal  | comm |
+--------+-----------+------+------+
| CLARK  | MANAGER   | 2450 | NULL |
| KING   | PRESIDENT | 5000 | NULL |
| MILLER | CLERK     | 1300 | NULL |
+--------+-----------+------+------+
3 rows in set (0.00 sec)

2.56.显示工资高于部门平均工资的雇员姓名、工作、工资和部门号

-- 在from子句中使用子查询时,必须给子查询指定别名

select ename,job,sal,emp.deptno
from emp,(select deptno,avg(sal) avgsal from emp group by deptno) t
where emp.deptno = t.deptno
and sal > t.avgsal;
mysql> select ename,job,sal,emp.deptno
    -> from emp,(select deptno,avg(sal) avgsal from emp group by deptno) t
    -> where emp.deptno = t.deptno
    -> and sal > t.avgsal;
+-------+-----------+------+--------+
| ename | job       | sal  | deptno |
+-------+-----------+------+--------+
| ALLEN | SALESMAN  | 1600 |     30 |
| JONES | MANAGER   | 2975 |     20 |
| BLAKE | MANAGER   | 2850 |     30 |
| SCOTT | ANALYST   | 3000 |     20 |
| KING  | PRESIDENT | 5000 |     10 |
| FORD  | ANALYST   | 3000 |     20 |
+-------+-----------+------+--------+
6 rows in set (0.00 sec)

2.57.查询各部门中工资等级最高的雇员姓名、工作、工资、工资等级和部门号

select max(s.grade) maxgrade,e.deptno 
from emp e,salgrade s 
where e.sal between s.losal and s.hisal 
group by e.deptno;

select e.ename,e.job,e.sal,s.grade,e.deptno
from emp e,salgrade s,(
	select max(s.grade) maxgrade,e.deptno 
	from emp e,salgrade s 
	where e.sal between s.losal and s.hisal 
	group by e.deptno) p
where e.sal between s.losal and s.hisal
and e.deptno = p.deptno
and s.grade = p.maxgrade
order by e.deptno;
mysql> select e.ename,e.job,e.sal,s.grade,e.deptno
    -> from emp e,salgrade s,(
    -> select max(s.grade) maxgrade,e.deptno 
    -> from emp e,salgrade s 
    -> where e.sal between s.losal and s.hisal 
    -> group by e.deptno) p
    -> where e.sal between s.losal and s.hisal
    -> and e.deptno = p.deptno
    -> and s.grade = p.maxgrade
    -> order by e.deptno;
+-------+-----------+------+-------+--------+
| ename | job       | sal  | grade | deptno |
+-------+-----------+------+-------+--------+
| KING  | PRESIDENT | 5000 |     5 |     10 |
| SCOTT | ANALYST   | 3000 |     4 |     20 |
| JONES | MANAGER   | 2975 |     4 |     20 |
| FORD  | ANALYST   | 3000 |     4 |     20 |
| BLAKE | MANAGER   | 2850 |     4 |     30 |
+-------+-----------+------+-------+--------+
5 rows in set (0.00 sec)

2.58.查询平均工资最低的工种名称及其平均工资

-- 1.按工种查询平均工资
select job,avg(sal) from emp group by job;

-- 2.按工种查询最低平均工资
select min(avgsal) from (select avg(sal) avgsal from emp e group by e.job) as t;

-- 3.使用having子句把(2)作为(1)子查询
select job,avg(sal)
from emp
group by job
having avg(sal) = (select min(avgsal) from (select avg(sal) avgsal from emp e group by e.job) as t);
mysql> select job,avg(sal) from emp group by job;
+-----------+--------------------+
| job       | avg(sal)           |
+-----------+--------------------+
| ANALYST   |               3000 |
| CLERK     |             1037.5 |
| MANAGER   | 2758.3333333333335 |
| PRESIDENT |               5000 |
| SALESMAN  |               1400 |
+-----------+--------------------+
5 rows in set (0.00 sec)

mysql> select min(avgsal) from (select avg(sal) avgsal from emp e group by e.job) as t;
+-------------+
| min(avgsal) |
+-------------+
|      1037.5 |
+-------------+
1 row in set (0.00 sec)

mysql> select job,avg(sal)
    -> from emp
    -> group by job
    -> having avg(sal) = (select min(avgsal) from (select avg(sal) avgsal from emp e group by e.job) as t);
+-------+----------+
| job   | avg(sal) |
+-------+----------+
| CLERK |   1037.5 |
+-------+----------+
1 row in set (0.00 sec)

2.59.查询与部门编号为30的员工工资水平相同的员工信息

select avg(sal) from emp where deptno = 30;

select grade from salgrade where (select avg(sal) from emp where deptno = 30) between losal and hisal;

select * 
from emp
inner join salgrade s
on sal between s.losal and s.hisal
where s.grade = 
(select grade from salgrade where (select avg(sal) from emp where deptno = 30) between losal and hisal);
mysql> select avg(sal) from emp where deptno = 30;
+--------------------+
| avg(sal)           |
+--------------------+
| 1566.6666666666667 |
+--------------------+
1 row in set (0.00 sec)

mysql> select grade from salgrade where (select avg(sal) from emp where deptno = 30) between losal and hisal;
+-------+
| grade |
+-------+
|     3 |
+-------+
1 row in set (0.00 sec)

mysql> select * 
    -> from emp
    -> inner join salgrade s
    -> on sal between s.losal and s.hisal
    -> where s.grade = 
    -> (select grade from salgrade where (select avg(sal) from emp where deptno = 30) between losal and hisal);
+-------+--------+----------+------+------------+------+------+--------+-------+-------+-------+
| empno | ename  | job      | mgr  | hiredate   | sal  | comm | deptno | grade | losal | hisal |
+-------+--------+----------+------+------------+------+------+--------+-------+-------+-------+
|  7499 | ALLEN  | SALESMAN | 7698 | 1981-02-20 | 1600 |  300 |     30 |     3 |  1401 |  2000 |
|  7844 | TURNER | SALESMAN | 7698 | 1981-09-08 | 1500 |    0 |     30 |     3 |  1401 |  2000 |
+-------+--------+----------+------+------------+------+------+--------+-------+-------+-------+
2 rows in set (0.00 sec)

2.60.查询部门号、部门名、部门所在位置及其每个部门的员工总数

select d.deptno,d.dname,d.loc,count(e.deptno)
from emp e
inner join dept d
on e.deptno = d.deptno
group by d.deptno;
mysql> select d.deptno,d.dname,d.loc,count(e.deptno)
    -> from emp e
    -> inner join dept d
    -> on e.deptno = d.deptno
    -> group by d.deptno;
+--------+------------+----------+-----------------+
| deptno | dname      | loc      | count(e.deptno) |
+--------+------------+----------+-----------------+
|     10 | ACCOUNTING | NEW YORK |               3 |
|     20 | RESEARCH   | DALLAS   |               5 |
|     30 | SALES      | CHICAGO  |               6 |
+--------+------------+----------+-----------------+
3 rows in set (0.00 sec)

2.61.计算员工的年薪并且以年薪排序

select (sal*12+(if(comm is null,0,comm))) from emp;

select e.*,(select (sal*12+(if(comm is null,0,comm))) y_sal from emp where e.empno = emp.empno) y_sal
from emp e
order by y_sal asc;
mysql> select e.*,(select (sal*12+(if(comm is null,0,comm))) y_sal from emp where e.empno = emp.empno) y_sal
    -> from emp e
    -> order by y_sal asc;
+-------+--------+-----------+------+------------+------+------+--------+-------+
| empno | ename  | job       | mgr  | hiredate   | sal  | comm | deptno | y_sal |
+-------+--------+-----------+------+------------+------+------+--------+-------+
|  7369 | SMITH  | CLERK     | 7902 | 1980-12-17 |  800 | NULL |     20 |  9600 |
|  7900 | JAMES  | CLERK     | 7698 | 1981-12-03 |  950 | NULL |     30 | 11400 |
|  7876 | ADAMS  | CLERK     | 7788 | 1987-07-13 | 1100 | NULL |     20 | 13200 |
|  7521 | WARD   | SALESMAN  | 7698 | 1981-02-22 | 1250 |  500 |     30 | 15500 |
|  7934 | MILLER | CLERK     | 7782 | 1981-01-23 | 1300 | NULL |     10 | 15600 |
|  7654 | MARTIN | SALESMAN  | 7698 | 1981-09-28 | 1250 | 1400 |     30 | 16400 |
|  7844 | TURNER | SALESMAN  | 7698 | 1981-09-08 | 1500 |    0 |     30 | 18000 |
|  7499 | ALLEN  | SALESMAN  | 7698 | 1981-02-20 | 1600 |  300 |     30 | 19500 |
|  7782 | CLARK  | MANAGER   | 7839 | 1981-06-09 | 2450 | NULL |     10 | 29400 |
|  7698 | BLAKE  | MANAGER   | 7839 | 1981-05-01 | 2850 | NULL |     30 | 34200 |
|  7566 | JONES  | MANAGER   | 7839 | 1981-04-02 | 2975 | NULL |     20 | 35700 |
|  7788 | SCOTT  | ANALYST   | 7566 | 1987-07-03 | 3000 | NULL |     20 | 36000 |
|  7902 | FORD   | ANALYST   | 7566 | 1981-12-03 | 3000 | NULL |     20 | 36000 |
|  7839 | KING   | PRESIDENT | NULL | 1981-11-17 | 5000 | NULL |     10 | 60000 |
+-------+--------+-----------+------+------------+------+------+--------+-------+
14 rows in set (0.00 sec)

endl

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

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

相关文章

Flink 动态表 (Dynamic Table) 解读

博主历时三年精心创作的《大数据平台架构与原型实现&#xff1a;数据中台建设实战》一书现已由知名IT图书品牌电子工业出版社博文视点出版发行&#xff0c;点击《重磅推荐&#xff1a;建大数据平台太难了&#xff01;给我发个工程原型吧&#xff01;》了解图书详情&#xff0c;…

编译原理与技术(二)——词法分析(一)正则表达式

一、词法分析的概貌 一个程序&#xff0c;在我们看来往往是像下图这样的。 实际上&#xff0c;上面的程序本质上就是一个字符串&#xff0c;所以&#xff0c;它等价于下面这样的。 上面的字符串&#xff08;字符流&#xff09; &#xff0c;就是编译器接收到的程序的形式。 所…

数据库管理-第145期 最强Oracle监控EMCC深入使用-02(20240205)

数据库管理145期 2024-02-05 数据库管理-第145期 最强Oracle监控EMCC深入使用-02&#xff08;20240205&#xff09;1 监控方式2 度量配置3 阻塞4 DG监控总结 数据库管理-第145期 最强Oracle监控EMCC深入使用-02&#xff08;20240205&#xff09; 作者&#xff1a;胖头鱼的鱼缸&…

lombok的使用

lombok的使用 Lombok如何使用&#xff1f;导入POM依赖即可使用示例 Lombok Lombok是一个可以帮助我们简化Java代码编写的工具类&#xff0c;通过注解的方式&#xff0c;可以消除代码中的构造方法、getter/setter等代码&#xff0c;使写的类更加简洁。 如何使用&#xff1f;导入…

769933-15-5,Biotin aniline,用来标记和检测细胞膜上的特定蛋白质

您好&#xff0c;欢迎来到新研之家 文章关键词&#xff1a;769933-15-5&#xff0c;Biotin aniline&#xff0c;生物素苯胺 一、基本信息 产品简介&#xff1a;Biotin aniline, also known as Biotin aniline, is a molecular probe with strong reactivity. Its uniqueness…

goland中快捷关键词forr、main等不生效的问题记录

平时用的较多的一些快捷关键词&#xff0c;如forr会提示range循环并通过回车可自动打出来&#xff0c;main会提示main函数并通过回车可自动打出来&#xff0c;这种快捷处理能极大的提高编码效率。 但由于之前更改了一些配置&#xff0c;可能有的被覆盖失效了&#xff0c;重新修…

vue+vite项目,动态导入静态资源的几种方式

博主的桌面工具软件已经正式开发&#xff0c;获取方式&#xff1a; 可以关注我的小程序【中二少年工具箱】获取。&#xff08;若小程序更新有延迟&#xff0c;可先收藏小程序&#xff09; 通过下载链接 百度网盘: 链接&#xff1a;https://pan.baidu.com/s/15zDnSoEzJGSZLjpD…

【安卓跨程序共享数据,探究ContentProvider】

ContentProvider主要用于在不同的应用程序之间实现数据共享的功能&#xff0c;它提供了一套完整的机制&#xff0c;允许一个程序访问另一个程序中的数据&#xff0c;同时还能保证被访问数据的安全性。 目前&#xff0c;使用ContentProvider是Android实现跨程序共享数据的标准方…

003集—三调数据库添加三大类字段——arcgis

在国土管理日常统计工作中经常需要用到三大类数据&#xff08;农用地、建设用地、未利用地&#xff09;&#xff0c;而三调数据库中无三大类字段&#xff0c;因此需要手工录入三大类字段&#xff0c;并根据二级地类代码录入相关三大类名称。本代码可一键录入海量三大类名称统计…

性能实测:分布式存储 ZBS 与集中式存储 HDS 在 Oracle 数据库场景表现如何

作者&#xff1a;深耕行业的 SmartX 金融团队 金鑫 在金融客户的基础架构环境中&#xff0c;HDS 是一种被广泛使用的存储解决方案。作为集中式存储的代表之一&#xff0c;HDS 拥有高性能、高可用性和可扩展性的企业级存储特点&#xff0c;适用于实时数据处理、虚拟化和灾难备份…

c++入门学习④——对象的初始化和清理

目录 对象的初始化和清理&#xff1a; why? 如何进行初始化和清理呢&#xff1f; 使用构造函数和析构函数​编辑 构造函数语法: 析构函数语法: 构造函数的分类&#xff1a; 两种分类方式&#xff1a; 三种调用方法&#xff1a; 括号法&#xff08;默认构造函数调用&…

thinkphp数据批量提交(群发消息)

<form id="edit-form" class="form-horizontal" role="form" data-toggle<

PHP实现DESede/ECB/PKCS5Padding加密算法兼容Java SHA1PRNG

这里写自定义目录标题 背景JAVA代码解决思路PHP解密 背景 公司PHP开发对接一个Java项目接口&#xff0c;接口返回数据有用DESede/ECB/PKCS5Padding加密&#xff0c;并且key也使用了SHA1PRNG加密了&#xff0c;网上找了各种办法都不能解密&#xff0c;耗了一两天的时间&#xf…

【Qt学习笔记】(三)常用控件(持续更新)

Qt 常用控件 1 控件概述2 QWidget 控件核心属性2.1 enabled2.2 geometry2.3 window frame 的影响2.4 windowTitle2.5 window Icon2.6 windowOpacity2.7 cursor2.8 font2.9 toolTip2.10 focusPolicy2.11 stylesheet 1 控件概述 Widget是Qt中的核心概念英文原义是"小部件&q…

netty-websocket扩展协议及token鉴权补充

文章源码&#xff1a;gitee 源码部分可以看上一篇文章中的源码分析netty-websocket 鉴权token及统一请求和响应头&#xff08;鉴权控制器&#xff09; 最近刚好没事&#xff0c;看到有朋友说自定义协议好搞&#xff0c;我就想了想&#xff0c;发现上面那种方式实现确实麻烦&…

假期刷题打卡--Day24

1、MT1198阶乘差 求1!-2!-3!-…-n! 格式 输入格式&#xff1a; 输入为整型 输出格式&#xff1a; 输出为整型 样例 1 输入&#xff1a; 5输出&#xff1a; -151 分析过程 看到这个题目的时候&#xff0c;感觉这个题目出现的没有必要&#xff0c;就和前面阶乘和一样的…

jbdc的简单了解

JDBC JDBC所处的位置 JDBC的本质 Java操作数据库的一套接口。 补充 ddl:数据库定义语言,例如建表,创建数据库等。 dml:数据库操作语言,例如增删改。 dql:数据库查询语言,例如查询语句。 注意 在创建Java项目后的第一个步骤是导入jar包。 导入jar包的步骤 1 创建l…

华为视频监控接入到视频监控平台 (华为网路监控摄像机IPC和华为视频节点设备VCN)

目 录 一、设备介绍 1.1 华为VCN介绍 1.2 AS-V1000视频监控平台介绍 1.3 平台服务器配置说明 二、安装、配置HW_IVS软件 2.1下载安装HW_IVS软件 2.2登录HW_IVS 2.3共享到外域 三、配置华为外域参数 3.1 PCG模块设置 3.2通信协议GBT28181配置 3.3传…

ChatGPT 4.0 升级指南, ChatGPT Plus(GPT 4.0) 有何优势?

1.ChatGPT 是什么&#xff1f; ChatGPT 是由 OpenAI 开发的一种基于人工智能的聊天机器人&#xff0c;它基于强大的语言处理模型 GPT&#xff08;Generative Pre-trained Transformer&#xff09;构建。它能够理解人类语言&#xff0c;可以为我们解决实际的问题。 ChatGPT 4.…

网络选择流程分析(首选网络类型切换流程)

首先是界面,我在此平台的界面如下: 对应的入口源码位置在Settings的UniEnabledNetworkModePreferenceController中,当然其他平台可能在PreferredNetworkModePreferenceController中,流程上都是大同小异 然后点击切换按钮会调用到UniEnabledNetworkModePreferenceControlle…