July 2, 2017

Oracle SQL Queries Recently Asked In Wipro Interview


Oracle SQL Queries Recently in May 2017 Asked In Wipro Interview:

Consider the below DEPT and EMPLOYEE table and answer the below queries:

DEPARTMENT Table:

DEPTNO (NOT NULL , NUMBER(2)),
DNAME (VARCHAR2(14)),
LOC (VARCHAR2(13)


EMPLOYEE Table:

EMPNO (NOT NULL , NUMBER(4)),
ENAME (VARCHAR2(10)),
JOB (VARCHAR2(9)),
MGR (NUMBER(4)),
HIREDATE (DATE),
SAL (NUMBER(7,2)),
COMM (NUMBER(7,2)),
DEPTNO (NUMBER(2))

MGR is the EMPno of the Employee whom the Employee reports to.
DEPTNO is a foreign key.

1. List all the Employees who have at least one person reporting to them.

SELECT ENAME FROM EMPLOYEE WHERE EMPNO IN (SELECT MGR FROM EMPLOYEE);

2. List the highest salary paid for each job.

SELECT JOB, MAX(SAL) FROM EMPLOYEE GROUP BY JOB

3. In which year did most people join the company? Display the year and the number of Employees. 

SELECT TO_CHAR(HIREDATE,'YYYY') "YEAR", COUNT(EMPNO) "NO. OF EMPLOYEES"
FROM EMPLOYEE
GROUP BY TO_CHAR(HIREDATE,'YYYY')
HAVING COUNT(EMPNO) = (SELECT MAX(COUNT(EMPNO))
FROM EMPLOYEE
GROUP BY TO_CHAR(HIREDATE,'YYYY'));

4. Write a correlated sub-query to list out the Employees who earn more than the average salary of their department. 

SELECT ENAME,SAL
FROM EMPLOYEE E
WHERE SAL > (SELECT AVG(SAL)
FROM EMPLOYEE F
WHERE E.DEPTNO = F.DEPTNO);

5. Find the nth maximum salary. 

SELECT ENAME, SAL
FROM EMPLOYEE A
WHERE &N = (SELECT COUNT (DISTINCT(SAL))
FROM EMPLOYEE B
WHERE A.SAL<=B.SAL);

6. Select the duplicate records (Records, which are inserted, that already exist) in the EMPLOYEE table. 

SELECT * FROM EMPLOYEE A
WHERE A.EMPNO IN (SELECT EMPNO                                        
FROM EMPLOYEE                                        
GROUP BY EMPNO                                        
HAVING COUNT(EMPNO)>1)
AND A.ROWID!=MIN (ROWID));

7. Write a query to list the length of service of the Employees (of the form n years and m months). 

SELECT ENAME "EMPLOYEE",TO_CHAR(TRUNC(MONTHS_BETWEEN(SYSDATE,HIREDATE)/12))
||' YEARS '|| TO_CHAR(TRUNC(MOD(MONTHS_BETWEEN
(SYSDATE, HIREDATE),12)))||' MONTHS ' "LENGTH OF SERVICE"
FROM EMPLOYEE;



Subscribe to get more Posts :