第一次使用Oracle,想做一些练习，熟悉一些oracle。

表：使用的是scott用户，默认的表

具体表讲解，可以参考该文档：https://www.cnblogs.com/xjcheng1/p/7220159.html

```SELECT
SUM(CASE WHEN sal>0 AND sal<1000 THEN 1 ELSE 0 END) AS "0<sal<1000",
SUM(CASE WHEN sal>1000 AND sal<2000 THEN 1 ELSE 0 END) AS "1000<sal<2000",
SUM(CASE WHEN sal>2000 AND sal<3000 THEN 1 ELSE 0 END) AS "2000<sal<3000",
SUM(CASE WHEN sal>3000 THEN 1 ELSE 0 END) AS "sal>3000"
FROM
emp```

```SELECT
d.dname as "部门名称",
s.count as "部门的员工数",
s.avgsal as "部门的平均工资",
e.ename as "最低收入雇员的姓名"
FROM
emp e ,
dept d,
(
SELECT
deptno,COUNT(1) count,AVG(sal) avgsal,MIN(sal) minsal
FROM
emp
GROUP BY
deptno
) s
WHERE
e.deptno = d.deptno
AND
e.deptno = s.deptno
AND
s.minsal = e.sal```

```SELECT
COUNT(1),
job
FROM
emp
WHERE
sal > (SELECT AVG(SAL) FROM emp)
GROUP BY
job```

```SELECT
e1.ename,
e2.ename AS manage
FROM
emp e1 LEFT JOIN emp e2
ON
e1.mgr = e2.empno```

```SELECT
t1.empno,
t1.ename,
t1.deptno,
t1.sal,
t2.avgsal
FROM
emp t1 , (SELECT deptno,AVG(sal) avgsal FROM emp t2  GROUP BY t2.deptno) t2
WHERE
t1.deptno = t2.deptno
AND
t1.sal > t2.avgsal```

```SELECT
empno,
ename,
job,
deptno
FROM
emp
WHERE
(job,deptno)
IN
(
SELECT
job,
deptno
FROM
emp
WHERE
empno
IN
(7369,7499)
) ```

```SELECT
*
FROM
(
SELECT
rownum r,empno,ename,sal
FROM
(
SELECT
empno,ename,sal
FROM
emp
ORDER BY
sal
DESC
)
) t
WHERE
t.r>=2 and t.r<=5 ```

```SELECT
*
FROM
emp
WHERE
sal >
(
SELECT
sal
FROM
emp
WHERE
ename = 'SMITH'
)```

```SELECT
*
FROM
emp
WHERE
EXTRACT(MONTH FROM hiredate)
=
EXTRACT(MONTH FROM sysdate);```

```SELECT
COUNT(*),
to_char(hiredate,'yyyy-MM')
FROM
emp
WHERE
to_char(hiredate,'yyyy') = '1981'
GROUP BY
to_char(hiredate,'yyyy-MM')
ORDER BY
to_char(hiredate,'yyyy-MM')```

```SELECT
ename,
hiredate
FROM
emp
WHERE
deptno
=
(
SELECT
deptno
FROM
emp
WHERE
ename = 'SCOTT'
)```

```SELECT
e.ename,
d.dname,
e.job
From
emp e , dept d
WHERE
e.deptno = d.deptno
AND
dname =
(
SELECT
dname
FROM
dept
WHERE
loc = 'NEW YORK'
)```

```SELECT
ename,
sal
FROM
emp
WHERE
mgr =
(
SELECT
empno
FROM
emp
WHERE
ename = 'KING'
)```

```SELECT
*
FROM
emp
WHERE
deptno
IN
(
SELECT
deptno
FROM
emp
WHERE
ename
LIKE
'%U%'
)```

```SELECT
e.ename,
d.dname
FROM
emp e
INNER JOIN
dept d
ON
e.deptno = d.deptno```

```SELECT
e.ename,
d.dname,
e.job,
e.sal,
FROM
emp e
INNER JOIN
dept d
ON
e.deptno = d.deptno
INNER JOIN
ON
e.sal
BETWEEN
s.losal
AND
s.hisal```

```SELECT
e.ename,
r.maxsal,
r.deptno
FROM
emp e ,
(
SELECT
MAX(sal) maxsal,
deptno
FROM
emp
GROUP BY
deptno
) r
WHERE
e.deptno = r.deptno
AND
e.sal = r.maxsal
ORDER BY
deptno```

```SELECT
*
FROM
(
SELECT
e1.empno,
e1.deptno,
e1.ename,
e2.ename AS manage,
e2.hiredate managedate,
e1.hiredate staffdate
FROM
emp e1 LEFT JOIN emp e2
ON
e1.mgr = e2.empno
) z
WHERE
z.staffdate < z.managedate
ORDER BY
z.empno```

```SELECT
s.ename,
s.dname,
k.deptno,
k.count
FROM
(
SELECT
e.ename,
d.dname,
e.deptno deptno
FROM
emp e , dept d
WHERE
e.deptno = d.deptno
AND
job = 'CLERK'
) s,
(
SELECT
COUNT(*) count,
deptno
FROM
emp
GROUP BY
deptno
) k
WHERE
s.deptno = k.deptno```

```SELECT
e.*,
TO_CHAR(hiredate,'yyyy') 受雇年份,
ROUND(MONTHS_BETWEEN(SYSDATE,hiredate)/12,2) 受雇年限
FROM
emp e
WHERE
MONTHS_BETWEEN(SYSDATE,hiredate)/12>12
ORDER BY

DESC```

```SELECT
DISTINCT
e.empno,
e.ename,
e.job,
e.deptno
FROM
emp e,emp k
WHERE
e.deptno != k.deptno
AND
e.job = k.job
ORDER BY
job,deptno```

三、重点说明

1.oracle数据库，sql语句不能使用limit关键字。

2.rownum非常的特殊，使用的时候需要特别注意。详细可参考文档：https://www.cnblogs.com/szlbm/p/5806070.html

3.所有包含中文的字体，需要加单引号。比如：job = 'CLERK'   ，ename = 'KING'

4.函数的使用。

to_char()  函数功能，就是将数值型或者日期型转化为字符型。具体参考文档：https://www.cnblogs.com/aipan/p/7941917.html

extract()函数----用于截取年、月、日、时、分、秒。具体参考文档：https://www.cnblogs.com/xqzt/p/4477239.html