• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
Download
INDEX
1.Query for retrieving N highest paid employees FROM each Department.
2.Query that will display the total no. of employees, and of that total the number who
were hired in 1980, 1981, 1982, and 1983.
3.Query for listing Deptno, ename, sal, SUM(sal in that dept).
4.Matrix query to display the job, the salary for that job based on department number,
and the total salary for that job for all departments.

5.Nth Top Salary of all the employees.
6.Retrieving the Nth row FROM a table.
7.Tree Query.
8.Eliminate duplicates rows in a table.
9.Displaying EVERY Nth row in a table.
10.Top N rows FROM a table.
11.COUNT/SUM RANGES of data values in a column.
12.For equal size ranges it might be easier to calculate it with

DECODE(TRUNC(value/range), 0, rate_0, 1, rate_1, ...).

13.Count different data values in a column.
14.Query to get the product of all the values of a column.
15.Query to display only the duplicate records in a table.
16.Query for getting the following output as many number of rows in the table.
17.Function for getting the Balance Value.
18.Function for getting the Element Value.
19.SELECT Query for counting No of words.
20.Function to check for a leap year.
21.Query for removing all non-numeric.
22.Query for translating a column values to INITCAP.
23.Function for displaying Rupees in Words.

24. Function for displaying Numbers in Words

25.Query for deleting alternate even rows FROM a table.
26.Query for deleting alternate odd rows FROM a table.
27.Procedure for sending Email.
28.Alternate Query for DECODE function.
29.Create table adding Constraint to a date field to SYSDATE or 3 months later.
30.Query to list all the suppliers who r supplying all the parts supplied by supplier 'S2'.
31.Query to get the last Sunday of any month.
32.Query to get all those who have no children themselves.
33.Query to SELECT last N rows FROM a table.
34.SELECT with variables.
35.Query to get the DB Name.
36.Getting the current default schema.
37.Query to get all the column names of a particular table.
38.Spool only the query result to a file in SQLPLUS.
39.Query for getting the current SessionID.
40.Query to display rows FROM m to n.
41.Query to count no. Of columns in a table.
42.Procedure to increase the buffer length.
43.Inserting an & symbol in a Varchar2 column.
44.Removing Trailing blanks in a spooled file.
45.Samples for executing Dynamic SQL Statements.
46.Differences between SQL and MS-Access.
47.Query to display all the children, sub children of a parent.
48.Procedure to read/write data from/to a text file.
49.Query to display random number between any two given numbers.
50.Time difference between two date columns.

51.Using INSTR and SUBSTR
52.View procedure code
53.To convert signed number to number in oracle
54.Columns of a table
55.Delete rows conditionally
56.CLOB to Char
57.Change Settings
58.Double quoting a Single quoted String
59.Time Conversion
60.Table comparison
61.Running Jobs
62.Switching Columns
63.Replace and Round
64.First date of the year
65.Create Sequence
66.Cursors
67.Current Week
68.Create Query to restrict the user to a single row.
69.Query to get the first inserted record FROM a table.
70.Concatenate a column value with multiple rows.
71.Query to delete all the tables at once.
72.SQL Query for getting Orphan Records.

1. The following query retrieves "2" highest paid employees FROM each Department :

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
2. Query that will display the total no. of employees, and of that total the number who
were hired in 1980, 1981, 1982, and 1983. Give appropriate column headings.
I am looking at the following output. We need to stick to this format.
Total
1980
1981
1982
1983
-----------
------------
------------
-------------
-----------
14
1
10
2
1

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

FROM emp;
Index
3. 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.deptno)
FROM emp a
ORDER BY a.deptno;

OUTPUT :
=======
DEPTNO
ENAME
SAL
SUM (SAL)
=========
=======
====
=========
10
KING
5000
11725
30
BLAKE
2850
10900
10
CLARK
2450
11725
10
JONES
2975
11725
30
MARTIN
1250
10900
30
ALLEN
1600
10900
30
TURNER
1500
10900
30
JAMES
950
10900
30
WARD
2750
10900
20
SMITH
8000
33000
20
SCOTT
3000
33000
20
MILLER
20000
33000
Index

4. Create a matrix query to display the job, the salary for that job based on department
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
Dept 10
Dept 20
Dept 30
Total
----------
---------------
-------------
-------------
---------
ANALYST
6000
6000
CLERK
1300
1900
950
4150
MANAGER
2450
2975
2850
8275
PRESIDENT
5000
5000
SALESMAN
5600
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
5. 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
6. 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
7. 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
8. 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
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...