mysql查询员工最高最低工资三种方式

首先建立部门表和员工表并插入数据

DROP DATABASE IF EXISTS teaching ;
CREATE DATABASE teaching CHARACTER SET utf8 COLLATE utf8_general_ci;
USE teaching;

-- 创建部门表
CREATE TABLE IF NOT EXISTS depts(
dept_id INT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
description VARCHAR(200)
);
-- 插入部门数据
INSERT INTO depts VALUES
(1,'人事部','NULL'),
(2,'研发部','NULL'),
(3,'市场部','NULL'),
(4,'培训部','NULL');

-- 创建员工表
CREATE TABLE IF NOT EXISTS employees(
employee_id INT PRIMARY KEY,
NAME VARCHAR(20) NOT NULL,
gender ENUM('M','F') DEFAULT 'M',
dept_id INT,
join_time DATETIME,
salary DECIMAL(8,2),
address VARCHAR(200),
image BLOB,
description VARCHAR(250)
);

-- 插入员工数据
INSERT INTO employees VALUES
(1,'张三','M',1,'2008-02-02 00:00:00','3500.00','北京','',''),
(2,'李四','F',2,'2007-02-20 00:00:00','5000.00','上海','',''),
(3,'王五','M',3,'2006-12-20 00:00:00','7000.00','福建','',''),
(4,'赵六','F',2,'2008-06-06 00:00:00','2800.00','广东','',''),
(5,'钱七','M',4,'2005-08-21 00:00:00','8000.00','山东','',''),
(6,'孙八','F',1,'2008-04-16 00:00:00','2000.00','河北','NULL','这家伙很懒,什么也没说!');
mysql查询员工最高最低工资三种方式_第1张图片mysql查询员工最高最低工资三种方式_第2张图片

 

下面查询最高最低工资

(1)只显示工资

select max(salary) as 最高工资,min(salary) as 最低工资 from employees

mysql查询员工最高最低工资三种方式_第3张图片

(2)显示全部信息

select * from employees where employee_id =
(select employee_id from employees order by salary asc limit 1)
or
employee_id =
(select employee_id from employees order by salary desc limit 1)

mysql查询员工最高最低工资三种方式_第4张图片

(3)

select * from employees where employee_id in
(
    (select employee_id from employees order by salary asc limit 1),
    (select employee_id from employees order by salary desc limit 1)
)

mysql查询员工最高最低工资三种方式_第5张图片

你可能感兴趣的:(mysql)