1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
| SQL> select dname from dept where deptno in(select deptno from emp);
DNAME
ACCOUNTING RESEARCH SALES
SQL> select * from emp where sal > (select sal from emp where ename = 'SMITH');
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 7566 JONES MANAGER 7839 02-APR-81 2975 20 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 7839 KING PRESIDENT 17-NOV-81 5000 10 7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 7900 JAMES CLERK 7698 03-DEC-81 950 30 7902 FORD ANALYST 7566 03-DEC-81 3000 20 7934 MILLER CLERK 7782 23-JAN-82 1300 10
13 rows selected.
SQL> select a.ename,(select ename from emp b where b.empno=a.mgr) as boss_name from emp a;
ENAME BOSS_NAME
SMITH FORD ALLEN BLAKE WARD BLAKE JONES KING MARTIN BLAKE BLAKE KING CLARK KING SCOTT JONES KING TURNER BLAKE ADAMS SCOTT JAMES BLAKE FORD JONES MILLER CLARK
14 rows selected.
SQL> select a.ename from emp a where a.hiredate<(select hiredate from emp b where b.empno=a.mgr);
ENAME
SMITH ALLEN WARD JONES BLAKE CLARK
6 rows selected.
SQL> select a.dname,b.empno,b.ename,b.job,b.mgr,b.hiredate,b.sal,b.deptno from dept a left join emp b on a.deptno=b.deptno;
DNAME EMPNO ENAME JOB MGR HIREDATE SAL DEPTNO
ACCOUNTING 7782 CLARK MANAGER 7839 09-JUN-81 2450 10 ACCOUNTING 7839 KING PRESIDENT 17-NOV-81 5000 10 ACCOUNTING 7934 MILLER CLERK 7782 23-JAN-82 1300 10 RESEARCH 7566 JONES MANAGER 7839 02-APR-81 2975 20 RESEARCH 7902 FORD ANALYST 7566 03-DEC-81 3000 20 RESEARCH 7876 ADAMS CLERK 7788 23-MAY-87 1100 20 RESEARCH 7369 SMITH CLERK 7902 17-DEC-80 800 20 RESEARCH 7788 SCOTT ANALYST 7566 19-APR-87 3000 20 SALES 7521 WARD SALESMAN 7698 22-FEB-81 1250 30 SALES 7844 TURNER SALESMAN 7698 08-SEP-81 1500 30 SALES 7499 ALLEN SALESMAN 7698 20-FEB-81 1600 30 SALES 7900 JAMES CLERK 7698 03-DEC-81 950 30 SALES 7698 BLAKE MANAGER 7839 01-MAY-81 2850 30 SALES 7654 MARTIN SALESMAN 7698 28-SEP-81 1250 30 OPERATIONS
15 rows selected.
SQL> select a.ename,b.dname from emp a join dept b on a.deptno=b.deptno and a.job='CLERK';
ENAME DNAME
MILLER ACCOUNTING SMITH RESEARCH ADAMS RESEARCH JAMES SALES
SQL> select distinct job as HighSalJob from emp group by job having min(sal)>1500;
HIGHSALJOB
PRESIDENT MANAGER ANALYST
SQL> select ename from emp where deptno=(select deptno from dept where dname='SALES');
ENAME
ALLEN WARD MARTIN BLAKE TURNER JAMES
6 rows selected.
SQL> select ename from emp where sal>(select avg(sal) from emp);
ENAME
JONES BLAKE CLARK SCOTT KING FORD
6 rows selected.
SQL> select ename from emp where job=(select job from emp where ename='SCOTT');
ENAME
SCOTT FORD
SQL> select a.ename,a.sal from emp a where a.sal in (select b.sal from emp b where b.deptno=30) and a.deptno<>30;
no rows selected
SQL> select ename,sal from emp where sal>(select max(sal) from emp where deptno=30);
ENAME SAL
JONES 2975 SCOTT 3000 KING 5000 FORD 3000
SQL> select (select b.dname from dept b where a.deptno=b.deptno) as deptname ,count(deptno) as deptcount,avg(sal) as deptavgsal from emp a group by deptno;
DEPTNAME DEPTCOUNT DEPTAVGSAL
SALES 6 1566.66667 RESEARCH 5 2175 ACCOUNTING 3 2916.66667
SQL> select a.ename,(select b.dname from dept b where b.deptno=a.deptno) as deptname,sal from emp a;
ENAME DEPTNAME SAL
SMITH RESEARCH 800 ALLEN SALES 1600 WARD SALES 1250 JONES RESEARCH 2975 MARTIN SALES 1250 BLAKE SALES 2850 CLARK ACCOUNTING 2450 SCOTT RESEARCH 3000 KING ACCOUNTING 5000 TURNER SALES 1500 ADAMS RESEARCH 1100 JAMES SALES 950 FORD RESEARCH 3000 MILLER ACCOUNTING 1300
14 rows selected.
SQL> select a.deptno,a.dname,a.loc,(select count(deptno) from emp b where b.deptno=a.deptno group by b.deptno) as deptcount from dept a;
DEPTNO DNAME LOC DEPTCOUNT
10 ACCOUNTING NEW YORK 3 20 RESEARCH DALLAS 5 30 SALES CHICAGO 6 40 OPERATIONS BOSTON
SQL> select job,avg(sal) from emp group by job;
JOB AVG(SAL)
CLERK 1037.5 SALESMAN 1400 PRESIDENT 5000 MANAGER 2758.33333 ANALYST 3000
SQL> select deptno,min(sal) from emp where job='MANAGER' group by deptno;
DEPTNO MIN(SAL)
30 2850 20 2975 10 2450
SQL> select ename,(sal+nvl(comm,0))*12 as salpersal from emp order by salpersal;
ENAME SALPERSAL
SMITH 9600 JAMES 11400 ADAMS 13200 MILLER 15600 TURNER 18000 WARD 21000 ALLEN 22800 CLARK 29400 MARTIN 31800 BLAKE 34200 JONES 35700 FORD 36000 SCOTT 36000 KING 60000
14 rows selected.
|