创建部门表:
CREATE TABLE DEPT(
DEPTNO INT PRIMARY KEY,
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 PRIMARY KEY,
ENAME VARCHAR(10),
JOB VARCHAR(9),
MGR INT,
HIREDATE DATE,
SAL DOUBLE,
COMM DOUBLE,
DEPTNO INT,
FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO));
插入数据:
-- ALTER TABLE EMP ADD FOREIGN KEY (DEPTNO) REFERENCES DEPT(DEPTNO);
INSERT INTO EMP VALUES(7369,'SMITH','职员',7566,"1980-12-17",800,NULL,20);
INSERT INTO EMP VALUES(7499,'ALLEN','销售员',7698,'1981-02-20',1600,300,30);
INSERT INTO EMP VALUES(7521,'WARD','销售员',7698,'1981-02-22',1250,500,30);
INSERT INTO EMP VALUES(7566,'JONES','经理',7839,'1981-04-02',2975,NULL,20);
INSERT INTO EMP VALUES(7654,'MARTIN','销售员',7698,'1981-09-28',1250,1400,30);
INSERT INTO EMP VALUES(7698,'BLAKE','经理',7839,'1981-05-01',2850,NULL,30);
INSERT INTO EMP VALUES(7782,'CLARK','经理',7839,'1981-06-09',2450,NULL,10);
INSERT INTO EMP VALUES(7788,'SCOTT','职员',7566,'1987-07-03',3000,2000,20);
INSERT INTO EMP VALUES(7839,'KING','董事长',NULL,'1981-11-17',5000,NULL,10);
INSERT INTO EMP VALUES(7844,'TURNERS','销售员',7698,'1981-09-08',1500,50,30);
INSERT INTO EMP VALUES(7876,'ADAMS','职员',7566,'1987-07-13',1100,NULL,20);
INSERT INTO EMP VALUES(7900,'JAMES','职员',7698,'1981-12-03',1250,NULL,30);
INSERT INTO EMP VALUES(7902,'FORD','销售员',7566,'1981-12-03',3000,NULL,20);
INSERT INTO EMP VALUES(7934,'MILLER','职员',7782,'1981-01-23',1300,NULL,10);
创建工资等级表:
CREATE TABLE SALGRADE(
GRADE INT,
LOSAL DOUBLE,
HISAL DOUBLE );
插入数据:
INSERT INTO SALGRADE VALUES (1,500,1000);
INSERT INTO SALGRADE VALUES (2,1001,1500);
INSERT INTO SALGRADE VALUES (3,1501,2000);
INSERT INTO SALGRADE VALUES (4,2001,3000);
INSERT INTO SALGRADE VALUES (5,3001,9999);
1.查找部门30中员工的详细信息。
SELECT * FROM EMP WHERE DEPTNO=30;
2.找出从事职员工作的员工的编号、姓名、部门号。
SELECT EMPNO,ENAME,DEPTNO FROM EMP WHERE JOB="职员";
3.检索出奖金多于基本工资的员工信息。
SELECT *FROM EMP WHERE COMM>SAL;
4.检索出奖金多于基本工资60%的员工信息。
SELECT * FROM EMP WHERE COMM>SAL*0.6;
5.找出姓名中包含A的员工信息。
SELECT *FROM EMP WHERE ENAME LIKE '%A%';
6.找出姓名以A、B、S开始的员工信息。
SELECT *FROM EMP WHERE ENAME LIKE 'A%' OR ENAME LIKE'B%' OR ENAME LIKE 'S%';
7.找到名字长度为7个字符的员工信息。
SELECT * FROM EMP WHERE LENGTH(ENAME)=7;
8.名字中不包含R字符的员工信息。
SELECT * FROM EMP WHERE ENAME NOT LIKE '%R%';
9.返回员工的详细信息并按姓名升序排序。
SELECT *FROM EMP ORDER BY ENAME ASC;
10.返回员工的信息并按姓名降序,工资升序排列。
SELECT * FROM EMP ORDER BY ENAME DESC,SAL ASC;
11.计算员工的日薪(按30天)。
select ename,sal/30 as '日薪' from emp;
12.找出获得奖金的员工的工作:
SELECT * FROM EMP WHERE COMM;
13.找出奖金少于100或者没有获得奖金的员工的信息。
SELECT * FROM EMP WHERE COMM<100 OR COMM IS NULL;
14.找出10部门的经理、20部门的职员 的员工信息。
SELECT *FROM EMP WHERE DEPTNO=10 OR DEPTNO=20;
15.找出10部门的经理、20部门的职员 或者既不是经理也不是职员但是工资高于2000元的员工信息。
SELECT * FROM EMP
WHERE (DEPTNO=10 AND JOB='经理')
OR (DEPTNO=20 AND JOB='职员')
OR (JOB<>'经理' AND JOB<>'职员' AND SAL>2000);