阅读更多
实例表结构如下
CREATE TABLE `emp` (
`EMPNO` int(11) NOT NULL,
`ENAME` varchar(45) DEFAULT NULL,
`JOB` varchar(45) DEFAULT NULL,
`MGR` varchar(45) DEFAULT NULL,
`HIREDATE` date DEFAULT NULL,
`SAL` float DEFAULT NULL,
`COMM` float DEFAULT NULL,
`DEPTNO` varchar(45) DEFAULT NULL,
PRIMARY KEY (`EMPNO`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
CREATE TABLE `dept` (
`DEPTNO` int(11) NOT NULL,
`DNAME` varchar(45) DEFAULT NULL,
`LOC` varchar(45) DEFAULT NULL,
PRIMARY KEY (`DEPTNO`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
表数据如下:
INSERT INTO `emp` VALUES (102,'EricHu','Developer','1455','2011-05-26',5500,14,'10'),(104,'huyong','PM','1455','2011-05-26',5500,14,'10'),(105,'WANGJING','Developer','1455','2011-05-26',5500,14,'10'),(7369,'SMITH','CLERK','7902','1980-12-17',800,NULL,'20'),(7499,'ALLEN','SALESMAN','7698','1981-02-20',1600,300,'30'),(7521,'WARD','SALESMAN','7698','1981-02-22',1250,500,'30'),(7566,'JONES','MANAGER','7839','1981-04-02',2975,NULL,'20'),(7654,'MARTIN','SALESMAN','7698','1981-09-28',1250,1400,'30'),(7698,'BLAKE','MANAGER','7839','1981-05-01',2850,NULL,'30'),(7782,'CLARK','MANAGER','7839','1981-06-09',2450,NULL,'10'),(7788,'SCOTT','ANALYST','7566','1987-04-19',4000,NULL,'20'),(7839,'KING','PRESIDENT',NULL,'1981-11-17',5000,NULL,'10'),(7844,'TURNER','SALESMAN','7698','1981-09-08',1500,0,'30'),(7876,'ADAMS','CLERK','7788','1987-05-23',1100,NULL,'20'),(7900,'JAMES','CLERK','7698','1981-12-03',950,NULL,'30'),(7902,'FORD','ANALYST','7566','1981-12-03',3000,NULL,'20'),(7934,'MILLER','CLERK','7782','1982-01-23',1300,NULL,'10');
INSERT INTO `dept` VALUES (10,'ACCOUNTING','NEW YORK'),(20,'RESEARCH','DALLAS'),(30,'SALES','CHICAGO'),(40,'OPERATIONS','BOSTON'),(50,'abc','def'),(60,'Developer','HaiKou');
1.列出至少有一个员工的所有部门
select distinct dname from dept left join emp on dept.DEPTNO = emp.DEPTNO where emp.DEPTNO is not null;
select dname from dept where deptno in(select deptno from emp group by deptno having count(*)>1)
2.列出薪金比“SMITH”多的所有员工
select emp1.ENAME from emp as emp1 , emp as emp2 where emp2.ENAME = 'SMITH' and emp1.SAL > emp2.SAL;
select ename from emp where sal>(select emp1.SAL from emp as emp1 where emp1.ENAME='SMITH')
3.列出所有员工的姓名及其直接上级的姓名
select e1.ENAME,e2.ENAME from emp as e1 left join emp as e2 on e1.MGR = e2.EMPNO
select ename,(select ename from emp where empno in(a.mgr)) from emp a ;
4.列出受雇日期早于其直接上级的所有员工。
select e1.ENAME from emp as e1 left join emp as e2 on e1.MGR = e2.EMPNO WHERE e1.HIREDATE < e2.HIREDATE
select ename from emp a where HIREDATE<(select HIREDATE from emp where empno in(a.mgr));
5.列出部门名称和这些部门的员工信息,同时列出那些没有员工的部门(以emp表为主,左连接查询)
select dname,emp.* from dept left join emp on dept.deptno=emp.deptno;
6.列出所有“CLERK”(办事员)的姓名及其部门名称。
select e.ENAME,d.DNAME from emp as e left join dept d on e.DEPTNO = d.DEPTNO where e.JOB = 'CLERK'
7.列出最低薪金大于1500的各种工作
select DISTINCT JOB from emp where SAL >1500
8.列出在部门“SALES”(销售部)工作的员工的姓名,假定不知道销售部的部门编号。
select e.ENAME from emp as e left join dept as d on e.DEPTNO= d.DEPTNO where d.DNAME ='SALES'
9.列出薪金高于公司平均薪金的所有员工
select ename from emp where sal>( select avg( sal) from emp);
10.列出与“SCOTT”从事相同工作的所有员工
select ename from emp where job in(select job from emp where ENAME='SCOTT') and ename !='SCOTT' ;
11.列出薪金等于部门30中员工的薪金的所有员工的姓名和薪金
select e1.ename,e1.sal,e1.DEPTNO from emp e1,emp e2 where e1.sal = e2.sal and e2.deptno = '30' and e1.deptno !='30'
select ename,sal from emp where sal=any(select sal from emp where deptno=30) and deptno!=30;
12.列出薪金高于在部门30工作的所有员工的薪金的员工姓名和薪金。(max的用法)
select sal,ename from emp where sal>(select max(sal) from emp where deptno=30);
13.列出在每个(每个是关键字,对此group by)部门工作的员工数量、平均工资和平均服务期限。(经典的group by用法)select deptno,count(*),avg(a.sal),avg(sysdate-HIREDATE) from emp a group by deptno;