CREATE TABLE emp(
empno INT,
ename VARCHAR(7),
job VARCHAR(7),
mgr INT,
hiredate VARCHAR(20),
sal DOUBLE(18,2),
comm DOUBLE(18,2),
depton INT
)
SELECT * FROM emp;
INSERT INTO emp VALUES(1001,'甘宁','文员',1013,'2000-12-17',8000.00,NULL,20);
INSERT INTO emp VALUES(1002,'黛丽丝','销售员',1006,'2001-02-20',16000.00,3000.00,30);
INSERT INTO emp VALUES(1003,'殷天正','销售员',1006,'2001-02-22',12500.00,5000.00,30);
INSERT INTO emp VALUES(1004,'刘备','经理',1009,'2001-04-02',29750.00,NULL,20);
INSERT INTO emp VALUES(1005,'谢逊','销售员',1006,'2001-09-28',12500.00,14000.00,30);
INSERT INTO emp VALUES(1006,'关羽','经理',1009,'2001-05-01',28500.00,NULL,30);
INSERT INTO emp VALUES(1007,'张飞','经理',1009,'2001-09-01',24500.00,NULL,10);
INSERT INTO emp VALUES(1008,'诸葛亮','分析师',1004,'2007-04-19',30000.00,NULL,20);
INSERT INTO emp VALUES(1009,'曾阿牛','董事长',NULL,'2001-11-17',50000.00,NULL,10);
INSERT INTO emp VALUES(1010,'韦一笑','销售员',1006,'2001-09-08',15000.00,0.00,30);
INSERT INTO emp VALUES(1011,'周泰','文员',1008,'2007-05-23',11000.00,NULL,20);
INSERT INTO emp VALUES(1012,'程普','文员',1006,'2001-12-03',9500.00,NULL,30);
INSERT INTO emp VALUES(1013,'庞统','分析员',1004,'2001-12-03',30000.00,NULL,20);
INSERT INTO emp VALUES(1014,'黄盖','文员',1007,'2002-01-23',13000.00,NULL,10);
INSERT INTO emp VALUES(1015,'张三','保洁员',1001,'2013-05-01',80000.00,50000.00,50);
/*1查询出部门编号为30的所有员工*/
SELECT * FROM emp WHERE depton = 30;
/*2所有销售员的姓名、编号和部门编号*/
SELECT ename,depton,empno FROM emp WHERE job = '销售员';
/*3找出奖金高于工资的员工*/
SELECT * FROM emp WHERE comm>sal;
/*4找出奖金高于工资60%的员工*/
SELECT * FROM emp WHERE comm>sal*(0.6);
/*5找出部门编号为10中所有经理,和部门编号为20中所有销售员的详细资料*/
SELECT * FROM emp WHERE (job='经理' AND depton=10)OR(job='销售员' AND depton=20);
/*66. 找出部门编号为10中所有经理,部门编号为20中所有销售员,还有即不是经理又不是销售员但其工资大或等于20000的所有员工详细资料。*/
SELECT * FROM emp WHERE (job='经理' AND depton=10)OR(job='销售员' AND depton=20)OR(job NOT IN('经理','销售员') AND sal>=20000);
/*77. 无奖金或奖金低于1000的员工。*/
SELECT * FROM emp WHERE comm IS NULL OR comm<1000;
/*8查询名字由三个字组成的员工。*/
SELECT * FROM emp WHERE ename LIKE '___';
/*9查询2000年入职的员工*/
SELECT * FROM emp WHERE hiredate LIKE '2000-%';
/*10查询所有员工详细信息,用编号升序排序*/
SELECT * FROM emp ORDER BY empno;
/*11查询所有员工详细信息,用工资降序排序,如果工资相同使用入职日期升序排序*/
SELECT * FROM emp ORDER BY sal DESC, hiredate ASC;
/*1212. 查询姓周的两个名字的员工*/
SELECT * FROM emp WHERE ename LIKE'周_';
/*1313. 查询所有姓张的员工*/
SELECT * FROM emp WHERE ename LIKE '张%';
/*1414. 查询该公司有多少个岗位,每个岗位有多少人*/
SELECT job, COUNT(*) FROM emp GROUP BY job;
/*15查询哪个岗位中人数大于3*/
SELECT job, COUNT(*) FROM emp GROUP BY job HAVING COUNT(*)>3;