mysql 6.3 入门_Mysql 入门小练习

7.1 查询所有雇员月薪和: SELECT SUM(sal) FROM emp; 7.2 查询所…

7.1 查询所有雇员月薪和:

SELECT SUM(sal) FROM emp;

7.2 查询所有雇员月薪和,以及所有雇员佣金和:

SELECT SUM(sal) AS '月薪和',SUM(comm) FROM emp;

7.3 查询所有雇员月薪+佣金和:

SELECT SUM(sal+IFNULL(comm,0)) AS '月需发工资' FROM emp;

7.4 统计所有员工平均工资:

SELECT AVG(sal) FROM emp;

7.5 查询最高工资和最低工资:

SELECT MAX(sal),MIN(sal) FROM emp;

7.6 查询每个部门的部门编号和每个部门的工资和:

SELECT deptno , SUM(sal) FROM emp GROUP BY deptno;

7.7 查询每个部门的部门编号以及每个部门的人数:

SELECT deptno,COUNT(empno) FROM emp GROUP BY deptno;

7.8 查询每个部门的部门编号以及每个部门工资大于1500的人数:

SELECT deptno,COUNT(*) FROM emp WHERE sal>1500 GROUP BY deptno ;

7.9 查询工资总和大于9000的部门编号以及工资和:

SELECT deptno , SUM(sal) FROM emp GROUP BY deptno HAVING SUM(sal)>9000;

8.1 查询5行记录,起始行从0开始

SELECT * FROM emp LIMIT 0,5;

8.2 查询10行记录,起始行从3开始

SELECT * FROM emp LIMIT 3,10;

9.1 工资高于JONES的员工。

SELECT e1.empno,e1.ename,e1.sal FROM emp e1 ,(SELECT sal FROM emp WHERE ename='JONES') e2 WHERE e1.sal >e2.sal;

9.2 查询与SCOTT同一个部门的员工。

SELECT ename FROM emp WHERE deptno = (SELECT deptno FROM emp WHERE ename='SCOTT');

9.3 工资高于30号部门所有人的员工信息

SELECT * FROM emp WHERE sal>(SELECT MAX(sal) FROM emp WHERE deptno=30);

9.4 查询工作和工资与MARTIN(马丁)完全相同的员工信息

SELECT *

FROM emp e1,(SELECT ename,job,sal FROM emp WHERE ename='MARTIN') e2

WHERE e1.job=e2.job AND e1.sal=e2.sal AND e1.ename != ('MARTIN');

9.5 有2个以上直接下属的员工信息

SELECT * FROM EMP WHERE empno IN(SELECT mgr FROM emp GROUP BY mgr HAVING COUNT(*)>2);

9.6 查询员工编号为7788的员工名称、员工工资、部门名称、部门地址

SELECT e.ename AS '员工姓名',e.sal AS '员工工资' , d.dname AS '部门名称',d.loc AS '部门地址'

FROM emp e,dept d

WHERE e.empno =7788 AND e.deptno = d.deptno;

9.7 求7369员工编号、姓名、经理编号和经理姓名

SELECT e.empno,e.ename,e.mgr,a.ename

FROM emp e, emp a

WHERE e.empno=7369 AND e.mgr = a.empno;

9.8 求各个部门薪水最高的员工所有信息

SELECT *

FROM emp e,(SELECT e2.deptno ,MAX(e2.sal) AS 'ss' FROM emp e2 GROUP BY e2.deptno) s

WHERE e.sal = s.ss AND e.deptno = s.deptno;

======================================================================

学生表

CREATE TABLE stu (

sid CHAR(6),

sname VARCHAR(50),

age INT,

gender VARCHAR(50)

INSERT INTO stu VALUES('S_1001', 'liuYi', 35, 'male');

INSERT INTO stu VALUES('S_1002', 'chenEr', 15, 'female');

INSERT INTO stu VALUES('S_1003', 'zhangSan', 95, 'male');

INSERT INTO stu VALUES('S_1004', 'liSi', 65, 'female');

INSERT INTO stu VALUES('S_1005', 'wangWu', 55, 'male');

INSERT INTO stu VALUES('S_1006', 'zhaoLiu', 75, 'female');

INSERT INTO stu VALUES('S_1007', 'sunQi', 25, 'male');

INSERT INTO stu VALUES('S_1008', 'zhouBa', 45, 'female');

INSERT INTO stu VALUES('S_1009', 'wuJiu', 85, 'male');

INSERT INTO stu VALUES('S_1010', 'zhengShi', 5, 'female');

INSERT INTO stu VALUES('S_1011', 'xxx', NULL, NULL);

============================================================================

部门表

CREATE TABLE dept(

deptno INT,

dname VARCHAR(14),

loc VARCHAR(13)

INSERT INTO dept VALUES(10, 'ACCOUNTING', 'NEW YORK');

INSERT INTO dept VALUES(20, 'RESEARCH', 'DALLAS');

INSERT INTO dept VALUES(30, 'SALES', 'CHICAGO');

INSERT INTO dept VALUES(40, 'OPERATIONS', 'BOSTON');

=============================================================================

雇员表

CREATE TABLE emp(

empno INT,

ename VARCHAR(50),

job VARCHAR(50),

mgr INT,

hiredate DATE,

sal DECIMAL(7,2),

comm DECIMAL(7,2),

deptno INT

) ;

INSERT INTO emp VALUES(7369,'SMITH','CLERK',7902,'1980-12-17',800,NULL,20);

INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN',7698,'1981-02-20',1600,300,30);

INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-02-22',1250,500,30);

INSERT INTO emp VALUES(7566,'JONES','MANAGER',7839,'1981-04-02',2975,NULL,20);

INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-09-28',1250,1400,30);

INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-05-01',2850,NULL,30);

INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-06-09',2450,NULL,10);

INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987-04-19',3000,NULL,20);

INSERT INTO emp VALUES(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,10);

INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-09-08',1500,0,30);

INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-05-23',1100,NULL,20);

INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-03',950,NULL,30);

INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-03',3000,NULL,20);

INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-01-23',1300,NULL,10);

本文来自网络,不代表软粉网立场,转载请注明出处:https://www.rfff.net/p/6048.html

作者: HUI

发表评论

您的电子邮箱地址不会被公开。

返回顶部