数据库查询语句练习题二(员工部门表)

表结构
dept(部门表)

deptno 部门编号
loc	 部门位置
dname	 部门名

emp(员工表)

empno	员工编号
ename	员工姓名
sal	员工奖金
comm	员工佣金
job	员工职务
mgr	员工上司编号
deptno	员工所属部门

创建表并插入信息

创建部门表
create table dept
(
deptno	int(11)	not null primary key,
loc	varchar(300),
dname	varchar(100)
)DEFAULT CHARSET=gb2312;

创建员工表
CREATE TABLE emp
(
empno INT NOT NULL PRIMARY KEY,
ename VARCHAR(30),
job VARCHAR(30),
mgr INT,
datet DATETIME,
sal FLOAT,
comm FLOAT,
deptno INT
)DEFAULT CHARSET=gb2312;

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');

INSERT INTO emp VALUES(7369,'SMITH','CLERK'/*职员*/,7902,'1980-12-17',1456.00,800.00,20);
INSERT INTO emp VALUES(7499,'ALLEN','SALESMAN'/*推销员*/,7698,'1981-2-20',1600.00,300.00,30);
INSERT INTO emp VALUES(7521,'WARD','SALESMAN',7698,'1981-2-22',1250.00,500.00,30);
INSERT INTO emp VALUES(7566,'JONES','MANAGER'/*经理*/,7839,'1981-4-2',2975.00,2300.00,20);
INSERT INTO emp VALUES(7654,'MARTIN','SALESMAN',7698,'1981-9-28',1250.00,1400.00,30);
INSERT INTO emp VALUES(7698,'BLAKE','MANAGER',7839,'1981-5-1',2850.00,1020.00,30);
INSERT INTO emp VALUES(7782,'CLARK','MANAGER',7839,'1981-6-9',2450.00,200.00,10);
INSERT INTO emp VALUES(7788,'SCOTT','ANALYST',7566,'1987--4-19',3000.00,2000.00,20);
INSERT INTO emp VALUES(7839,'KING','PRESIDENT'/*总裁*/,7765,'1981-11-17',5000.00,1500.00,10);
INSERT INTO emp VALUES(7844,'TURNER','SALESMAN',7698,'1981-9-8',1500.00,0.00,30);
INSERT INTO emp VALUES(7876,'ADAMS','CLERK',7788,'1987-5-23',11000.00,2500,20);
INSERT INTO emp VALUES(7900,'JAMES','CLERK',7698,'1981-12-3',9500.00,600.00,30);
INSERT INTO emp VALUES(7902,'FORD','ANALYST',7566,'1981-12-3',3000.00,500.00,20);
INSERT INTO emp VALUES(7934,'MILLER','CLERK',7782,'1982-1-23',1300.00,400.00,10);
INSERT INTO emp VALUES(9999,'SHUNPING','CLERK',7782,'1988-5-5',2456.34,55.66,10);

习题:

1:选择部门编号为30的雇员
select * from emp where deptno=30;
2:列出所有办事员的姓名、编号和部门
select ename,empno,dname from dept d,emp e where d.deptno = e.deptno
and job='CLERK';
3:找出佣金高于薪金的雇员
select ename from emp where comm>sal;
4:找出佣金高于薪金60%的雇员
select ename from emp where comm>sal*0.6;
5:找出部门10中所有经理和部门20中所有办事员的详细资料
select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK');
或
select * from emp where deptno=10 and job='MANAGER'
union
select * from emp where deptno=20 and job='CLERK';
6:找出部门10中所有经理和部门20中所有办事员以及既不是经理又不是办事员但其薪金大于或等于2000的所有雇员的详细资料
select * from emp where (deptno=10 and job='MANAGER') or (deptno=20 and job='CLERK') 
or (job<>'MANAGER' and job!='CLERK' and sal>=2000);
或
select * from emp where deptno=10 and job='MANAGER'
union
select * from emp where deptno=20 and job='CLERK'
union
select * from emp where job not in('MANAGER','CLERK') and sal>=2000;
7:找出收取佣金的雇员的不同工作
select distinct job from emp where comm is  not null;
8:找出不收取佣金或收取佣金低于100的雇员
select ename from emp where comm=0 or comm is null or comm<100;
9:找出各月最后一天受雇的所有雇员
select * from emp where datet=last_day(datet);//last_day():表示每月最后一天的日期
10:找出早于12年之前受雇的雇员
select ename from emp where (year(now())-year(datet))>12;
方法二:
select ename from emp where datet

你可能感兴趣的:(数据库查询语句练习题二(员工部门表))