- 論壇徽章:
- 7
|
作為一名dba,會寫sql是一項基本功。
如果在做dba之前,從事過開發(fā),可能寫sql就相對容易些,如果直接就上手dba,可能寫sql的能力會差一些。
本人也沒有從事過開發(fā),寫sql也是在工作中慢慢的學會的。
不過,這也沒有關系,多練習練習,就會了。
下面總共有sql練習55道題,這55道題目是itpub網(wǎng)友提供的,但沒有提供作者的名字,這里先行謝謝了。
原版的55道題目,既有題也有答案,因為是學習寫sql,不是去看人家怎么寫的,我這里的練習的是自己這幾天寫出來的,
當然也參考了原作者的答案。原作者的文檔命名為《Oracle_SQL:經(jīng)典查詢練手四篇(全)》可以到itpub上下載。
下面的sql基本上是我自己寫出來的,因為我也是在不斷的學習寫sql中,有寫的不妥的地方,請網(wǎng)友們指出,謝謝。
好了,轉入正題,下面我們來一起寫sql:
SQL學習1-18:
1.列出至少有一個員工的所有部門。
2.列出薪金比“MARTIN”多的所有員工。
3.列出所有員工的姓名及其直接上級的姓名。
4.列出受雇日期早于其直接上級的所有員工。
5.列出部門名稱和這些部門的員工信息,同時列出那些沒有員工的部門
6.列出所有“CLERK”(辦事員)的姓名及其部門名稱。
7.列出最低薪金大于1500的各種工作。
8.列出在部門“SALES”(銷售部)工作的員工的姓名,假定不知道銷售部的部門編號。
9.列出薪金高于公司平均薪金的所有員工。
10.列出與“BLAKE”從事相同工作的所有員工。
11.列出薪金等于部門30中員工的薪金的所有員工的姓名和薪金。
12.列出薪金高于在部門30工作的所有員工的薪金的員工姓名和薪金。
13.列出在每個部門工作的員工數(shù)量、平均工資和平均服務期限。
14.列出所有員工的姓名、部門名稱和工資。
15.列出所有部門的詳細信息和部門人數(shù)。
16.列出各種工作的最低工資。
17.列出各個部門的MANAGER(經(jīng)理)的最低薪金。
18.列出所有員工的年工資,按年薪從低到高排序。
1.列出至少有一個員工的所有部門。
select dname from scott.dept a,(
select deptno,count(*) from scott.emp where deptno is not null group by deptno order by count(*) desc
) b
where a.deptno = b.deptno
DNAME
ACCOUNTING
RESEARCH
SALES
2. 列出薪金比“MARTIN”多的所有員工。
select * from scott.emp b where sal>
(select sal from scott.emp where ename='MARTIN')
3. 列出所有員工的姓名及其直接上級的姓名。
select a.empno as "員工編號",
a.ename as "員工姓名",
b.ename as "員工的直接領導"
from scott.emp a, scott.emp b
where a.mgr = b.empno(+)
員工編號 員工姓名 員工的直接領導
7902 FORD JONES
7900 JAMES BLAKE
7844 TURNER BLAKE
7654 MARTIN BLAKE
7521 WARD BLAKE
7499 ALLEN BLAKE
7934 MILLER CLARK
7782 CLARK KING
7698 BLAKE KING
7566 JONES KING
7369 SMITH FORD
7839 KING
4.列出受雇日期早于其直接上級的所有員工。
select a.empno as "員工編號",
a.ename as "員工姓名",
a.hiredate as "員工受雇日期",
b.ename as "員工的直接領導",
b.hiredate "員工的直接領導受雇日期"
from scott.emp a, scott.emp b
where a.mgr = b.empno(+)
and a.hiredate<b.hiredate
員工編號 員工姓名 員工受雇日期 員工的直接領導 員工的直接領導受雇日期
7521 WARD 1981/2/22 BLAKE 1981/5/1
7499 ALLEN 1981/2/20 BLAKE 1981/5/1
7782 CLARK 1981/6/9 KING 1981/11/17
7566 JONES 1981/4/2 KING 1981/11/17
7698 BLAKE 1981/5/1 KING 1981/11/17
7369 SMITH 1980/12/17 FORD 1981/12/3
紅色標示的這些員工就是比直接領導受雇早的員工。
5.列出部門名稱和這些部門的員工信息,同時列出那些沒有員工的部門
select a.empno, a.ename, b.dname, b.deptno
from scott.emp a, scott.dept b
where a.deptno(+) = b.deptno
EMPNO ENAME DNAME DEPTNO
7782 CLARK ACCOUNTING 10
7934 MILLER ACCOUNTING 10
7902 FORD RESEARCH 20
7369 SMITH RESEARCH 20
7566 JONES RESEARCH 20
7900 JAMES SALES 30
7844 TURNER SALES 30
7654 MARTIN SALES 30
7521 WARD SALES 30
7499 ALLEN SALES 30
7698 BLAKE SALES 30
OPERATIONS 40
等同于:
select a.empno, a.ename, b.dname, b.deptno
from scott.emp a
right join scott.dept b
on a.deptno = b.deptno
6.列出所有“CLERK”(辦事員)的姓名及其部門名稱。
select c.job,c.ename,b.dname from scott.dept b,
(select a.ename,a.deptno,a.job from scott.emp a where job ='CLERK') c
where c.deptno = b.deptno
JOB ENAME DNAME
CLERK SMITH RESEARCH
CLERK JAMES SALES
CLERK MILLER ACCOUNTING
7. 列出最低薪金大于1500的各種工作。
select a.job,min(a.sal) from scott.emp a group by a.job having min(sal) >1500;
JOB MIN(A.SAL)
PRESIDENT 5000
MANAGER 2450
ANALYST 3000
8.列出在部門“SALES”(銷售部)工作的員工的姓名,假定不知道銷售部的部門編號。
select a.ename from scott.emp a ,(
select b.deptno from scott.dept b where dname ='SALES') c where
c.deptno = a.deptno
或
select ename from scott.emp where deptno = (select deptno from scott.dept where dname ='SALES');
ENAME
ALLEN
WARD
MARTIN
BLAKE
TURNER
JAMES
9.列出薪金高于公司平均薪金的所有員工。
select ename from scott.emp where sal > (
select round(avg(sal)) from scott.emp )
ENAME
JONES
BLAKE
CLARK
KING
FORD
10.列出與“BLAKE”從事相同工作的所有員工。
select b.ename,b.job from scott.emp b,(
select a.job from scott.emp a where ename='BLAKE') c
where b.job = c.job
或
select ename,job from scott.emp b where job = (select a.job from scott.emp a where ename='BLAKE')
ENAME JOB
JONES MANAGER
BLAKE MANAGER
CLARK MANAGER
11.列出薪金等于部門30中員工的薪金的所有員工的姓名和薪金。
select a.ename, a.sal
from scott.emp a
where a.sal in (select b.sal from scott.emp b where b.deptno = 30)
and a.deptno <> 30;
12.列出薪金高于在部門30工作的所有員工的薪金的員工姓名和薪金。
select ename,sal from scott.emp where sal> all (
select sal from scott.emp where deptno=30)
或
select ename,sal from scott.emp where sal>(select max(sal) from scott.emp where deptno=30)
ENAME SAL
JONES 2975.00
KING 5000.00
FORD 3000.00
13.列出在每個部門工作的員工數(shù)量、平均工資和平均服務期限。
select b."部門平均工資","部門員工數(shù)量","平均服務年限" from scott.dept a,
(select deptno,
round(avg(sal)) as "部門平均工資",
count(*) as "部門員工數(shù)量",
round(avg(to_number(to_char(hiredate, 'yyyymmdd')))) as "平均服務年限"
from scott.emp
where deptno is not null
group by deptno) b
where a.deptno = b.deptno
部門平均工資 部門員工數(shù)量 平均服務年限
1567 6 19810664
2258 3 19807607
1875 2 19815366
14.列出所有員工的姓名、部門名稱和工資。
select ename, dname, sal
from scott.dept a, (select * from scott.emp b) b
where a.deptno = b.deptno
ENAME DNAME SAL
CLARK ACCOUNTING 2450.00
MILLER ACCOUNTING 1300.00
FORD RESEARCH 3000.00
SMITH RESEARCH 800.00
JONES RESEARCH 2975.00
JAMES SALES 950.00
TURNER SALES 1500.00
MARTIN SALES 1250.00
WARD SALES 1250.00
ALLEN SALES 1600.00
BLAKE SALES 2850.00
15.列出所有部門的詳細信息和部門人數(shù)。
select b.deptno, b.dname, b.loc, count(ename)
from scott.emp a, (select * from scott.dept) b
where a.deptno(+) = b.deptno
group by b.deptno, b.dname, b.loc;
或
select a.deptno,
a.dname,
a.loc,
(select count(*)
from scott.emp b
where b.deptno = a.deptno
group by b.deptno) as deptcount
from scott.dept a;
20 RESEARCH DALLAS 3
40 OPERATIONS BOSTON 0
10 ACCOUNTING NEW YORK 2
30 SALES CHICAGO 6
16.列出各種工作的最低工資。
select job,min(sal) from scott.emp group by job
JOB MIN(SAL)
CLERK 800
SALESMAN 1250
PRESIDENT 5000
MANAGER 2450
ANALYST 3000
17.列出各個部門的MANAGER(經(jīng)理)的最低薪金。
select b.ename as "部門經(jīng)理",
(select c.dname as "部門"
from scott.dept c
where c.deptno = a.deptno) as "部門",
min(a.sal) as "部門經(jīng)理最低工資"
from scott.emp a, scott.emp b
where a.mgr = b.empno(+)
and b.ename <> 'KING'
group by b.ename, a.deptno
order by a.deptno desc
部門經(jīng)理 部門 部門經(jīng)理最低工資
BLAKE SALES 950
FORD RESEARCH 800
JONES RESEARCH 3000
CLARK ACCOUNTING 1300
18.列出所有員工的年工資,按年薪從低到高排序。
select ename, (sal + nvl(comm, 0)) * 12 as "年薪"
from scott.emp
order by "年薪";
|
|