MySQL练习题
- 让emp表中所员工的工资 + 200, 同时显示之前的工资和加薪之后的工资
SELECT
sal,
(sal + 200)
FROM
emp;
- 员工试用期6个月,转正后月薪上调20%,请查询出所有员工工作第一年的所有收入,要求显示列标题为员工姓名,工资收入,奖金收入,总收入
SELECT
ename AS 员工姓名,
sal AS 工资收入,
ifnull(comm, 0) AS 奖金收入,
(
sal * 6 + sal * 1.2 * 6 + ifnull(comm, 0)
) AS 总收入
FROM
emp;
SELECT DISTINCT
job
FROM
emp;
- 查询职位为SALESMAN的员工编号、职位、入职日期。
SELECT
empno,
job,
hiredate
FROM
emp
WHERE
job = 'SALESMAN';
- 查询1985年12月31日之前入职的员工姓名及入职日期。
SELECT
empno,
hiredate
FROM
emp
WHERE
hiredate < '1985-12-31';
SELECT
empno,
deptno
FROM
emp
WHERE
deptno <> 10;
SELECT
ename,
hiredate
FROM
emp
WHERE
hiredate BETWEEN '1982-01-01'
AND '1985-01-01';
SELECT
ename,
sal
FROM
emp
WHERE
sal BETWEEN 3000
AND 5000;
- 查询经理编号为7902, 7566, 7788的员工姓名,经理编号
SELECT
ename,
mgr
FROM
emp
WHERE
mgr IN (7902, 7566, 7788);
SELECT
ename
FROM
emp
WHERE
ename LIKE 'W%';
SELECT
ename
FROM
emp
WHERE
ename LIKE "%T_";
SELECT
ename,
comm
FROM
emp
WHERE
comm IS NULL;
- 查询工资超过2000并且职位是 MANAGER或SALESMAN的员工姓名、职位、工资。
SELECT
ename,
job,
sal
FROM
emp
WHERE
sal > 2000
AND job IN ('MANAGER', 'SALESMAN');
- 查询部门在10或者20,并且工资在3000到5000之间的员工姓名、部门、工资。
SELECT
ename,
deptno,
sal
FROM
emp
WHERE
deptno IN (10, 20)
AND sal BETWEEN 3000
AND 5000;
- 查询入职日期在81年,并且职位不是SALES开头的员工姓名、入职日期、职位。
SELECT
ename,
hiredate,
job
FROM
emp
WHERE
hiredate BETWEEN '1981-01-01'
AND '1982-01-01'
AND job NOT LIKE 'SALES%';
- 查询职位为SALESMAN或MANAGER,部门编号为10或者20,
姓名包含A的员工姓名、职位、部门编号。
SELECT
ename,
job,
deptno
FROM
emp
WHERE
job IN ('SALESMAN', 'MANAGER')
AND deptno IN (10, 20)
AND ename LIKE 'A';
- 查询部门在20或30的员工姓名,部门编号,并按照工资升序排序。
SELECT
ename,
deptno,
sal
FROM
emp
WHERE
deptno IN (20, 30)
ORDER BY
sal;
- 查询工资在2000-3000之间,部门不在10号的员工姓名,部门编号,工资,并按照部门升序,工资降序排序。
SELECT
ename,
deptno,
sal
FROM
emp
WHERE
sal BETWEEN 2000
AND 3000
AND deptno NOT IN (10)
ORDER BY
deptno ASC,
sal DESC;
- 查询入职日期在81年至82年之间,职位以SALES或者MAN开头的员工姓名,入职日期,职位,并按照入职日期降序排序
SELECT
ename,
hiredate,
job
FROM
emp
WHERE
(
hiredate BETWEEN '1981-01-01'
AND '1983-01-01'
)
AND (
job LIKE 'SALES%'
OR job LIKE 'MAN%'
)
ORDER BY
hiredate DESC;
SELECT
ename,
hiredate
FROM
emp
ORDER BY
hiredate
LIMIT 5;
- 查询20号部门下入职日期最早的前2名员工姓名,入职日期。
SELECT
ename,
hiredate
FROM
emp
WHERE
deptno = 20
ORDER BY
hiredate
LIMIT 2;
- 按照每页显示5条记录,分别查询第1页,第2页,第3页信息,
要求显示员工姓名、入职日期、部门编号。
SELECT
ename,
hiredate,
deptno
FROM
emp
LIMIT 0,
5;
SELECT
ename,
hiredate,
deptno
FROM
emp
LIMIT 5,
5;
SELECT
ename,
hiredate,
deptno
FROM
emp
LIMIT 10,
5;
- 查询入职时间在1982-7-9之后,并且不从事SALESMAN工作的员工姓名、入职时间、职位。
SELECT
ename,
hiredate,
job
FROM
emp
WHERE
hiredate > '1982-07-09'
AND job <> 'SALESMAN';
SELECT
ename
FROM
emp
WHERE
ename LIKE '__a%';
- 查询除了10、20号部门以外的员工姓名、部门编号。
SELECT
ename,
deptno
FROM
emp
WHERE
deptno NOT IN (10, 20);
- 查询部门号为30号员工的信息,先按工资降序排序,再按姓名升序排序。
SELECT
*
FROM
emp
WHERE
deptno = 30
ORDER BY
sal DESC,
ename;
- 查询工资大于等于4500并且部门为10或者20的员工的姓名\工资、部门编号。
SELECT
ename,
sal,
deptno
FROM
emp
WHERE
sal >= 4500
AND deptno IN (10, 20);
- 写一个查询,显示所有员工姓名,部门编号,部门名称。
SELECT
e.ename,
e.empno,
e.job
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno;
- 写一个查询,显示所有工作在CHICAGO并且奖金不为空的员工姓名,工作地点,奖金
SELECT
e.ename,
d.loc,
e.comm
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno
AND d.loc = 'CHICAGO'
AND comm IS NOT NULL;
- 写一个查询,显示所有姓名中含有A字符的员工姓名,工作地点。
SELECT
e.ename,
d.loc
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno
AND e.ename LIKE '%A%';
SELECT
w.ename 员工姓名,
m.ename 领导姓名
FROM
emp w,
emp m
WHERE
w.mgr = m.empno;
- 查询所有雇员姓名,部门编号,部门名称,包括没有部门的员工也要显示出来(左外连接)
SELECT
e.ename,
e.deptno,
d.dname
FROM
emp e
LEFT OUTER JOIN dept d ON (e.deptno = d.deptno);
- 查询所有雇员姓名,部门编号,部门名称,包括没有员工的部门也要显示出来 (右外连接)
SELECT
e.ename,
e.deptno,
d.dname
FROM
emp e
RIGHT OUTER JOIN dept d ON (e.deptno = d.deptno);
SELECT
e.ename,
e.deptno,
d.deptno
FROM
emp e
CROSS JOIN dept d;
- 使用自然连接,显示入职日期在80年5月1日之后的员工姓名,部门名称,入职日期
SELECT
e.ename,
d.dname,
e.hiredate
FROM
emp e
NATURAL JOIN dept d
WHERE
hiredate > '1980-05-01';
- 使用左连接,查询每个员工的姓名,经理姓名,没有经理的King也要显示出来。
SELECT
w.ename,
m.ename
FROM
emp w
LEFT JOIN emp m ON w.mgr = m.empno;
- 使用右连接,查询每个员工的姓名,经理姓名,没有经理的King也要显示出来。
SELECT
w.ename,
m.ename
FROM
emp m
RIGHT JOIN emp w ON w.mgr = m.empno;
SELECT
w.ename 员工姓名,
m.ename 领导姓名,
d.dname 部门名称
FROM
emp w,
emp m,
dept d
WHERE
w.mgr = m.empno
AND w.deptno = d.deptno
AND w.ename = 'SMITH';
- 显示员工姓名,部门名称,工资,工资级别,要求工资级别大于4级。
SELECT
e.ename,
d.dname,
e.sal,
s.grade
FROM
emp e,
dept d,
salgrade s
WHERE
e.deptno = d.deptno
AND e.sal BETWEEN s.losal
AND s.hisal
AND s.grade > 4;
- 显示员工KING和FORD管理的员工姓名及其经理姓名。
SELECT
e.ename AS '管理的员工姓名',
m.ename AS '员工姓名',
p.ename AS '经理姓名'
FROM
emp e,
emp m
LEFT OUTER JOIN emp p ON (m.mgr = p.empno)
WHERE
m.ename IN ('KING', 'FORD')
AND e.mgr = m.empno;
- 显示员工姓名,参加工作时间,经理名,参加工作时间,要求参加时间比经理早。
SELECT
w.ename 员工姓名,
m.ename 经理姓名,
w.hiredate 员工入职时间,
m.hiredate 经理入职时间
FROM
emp w,
emp m
WHERE
w.mgr = m.empno
AND w.hiredate > m.hiredate;
SELECT
COUNT(comm)
FROM
emp
WHERE
deptno = 30;
SELECT
MIN(hirdate),
MAX(HIREDATE)
FROM
emp
- 查询职位以SALES开头的所有员工平均工资、最低工资、最高工资、工资和。
SELECT
AVG(SAL) 平均工资,
MIN(SAL) 最低工资,
MAX(SAL) 最高工资,
SUM(SAL) 工资和
FROM
emp
WHERE
JOB LIKE 'SALES%'
- IFNULL 函数可以使分组函数强制包含含有空值的记录
SELECT
COUNT(IFNULL(COMM,0))
FROM
emp;
SELECT
AVG(IFNULL(COMM,0))
FROM
emp;
SELECT
AVG(COMM)
FROM
emp;
SELECT
AVG(sal) 平均工资,
SUM(sal) 工资和
FROM
emp
WHERE
deptno = 20;
- 查询工作在CHICAGO的员工人数,最高工资及最低工资。
SELECT
COUNT(m.empno) 员工人数,
MAX(sal) 最高工资,
MIN(sal) 最低工资
FROM
emp m,
dept d
WHERE
m.deptno = d.DEPTNO
AND d.loc = 'CHICAGO';
SELECT
COUNT(DISTINCT JOB)
FROM
emp;
group by 语句
SELECT
deptno,
avg(sal)
FROM
emp
GROUP BY
deptno;
SELECT
deptno,
min(sal)
FROM
emp
GROUP BY
deptno;
SELECT
deptno,
max(sal)
FROM
emp
GROUP BY
deptno;
SELECT
deptno,
job,
sum(sal)
FROM
emp
GROUP BY
deptno,
job;
- 查询每个部门的部门编号,部门名称,部门人数,最高工资,最低工资,工资总和,平均工资。
SELECT
d.DEPTNO 部门编号,
d.DNAME 部门名称,
COUNT(*) 部门人数,
MAX(e.SAL) 最高工资,
MIN(e.SAL) 最低工资,
SUM(e.SAL) 工资总和,
AVG(e.SAL) 平均工资
FROM
emp e,
dept d
WHERE
e.DEPTNO = d.DEPTNO
GROUP BY
e.DEPTNO, d.DNAME;
- 查询每个部门,每个岗位的部门编号,部门名称,岗位名称,部门人数,最高工资,最低工资,工资总和,平均工资。
SELECT
d.DEPTNO 部门编号,
d.DNAME 部门名称,
e.JOB 岗位名称,
COUNT(*) 部门人数,
MAX(e.SAL) 最高工资,
MIN(e.SAL) 最低工资,
SUM(e.SAL) 工资总和,
AVG(e.SAL) 平均工资
FROM
emp e,
dept d
WHERE
e.DEPTNO = d.DEPTNO
GROUP BY
d.DEPTNO,d.DNAME, e.JOB;
- 查询每个经理所管理的人数,经理编号,经理姓名,要求包括没有经理的人员信息。
SELECT
COUNT(*) 管理人数,
m.EMPNO 经理编号,
m.ENAME 经理姓名
FROM
emp w
LEFT OUTER JOIN emp m ON w.MGR = m.EMPNO
GROUP BY
m.EMPNO,
m.ENAME
- 查询每个部门最高薪水大于2900的部门编号,最高工资
SELECT
e.DEPTNO,
MAX(e.sal)
FROM
emp e
GROUP BY
e.DEPTNO
HAVING
MAX(e.SAL) > 2900
- 查询每个工种, 工资总和, 并且工作不是以 SALE开头的且每个工种的工资和大于5000时输出,并按照结果的工资总数进行升序排序
SELECT
job 工种,
SUM(sal) 工资总和
FROM
emp
WHERE
job NOT LIKE 'SALES%'
GROUP BY
job
HAVING
SUM(sal) > 5000
ORDER BY
SUM(sal);
- 查询部门人数大于2的部门编号,部门名称,部门人数。
SELECT
e.DEPTNO 部门编号,
d.DNAME 部门名称,
COUNT(*) 部门人数
FROM
emp e,
dept d
WHERE
e.DEPTNO = d.DEPTNO
GROUP BY
e.DEPTNO
HAVING
COUNT(*) > 2
- 查询部门平均工资大于2000,且人数大于2的部门编号,部门名称,部门人数,部门平均工资,并按照部门人数升序排序。
SELECT
e.DEPTNO 部门编号,
d.DNAME 部门名称,
COUNT(*) 部门人数,
AVG(e.SAL) 部门平均工资
FROM
emp e,
dept d
WHERE
e.DEPTNO = d.DEPTNO
GROUP BY
e.DEPTNO
HAVING
(COUNT(*) > 2)
AND (AVG(e.SAL) > 2000)
ORDER BY
3
SELECT
*
FROM
emp e
WHERE
e.SAL > ( SELECT e.SAL from emp e WHERE e.ENAME = 'JONES');
SELECT
e.ENAME
FROM
emp e
WHERE
e.SAL = (SELECT MIN(e.sal) from emp e);
- 显示和工号7369从事相同工作并且工资大于7876的员工姓名和工作
SELECT e.ENAME, e.JOB
from emp e
WHERE
e.JOB = (SELECT JOB FROM emp WHERE EMPNO = 7369) and
e.sal > (SELECT SAL FROM emp WHERE EMPNO = 7876);
- 查询部门最低工资比20部门最低工资高的部门编号及最低工资
SELECT e.DEPTNO 部门编号 ,MIN(e.SAL)
from emp e
GROUP BY e.DEPTNO
HAVING
MIN(e.SAL) > (SELECT MIN(sal) from emp WHERE DEPTNO = 20)
- 查询入职日期比10部门任意一个员工晚的员工姓名、入职日期,不包括10部门员工
SELECT
e.ename AS '员工姓名',
e.hiredate AS '入职日期'
FROM
emp e
WHERE
e.deptno <> 10
AND e.hiredate > ANY (
SELECT
hiredate
FROM
emp
WHERE
deptno = 10
);
- 查询入职日期比10部门所有员工晚的员工姓名、入职日期,不包括10部门员工
SELECT
e.ename AS '员工姓名',
e.hiredate AS '入职日期'
FROM
emp e
WHERE
e.empno <> 10
AND e.hiredate > ALL (
SELECT
hiredate
FROM
emp
WHERE
deptno = 10
);
- 查询职位和10部门任意一个员工职位相同的员工姓名,职位,不包括10部门员工
SELECT
e.ename AS '员工姓名',
e.job AS '职位'
FROM
emp e
WHERE
e.deptno <> 10
AND e.job = ANY (
SELECT
job
FROM
emp
WHERE
deptno = 10
);
SELECT
d.dname AS '部门名称',
avg(e.sal) AS '平均工资'
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno
GROUP BY
e.deptno
HAVING
avg(e.sal) > 2500;
- 查询员工岗位中不是以“SA”开头并且平均工资在2500元以上的岗位及平均工资,并按平均工资降序排序。
SELECT
e.job AS '岗位',
avg(e.sal) AS '平均工资'
FROM
emp e
WHERE
e.job NOT LIKE 'SA%'
GROUP BY
e.job
HAVING
avg(e.sal) > 2500
ORDER BY
2 DESC;
- 查询部门人数在2人以上的部门名称、最低工资、最高工资。
SELECT
d.dname AS '部门名称',
min(e.sal) AS '最低工资',
max(e.sal) AS '最高工资'
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno
GROUP BY
d.deptno
HAVING
count(e.empno) > 2;
- 查询岗位不为SALESMAN,工资和大于等于2500的岗位及每种岗位的工资和。
SELECT
e.job AS '岗位',
sum(e.sal) AS '工资和'
FROM
emp e
WHERE
e.job <> 'SALESMAN'
GROUP BY
e.job
HAVING
sum(e.sal) > 2500;
- 显示经理号码和经理姓名,这个经理所管理员工的最低工资,没有经理的KING也要显示,不包括最低工资小于3000的,按最低工资由高到低排序
SELECT
m.empno AS '经理编号',
m.ename AS '经理姓名',
min(w.sal) AS '管理员工的最低工资'
FROM
emp w
LEFT OUTER JOIN emp m ON (w.mgr = m.empno)
GROUP BY
m.empno
HAVING
min(w.sal) > 3000
ORDER BY
3 DESC;
- 查询工资高于编号为7782的员工工资,并且和7369号员工从事相同工作的员工的编号、姓名及工资。
SELECT
e.empno AS '员工编号',
e.ename AS '员工姓名',
e.sal AS '员工工资'
FROM
emp e
WHERE
e.sal > (
SELECT
sal
FROM
emp
WHERE
empno = 7782
)
AND e.job = (
SELECT
job
FROM
emp
WHERE
empno = 7369
);
SELECT
e.ename AS '员工姓名',
e.sal AS '员工工资'
FROM
emp e
ORDER BY
e.sal DESC
LIMIT 1;
SELECT
e.ename AS '员工姓名',
e.sal AS '员工工资'
FROM
emp e
WHERE
e.sal = (SELECT max(sal) FROM emp);
- 查询部门最低工资高于10号部门最低工资的部门的编号、名称及部门最低工资。
SELECT
d.deptno AS '部门编号',
d.dname AS '部门名称',
min(e.sal) AS '部门最低工资'
FROM
emp e,
dept d
WHERE
e.deptno = d.deptno
GROUP BY
d.deptno
HAVING
min(e.sal) > (
SELECT
min(sal)
FROM
emp
WHERE
deptno = 10
);
- 查询员工工资为其部门最低工资的员工的编号和姓名及工资。
SELECT
e.empno AS '员工编号',
e.ename AS '员工姓名',
e.sal AS '员工工资'
FROM
emp e
WHERE
e.sal IN (
SELECT
min(e.sal)
FROM
emp e
GROUP BY
e.deptno
);
SELECT
w.ename AS '员工姓名',
w.sal AS '员工工资'
FROM
emp w,
emp m
WHERE
w.mgr = m.empno
AND m.ename = 'KING';
SELECT
w.ename AS '员工姓名',
w.sal AS '员工工资'
FROM
emp w
WHERE
w.mgr = (
SELECT
empno
FROM
emp
WHERE
ename = 'KING'
);
- 显示比员工SMITH参加工作时间晚的员工姓名,工资,参加工作时间。
SELECT
w.ename AS '员工姓名',
w.sal AS '员工工资',
w.hiredate AS '参加工作时间'
FROM
emp w
WHERE
w.hiredate > (
SELECT
hiredate
FROM
emp
WHERE
ename = 'SIMTH'
);