Oracle Query
Oracle Query
Oracle Query
html
Question 1: SQL Query to find second highest salary of Employee
select MAX(Salary) from Employee WHERE Salary NOT IN (select
MAX(Salary) from Employee );
GROUP BY DeptID.
This questions become more interesting if Interviewer will ask you to print
department name instead of department id, in that case you need to join
Employee table with Department using foreign key DeptID, make sure you
do LEFT OUTER JOIN to include departments without any employee as
well. Here is the query
Question 4: Write an SQL Query to check whether date passed to Query is date
of given format or not.
Answer : SQL has IsDate() function which is used to check passed value is date or
not of specified format ,it returns 1(true) or 0(false) accordingly.
Remember ISDATE() is a MSSQL function and it may not work on Oracle,
MySQL or any other database but there would be something similar.
SELECT
ISDATE('1/08/13') AS "MM/DD/YY";
Question 5: Write a SQL Query to print the name of distinct employee whose
DOB is between 01/01/1960 to 31/12/1975.
Answer : This SQL query is tricky but you can use BETWEEN clause to get all
records whose date fall between two dates.
SELECT DISTINCT EmpName FROM Employees WHERE DOB
BETWEEN 01/01/1960
AND 31/12/1975;
WHERE
Employees WHERE
Salary>=10000;
Question 8: Write an SQL Query to find name of employee whose name Start
with M
Answer :
SELECT * FROM Employees WHERE EmpName like 'M%';
Question 9: find all Employee records containing the word "Joe", regardless of
whether it was stored as JOE, Joe, or joe.
Answer :
SELECT * from Employees
WHERE
to Delete:
DELETE FROM emp a WHERE rowid != (SELECT MAX(rowid) FROM emp b WHERE
a.empno=b.empno);
Question 13 : How do you find all employees which are also manager? .
You have given an standard employee table with an additional
column mgr_id, which contains employee id of manager.
Answer : You need to know about self join to solve this problem. In Self Join,
you can join two instances of same table to find out additional details as
shown below
this will show employee name and manger name in two column e.g.
name manager_name
John David
One follow-up is to modify this query to include employees which doesn't have
manager. To solve that, instead of using inner join, just use left outer join, this
will also include employees without managers.
Question 14 : You have a composite index of three columns, and you
only provide value of two columns in WHERE clause of a select query?
Will Index be used for this operation? For example if Index is
If the given two columns are secondary index column then index will not
invoke, but if the given 2 columns contain primary index(first col while creating
index) then index will invoke. In this case Index will be used
because EmpId and EmpFirstName are primary columns.
SUB Queries:
1. List the employees working in research department
2. List employees who are located in New York and Chicago
3. Display the department name in which ANALYSTS are working
4. Display employees who are reporting to JONES
5. Display all the employees who are reporting to Jones Manager
6. Display all the managers in SALES and ACCOUNTING department
7. Display all the employee names in Research and Sales Department who are having at
least 1 person reporting to them
8. Display all employees who do not have any reportees
9. List employees who are having at least 2 reporting
10. List the department names which are having more than 5 employees
11. List department name having at-least 3 salesman
12. List employees from research and accounting having at-least 2 reporting
13. Display second max salary
14. Display 4th max salary
15. Display 5th max salary -- Answer for nth Max Salary
Co-Related Subqueries:
16. Write a query to get 4th max salary from EMP table
17. Write a query to get 2nd & 6th max salary from EMP table
18. Write a query to get first 3 salaries from the EMP table
19. Write a query to get 2nd least salary from the EMP table
20. Write a query to get least 3 salaries from the EMP table
21. List all the employees whose salaries are greater than their respective departmental
average salary.
1. SQL> select empno, ename from emp where deptno=(select deptno from dept where
dname='RESEARCH');
2. SQL> select empno, ename from emp where deptno in (select deptno from dept where
loc in ('NEW YORK','CHICAGO'));
3. SQL> select dname from dept where deptno in ( select deptno from emp where job
='ANALYST');
4. SQL> select empno, ename, mgr from emp where mgr = (select empno from emp where
ename='JONES');
5. SQL> select empno, ename, mgr from emp where mgr = (select mgr from emp where
ename='JONES')
6. SQL> select empno, ename, job from emp where deptno in ( select deptno from dept
where dname in ('SALES','ACCOUNTING'))
7. SQL> select empno, ename, job from emp where deptno in ( select deptno from dept
where dname in ('SALES','RESEARCH')) and empno in (select mgr from emp)
8. SQL> select empno, ename from emp where empno not in ( select mgr from emp where
mgr is not null)
9. select empno, ename from emp where empno in (select mgr from emp group by mgr
having count(*) >= 2)
10. SQL> select dname from dept where deptno in (select deptno from emp group by
deptno having count(*) >=5)
11. SQL> select deptno, job, count(*) from emp where job = 'SALESMAN' group by deptno,
job having count(*) >=3
12. SQL> select empno, ename, deptno from emp where empno in (select mgr from emp
group by mgr
having count(*) >= 2) and deptno in (select deptno from dept where dname='RESEARCH'
or dname='ACCOUNTING')
13. SQL>select max(sal) from emp where sal < (select max(sal) from emp);
14. SQL> select max(sal) from emp where sal < (select max(sal) from emp where sal <
(select max(sal) from emp where sal < (select max(sal) from emp)))
BASIC SELECT Statement:
1. Display all rows and all columns of emp table
2. Display any 2 columns of emp table
3. Calculate annual salary with Quarterly commission of 500
4. Display distinct salaries of all the employees
5. Display output as following,
"Hello SMITH your salary is 5000"
6. Is the following statement correct,
SeleCT ENAME,deptno FROM emp;
select *,ename from emp;
select ename deptno from emp;
21. List employees in all dept whose salary not in the range of 2000 to 3000 with the job
which is having a string called 'MAN'
Here SMITH is ename column, CLERK is JOB and 2000 is SAL column and rest everything
is literal strings.