-- 本文中大部分select语句都是针对 emp员工表和 dept部门表 的查询,
-- emp表(编号empno,姓名ename,job,工资sal,佣金comm,上级mgr,入职日期hiredate,部门号deptno)
-- dept表(编号deptno,名字 dname ,所在地loc)
e.g.1.查询人数大于4个的部门的全部信息(知识点:count() group by having 子查询)
SELECT * FROM DEPT WHERE DEPTNO IN(
SELECT DEPTNO FROM (
SELECT DEPTNO,COUNT(1) FROM EMP GROUP BY DEPTNO
HAVING COUNT(1)>4
));
2.查询员工及他的领导姓名(表连接)
select a.ename,b.ename mgr from emp a,emp b where a.mgr=b.empno
方法2:select concat(a.ename,'的领导是:',b.ename)from emp a,emp b where a.mgr=b.empno
3.查询员工姓名,要求为首字母大写,其他小写形式(concat upper lower)
select concat(upper(substr(ename,1,1)),lower(substr(ename,2))) from emp;
4.-- 看哪个月入职的人数最多
select *from(
select count(1),month(hiredate) a from emp group by month(hiredate)
)c order by a desc limit 0,1
SELECT * FROM(
SELECT COUNT(1),TO_CHAR(HIREDATE,'MM') FROM EMP
GROUP BY TO_CHAR(HIREDATE,'MM')
ORDER BY COUNT(1) DESC
)WHERE ROWNUM<2; (ORACLE)
5.将emp表备份(表的备份可防止直接对原表进行修改时出错而无法撤回的情况)
create table empc select * from emp;
6. -- 修改EMPNO为57的记录,将薪金涨20%,DEPTNO改成30;
update emp set sal=sal*1.2 ,deptno=30 where empno=57;
9.查询平均工资高于1500的所有职务的名称和平均工资金额.
SELECT JOB,ROUND(AVG(SAL),2) FROM EMP GROUP BY JOB HAVING AVG(SAL)>1500;
11.-- 查询所有雇员的实发工资(薪金+佣金) ( ifnull函数,comm为空时,实发工资为sal )
select(ifnull(comm+sal,sal))'实发工资',ename from emp ; NVL(NULL,0)+SAL
12.-- 查询所有经理中工资第三高的员工的详细信息(limit m,n m=(页数-1)*n )
select * from emp where job ='manager' order by sal desc limit 2,1
13.-- 找出所有员工中薪金第五高到第八高的员工信息。
select * from emp order by sal desc limit 4,4
14.-- 查询出SCOTT的上级领导的详细信息(子查询 )
select * from emp where empno=(
select mgr from emp where ename='scott')
15.-- 查询出没有员工的部门。
select * from dept where deptno not in(select distinct deptno from emp)
16.-- 找到同名的所有员工
方法1
select * from emp a,emp b where a.ename=b.ename and a.empno!= b.empno;
方法2
select ename from emp group by ename having count(1)>1;
17.-- 查询出king所在部门的部门号\部门名称\部门人数
select a.deptno,dname,count(a.ename) from emp a,dept b
where a.deptno=b.deptno and a.deptno=(select deptno from emp where ename='king')
18.查询出king所在部门的工作年限最大的员工名字:
select ename,hiredate from emp where deptno=(select deptno from emp where ename='king')
order by hiredate limit 0,1
19.查询出没有下属员工的人的名字和他的职位
select ename from emp a where ename not in(
select b.ename from emp b,emp a where a.mgr=b.empno)
20.-- 查询每个部门,每种职位的最高工资(group by后可加多个分组依据)
select max(sal),deptno,job from emp group by deptno,job;
21.查询出员工名字以A打头的人数最多的部门名称和员工名字:
select dname,group_concat(ename) from emp a,dept b where a.deptno=b.deptno
and a.ename like('A%')group by a.deptno order by count(1)desc limit 0,1
22.查询出管理员工人数最多的人的名字和他管理的人的名字
select b.ename manager,group_concat(a.ename) from emp a,emp b where a.mgr=b.empno
group by manager order by count(1) desc limit 0,1
23.按工资进行排名,排名从1开始,工资相同排名相同(如果两人并列第1则没有第2名,从第3名继续排)
select a.*, (select count(1)+1 from emp where sal>a.sal) rank from emp a order by sal desc;
-----------------------------------------------------
ORACLE:
LIKE ’M%’ OR LIKE ’%N’ 不等于 LIKE ’M%N’ (无括号,加引号)
DISTINCT X;
TRUNCATE/ALTER(DDL)
TRUNCATE不可ROLLBACK
DESC=DESCRIBE
SELECT DNAME FROM DEPT GROUP BY DEPTNO;(错误,DEPTNO->DNAME)
SELECT COMM+SAL SUM FROM EMP WHERE SUM >1000;(错误,从WHERE执行)
SELECT ENAME (AS)EMPLOYEE FROM EMP;(✔别名无需加引号)
不能在where子句中使用聚合函数,为什么呢?
聚集函数也叫列函数,它们都是基于整列数据进行计算的,而where子句则是对数据行进行过滤的(这里过滤是在一个记录里边过滤的,基于"行"),在筛选过程中依赖“基于已经筛选完毕的数据得出的计算结果”是一种悖论
子串的位置: INSTR('going swiming', 'ing’, 4)
截取SUBSTR('Accelerate', 4, 2)
字符替换TRANSLTE('I WORK FOR ACCENTURE', 'AEIOU', '12345')
数字截取TRUNC(10.6789,2):10.67
显示部门表中部门和所在城市列表,中间以下划线“”连接,城市名转换成以大写字母开头:
SELECT DNAME ||'_'||(INITCAP(LOC)) FROM DEPT;
(1)求各部门各职位的最大工资情况?
SELECT DEPTNO,JOB,MAX(SAL)
FROM EMP E
GROUP BY DEPTNO,JOB;
(2)及该员工姓名?
SELECT ENAME,DEPTNO,JOB,SAL FROM EMP
WHERE (DEPTNO,JOB,SAL)IN(
SELECT DEPTNO,JOB,MAX(SAL)
FROM EMP E
GROUP BY DEPTNO,JOB);
COUNT(1)速度 > COUNT(*):
SELECT 1,2,3 FROM EMP;
SELECT EMPNO FROM EMP;
--分别使用exists和in实现查询纽约的员工名
SELECT ENAME FROM EMP E
WHERE DEPTNO IN(
SELECT DEPTNO FROM DEPT D WHERE LOC='NEW YORK'); √
SELECT deptno FROM EMP E
WHERE EXISTS (
SELECT * FROM DEPT,EMP WHERE DEPT.DEPTNO=EMP.DEPTNO
AND LOC='NEW YORK'); × 有记录即返回true,SO RETURN ALL
SELECT * FROM EMP e1
WHERE EXISTS (SELECT *
FROM EMP e, DEPT d
WHERE e.DEPTNO = d.DEPTNO
AND e1.EMPNO = e.EMPNO
AND d.LOC = 'NEW YORK'); ✔
插入NULL:
insert into dept values(40,'accounting');❌
insert into dept values(40,'accounting','NULL'); ❌
insert into dept values(40,'accounting',NULL); ✔
SELECT ENAME FROM EMP ORDER BY DEPTNO; ❌ 原理emmmnn……
SELECT ENAME,DEPTNO FROM EMP ORDER BY DEPTNO; ✔