Вы находитесь на странице: 1из 4

I.

(The following query retrieves "2" highest paid employees FROM each Departmen
t :
SELECT deptno, empno, sal
FROM emp e
WHERE
2 > ( SELECT COUNT(e1.sal)
FROM emp e1
WHERE e.deptno = e1.deptno AND e.sal < e1.sal )
ORDER BY 1,3 DESC;
Index
II. Query that will display the total no. of employees, and of that total the nu
mber who were hired in 1980, 1981, 1982, and 1983. Give appropriate column headi
ngs.
I am looking at the following output. We need to stick to this format.
Total
----------14

1980
-----------1

1981
-----------10

1982
1983
----------------------2
1

SELECT COUNT (*), COUNT(DECODE(TO_CHAR (hiredate, 'YYYY'),'1980', empno)) "1980


",
COUNT (DECODE (TO_CHAR (hiredate, 'YYYY'), '1981', empn
o)) "1981",
COUNT (DECODE (TO_CHAR (hiredate, 'YYYY'), '1982', empno
)) "1982",
COUNT (DECODE (TO_CHAR (hiredate, 'YYYY'), '1983', empno
)) "1983"
FROM emp;
Index
III. Query for listing Deptno, ename, sal, SUM(sal in that dept) :
SELECT a.deptno, ename, sal, (SELECT SUM(sal) FROM emp b WHERE a.deptno = b.dept
no)
FROM emp a
ORDER BY a.deptno;
OUTPUT :
=======
DEPTNO
=========
10
30
10
10
30
30
30
30
30
20
20
20

ENAME
=======
KING
BLAKE
CLARK
JONES
MARTIN
ALLEN
TURNER
JAMES
WARD
SMITH
SCOTT
MILLER

SAL
====

SUM (SAL)
=========
5000
2850
2450
2975
1250
1600
1500
950
2750
8000
3000
20000

11725
10900
11725
11725
10900
10900
10900
10900
10900
33000
33000
33000

Index
IV. Create a matrix query to display the job, the salary for that job based on d

epartment number, and the total salary for that job for all departments, giving
each column an appropriate heading.
The output is as follows - we need to stick to this format :
Job
30
-------------------ANALYST
CLERK
950
MANAGER

Dept 10
Total
-----------------------

Dept 20

Dept

-------------

--

6000
6000
1300

1900
4150

2450

2975

2850

8275
PRESIDENT

5000
5000

SALESMAN
0

560
5600

SELECT job "Job", SUM (DECODE (deptno, 10, sal)) "Dept 10",
SUM (DECODE (deptno, 20, sal)) "Dept 20",
SUM (DECODE (deptno, 30, sal)) "Dept 30",
SUM (sal) "Total"
FROM emp
GROUP BY job ;
Index
V. 4th Top Salary of all the employees :
SELECT DEPTNO, ENAME, SAL
FROM EMP A
WHERE
3 = (SELECT COUNT(B.SAL) FROM EMP B
WHERE A.SAL < B.SAL) ORDER BY SAL DESC;
Index
VI. Retrieving the 5th row FROM a table :
SELECT DEPTNO, ENAME, SAL
FROM EMP
WHERE ROWID = (SELECT ROWID FROM EMP WHERE ROWNUM <= 5
MINUS
SELECT ROWID FROM EMP WHERE ROWNUM < 5)
Index
VII. Tree Query :
Name
Null?
Type
------------------------------------------------------------------SUB
NOT NULL
VARCHAR2(4)
SUPER
VARCHAR2(4)
PRICE
NUMBER(6,2)

SELECT sub, super


FROM parts
CONNECT BY PRIOR sub = super

START WITH sub = 'p1';


Index
VIII. Eliminate duplicates rows in a table :
DELETE FROM table_name A
WHERE ROWID > ( SELECT min(ROWID) FROM table_name B WHERE A.col = B.col);
Index
IX. Displaying EVERY 4th row in a table : (If a table has 14 rows, 4,8,12 rows
will be selected)
SELECT *
FROM emp
WHERE (ROWID,0) IN (SELECT ROWID, MOD(ROWNUM,4)
FROM emp);
Index
X. Top N rows FROM a table : (Displays top 9 salaried people)
SELECT ename, deptno, sal
FROM (SELECT * FROM emp ORDER BY sal DESC)
WHERE ROWNUM < 10;
Index
XI. How does one count/sum RANGES of data values in a column? A value x will be
between values y and z if GREATEST(x, y) = LEAST(x, z).
SELECT
f2,
COUNT(DECODE(greatest(f1,59), least(f1,100), 1, 0)) "Range 60-100
",
COUNT(DECODE(greatest(f1,30), least(f1, 59), 1, 0)) "Range 30-59"
,
COUNT(DECODE(greatest(f1,29), least(f1, 0), 1, 0)) "Range 00-29"
FROM my_table
GROUP BY f2;
Index
XII. For equal size ranges it migth be easier to calculate it with DECODE(TRUNC(
value/range), 0, rate_0, 1, rate_1, ...).
SELECT ename "Name", sal "Salary",
DECODE( TRUNC(sal/1000, 0), 0,
1,
2,
3,
FROM emp;

0.0,
0.1,
0.2,
0.3) "Tax rate"

XIII. How does one count different data values in a column?


COL NAME DATATYPE
---------------------------------------DNO
NUMBER
SEX
CHAR
SELECT dno, SUM(DECODE(sex,'M',1,0)) MALE,
SUM(DECODE(sex,'F',1,0)) FEMALE,
COUNT(DECODE(sex,'M',1,'F',1)) TOTAL
FROM t1

GROUP BY dno;
Index
XIV. Query to get the product of all the values of a column :
SELECT EXP(SUM(LN(col1))) FROM srinu;
Index
XV. Query to display only the duplicate records in a table:
SELECT num
FROM satyam
GROUP BY num
HAVING COUNT(*) > 1;
Index
XVI. Query for getting the following output as many number of rows in the table
:
*
**
***
****
*****
SELECT RPAD(DECODE(temp,temp,'*'),ROWNUM,'*')
FROM srinu1;