0% found this document useful (0 votes)
76 views11 pages

VVIMP SQL

The document provides SQL query examples for various scenarios involving multi-row sub-queries, string functions, and data retrieval from an employee table. It covers operations such as filtering employees based on salary, hire dates, and name characteristics, as well as retrieving specific records and calculating aggregates. Additionally, it includes syntax for obtaining the Nth maximum and minimum salaries, and manipulating string data for employee names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views11 pages

VVIMP SQL

The document provides SQL query examples for various scenarios involving multi-row sub-queries, string functions, and data retrieval from an employee table. It covers operations such as filtering employees based on salary, hire dates, and name characteristics, as well as retrieving specific records and calculating aggregates. Additionally, it includes syntax for obtaining the Nth maximum and minimum salaries, and manipulating string data for employee names.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

VVIMP_QUES

MULTI-ROW SUB-QUERY

• If, inner query returns more than one output to the outer query, we call it as
Muti-row Sub query.
• We can achieve multi-row sub-query by using ‘ALL’ & ‘ANY’ operator.

1] WAQTD details of an employee who are earning more than all the
‘SALESMAN’.

SELECT *
FROM EMP
WHERE SAL > ALL (SELECT SAL
FROM EMP
WHERE JOB = ‘SALESMAN’);

2] WAQTD names of an employee hired after all the ‘MANAGER’ &


earning more than all the ‘CLERK’.

SELECT ENAME
FROM EMP
WHERE HIREDATE > ALL (SELECT HIREDATE
FROM EMP
WHERE JOB = ‘MANAGER’) AND
SAL > ALL (SELECT SAL
FROM EMP
WHERE JOB = ‘CLERK’);

3] WAQTD name & salary of an employee if they are earning more than at
least a ‘MANAGER’.

SELECT ENAME, SAL


FROM EMP
WHERE SAL > ANY (SELECT SAL
FROM EMP
WHERE JOB = ‘MANAGER’);

4] WAQTD details of the employees working as ‘CLERK’ & hired before at


least a ‘SALESMAN’.

SELECT *
FROM EMP
WHERE JOB = ‘CLERK’ AND
HIREDATE < ANY (SELECT HIREDATE
FROM EMP
WHERE JOB = ‘SALESMAN’);
5] WAQTD 3rd record from EMP table. (Conceptual Question)

SELECT *
FROM (SELECT ROWNUM AS SLNO, EMP.*
FROM EMP)
WHERE SLNO = 3;

(HERE IN QUES THEY MIGHT ASK ANY NUMBER OF RECORD YOU


SHOULD USE THE ABOVE METHODE TO SOLVE THESE KIND OF
PROBLEMS)

6] WAQTD last 3 records from EMP table. (Important)

SELECT *
FROM (SELECT ROWNUM SLNO, EMP.*
FROM EMP
ORDER BY SLNO DESC)
WHERE ROWNUM<=3;

Syntax for Nth MAX(SAL)


SELECT SAL
FROM (SELECT ROWNUM SLNO, SAL
FROM (SELECT DISTINCT SAL
FROM EMP
ORDER BY SAL DESC))
WHERE SLNO = N;

Syntax for Nth MIN(SAL)

SELECT SAL
FROM (SELECT ROWNUM SLNO, SAL
FROM (SELECT DISTINCT SAL
FROM EMP
ORDER BY SAL ASC))
WHERE SLNO = N;

7] WAQTD first character of names of all the employees.(USING SRF)

SELECT SUBSTR (ENAME, 1, 1)


FROM EMP;

8] WAQTD first 3 characters of the name of all the employees.


SELECT SUBSTR (ENAME, 1, 3)
FROM EMP;

9] WAQTD details of an employee whose name starts with character ‘A’


using single row function.

SELECT *
FROM EMP
WHERE SUBSTR (ENAME, 1, 1) = ‘A’;

10] WAQTD details of an employee whose designation ends with character


‘N’.

SELECT *
FROM EMP
WHERE SUBSTR (JOB, -1, 1) = ‘N’;

11] WAQTD names of an employee whose name starts with character ‘A’ or
‘S’.

SELECT ENAME
FROM EMP
WHERE SUBSTR (ENAME, 1, 1) IN (‘A’, ‘S’);
12] WAQTD details of an employee whose name starts with vowels.

SELECT *
FROM EMP
WHERE SUBSTR (ENAME, 1, 1) IN (‘A’, ‘E’, ‘I’, ‘O’, ‘U’);

13] WAQTD names of an employee along with number of characters they are
having in their name.

SELECT ENAME, LENGTH (‘ENAME’)


FROM EMP;

14] WAQTD number of employees having exactly 4 characters in his name


using single row function.

SELECT COUNT (*)


FROM EMP
WHERE LENGTH (ENAME) = 4;

15] WAQTD name in uppercase, designation in lowercase if their name has


five characters & getting 3-digit salary.

SELECT UPPER (ENAME), LOWER (JOB)


FROM EMP
WHERE LENGTH (ENAME) = 5 AND LENGTH (SAL) = 3;

16] WAQTD first character & last character of employee name in uppercase
& rest of the characters in lowercase.
(VERY MOST IMP)

SELECT UPPER(SUBSTR(ENAME,1,1)) ||
LOWER(SUBSTR(ENAME,2,LENGTH(ENAME)-2)) ||
UPPER(SUBSTR(ENAME,-1,1))
FROM EMP;

17] WAQTD first half of employee name in uppercase & second half in
lowercase & reverse.

SELECT UPPER(SUBSTR(ENAME,1,2)) ||
REVERSE(LOWER(SUBSTR(ENAME,3,LENGTH(ENAME)-1)))
FROM EMP;

18] WAQTD details of an employee hired on SATURDAY.

SELECT *
FROM EMP
WHERE TO_CHAR (HIREDATE, ‘DY’) = ‘SAT’;

19] WAQTD details of an employee hired on FRIDAY, SATURDAY &


SUNDAY.

SELECT *
FROM EMP
WHERE TO_CHAR (HIREDATE, ‘DY’) IN (‘FRI’, ‘SAT’, ‘SUN’);

20] WAQTD details of an employee hired in a month of OCTOBER,


NOVEMBER & DECEMBER.

SELECT *
FROM EMP
WHERE TO_CHAR (HIREDATE, ‘MON’) IN (‘OCT’, ‘NOV’,
‘DEC’);

OR,

SELECT *
FROM EMP
WHERE TO_CHAR (HIREDATE, ‘MM’) IN (10, 11, 12);
21] WAQTD details of an employee if their name having character ‘A’ using
Single Row Function.

SELECT *
FROM EMP
WHERE INSTR (ENAME, ‘A’, 1, 1) > 0;

22] WAQTD details of an employee who is having at least two ‘A’ in his
name using single row function.

SELECT *
FROM EMP
WHERE INSTR (ENAME, ‘A’, 1, 2) > 0;

23] WAQTD total salary given to each employee.

SELECT ENAME, SAL, COMM, SAL+NVL (COMM, 0)


FROM EMP;

24] WAQTD 2ND MAX SAL IN EACH DEPTNO.

SELECT E1.DEPTNO,MAX(E1.SAL)
FROM EMP E1, EMP E2
WHERE E1.SAL < (SELECT MAX(SAL)
FROM EMP E2
WHERE E1.DEPTNO = E2.DEPTNO)
GROUP BY E1.DEPTNO;

25] WAQTD FIRST HALF RECORDS IN EMP TABLE.

SELECT *
FROM (SELECT ROWNUM SLNO, EMP.*
FROM EMP)
WHERE SLNO <= (SELECT COUNT(*)/2
FROM EMP);

26] WAQTD SECOND HALF RECORDS IN EMP TABLE.

SELECT *
FROM (SELECT ROWNUM SLNO, EMP.*
FROM EMP)
WHERE SLNO > (SELECT COUNT(*)/2
FROM EMP);

27] WAQTD LAST 5 RECORDS IN EMP TABLE.


SELECT *
FROM (SELECT ROWNUM SLNO, EMP.*
FROM EMP
ORDER BY SLNO DESC)
WHERE ROWNUM < 6;

You might also like