You are on page 1of 10

http://www.mithunashok.com/2011/08/answers-for-basic-select-with.

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 );

Question 2: SQL Query to find Max Salary from each department.


Answer : You can find maximum salary for each department by grouping all records
by DeptId and then using MAX() function to calculate maximum salary in each group
or each department.
SELECT DeptID, 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

SELECT DeptName, MAX(Salary) FROM Employee e LEFT JOIN Department d


ON e.DeptId = d.DeptID;

Question 3: Write SQL Query to display current date.


Answer : SQL has built in function called GetDate() which returns current
timestamp. This will work in Microsoft SQL Server, other vendors like Oracle and
MySQL also has equivalent functions.
SELECT GetDate();

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";

It will return 0 because passed date is not in correct format.

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;

Question 6: Write an SQL Query find number of employees according to


gender whose DOB is between 01/01/1960 to 31/12/1975.
Answer :
SELECT COUNT(*), sex from Employees
AND '31/12/1975' GROUP BY sex;

WHERE

DOB BETWEEN '01/01/1960'

Question 7: Write an SQL Query to find employee whose Salary is equal or


greater than 10000.
Answer :
SELECT EmpName FROM

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

UPPER(EmpName) like '%JOE%';

Question 10: Write a SQL Query to find year from date.


Answer : Here is how you can find Year from a Date in SQL Server 2008
SELECT YEAR(GETDATE()) as "Year";

Question 11 : Write SQL Query to find duplicate rows in a database? and


then write SQL query to delete them?
Answer : You can use following query to select distinct records :
SELECT * FROM emp a WHERE rowid = (SELECT MAX(rowid) FROM EMP b
WHERE a.empno=b.empno)

to Delete:
DELETE FROM emp a WHERE rowid != (SELECT MAX(rowid) FROM emp b WHERE
a.empno=b.empno);

Question 12 : There is a table which contains two column Student and


Marks, you need to find all the students, whose marks are greater than
average marks i.e. list of above average students.
Answer : This query can be written using sub query as shown below :
SELECT student, marks from table where marks > SELECT AVG(marks)
from table)

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

SELECT e.name, m.name FROM Employee e, Employee m WHERE e.mgr_id =


m.emp_id;

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

on EmpId, EmpFirstName and EmpSecondName and you write query like

SELECT * FROM Employee WHERE EmpId=2 and EmpFirstName='Radhe'

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;

Questions on BASIC SELECT with Conditions


1. Display all the employees whose name starts with 'S'
2. List the employees name having letter 'L' as the second character
3. List the employees name having 'E' as last but one character
4. List the employees name having exactly 4 letters
5. List the employee whose name is having letter 'L'
6. List the employees name having atleast 5 characters
7. List employees earning between 2000 and 3000
9. List emp who do not have any reporting manager or commision is Null
10. List emp who do not have any reporting manager AND commision is Null
11. List only managers
12. List managers working in dept 10 and 20
13. List all the clerks and analysts with salary atleast 1000 in dept 20 and 30
14. List the employees in dept 20 and 30 who get no commision
15. List employees whose name starts with either 'A' or 'S' in dept 20
16. List all the employee whoe name does not end with 'S' in dept 20 and 30
17. List the employees who are getting some commision with sal > 1500 in dept 30
18. List the employees who are getting some commision with sal>1500 in dept 30
19. List emp working as managers and clerks with Salary atleast 2000 except in dept 10
and 20
20. List emp who get commmision

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'

Answers on Basic SELECT statement with Conditions:

1. SQL> SELECT * FROM emp WHERE ename LIKE 'S%';


2. SQL> SELECT * FROM emp WHERE ename LIKE '_L%';
3. SQL> SELECT * FROM emp WHERE ename LIKE '%E_';
4. SQL> SELECT * FROM emp WHERE ename LIKE '____';
5. SQL> SELECT * FROM emp WHERE ename LIKE '%L%';
6. SQL> SELECT * FROM emp WHERE ename LIKE '_____%';
7. SQL> SELECT * FROM emp WHERE sal BETWEEN 2000 and 3000;
9. SQL> SELECT * FROM emp WHERE mgr IS NULL OR COMM IS NULL;
10. SQL> SELECT * FROM emp WHERE mgr IS NULL AND COMM IS NULL;
11. SQL> SELECT * from emp WHERE job = 'MANAGER';
12. SQL> SELECT * from emp WHERE job = 'MANAGER' and deptno in (10,20);
13. SQL> SELECT * FROM emp WHERE job IN ('CLERK','ANALYST') AND sal >= 1000
AND deptno IN (20,30);
14. SQL> SELECT * FROM emp where deptno in (20,30) and comm IS NULL;
15. SQL> SELECT * FROM emp WHERE ename LIKE ('A%') OR ename LIKE ('S%');
16. SQL>SELECT * FROM emp WHERE ename NOT LIKE ('%S') AND deptno IN (20,30);
17. SQL> SELECT * FROM emp where comm IS NOT NULL AND sal > 1500 AND deptno
= 30;
18. SQL> SELECT * FROM emp where comm IS NOT NULL AND sal > 1500 AND job =
'MANAGER'
19. SQL> SELECT * FROM emp WHERE job = 'MANAGER' OR job = 'CLERK' AND sal
>=2000 AND deptno NOT IN (10,20);

20. SQL> SELECT * FROM emp WHERE COMM IS NOT NULL;


21. SQL> SELECT * from emp WHERE sal NOT BETWEEN 2000 AND 3000 AND job LIKE
('%MAN%');

SQL Interview Questions on Functions


SQL Functions:
Use only functions for data manupulation.
1. List employees whose name having 4 characters
2. List employees whose job is having 7 characters
3. Find out howmany times letter 'S' occurs in 'qspiders'
4. List the employees whose job is having last 3 characters as 'man'
5. List employees whose job is having first 3 characters as 'man'
6. Display all the names whose name is having exactly 1 'L'
7. Display dept names which are having letter 'O'
8. Display the output as shown below,
Scott working as a clerk earns 3000 in dept 20
9. Display employees who earn odd numbered salaries
10. Display number of employees getting NULL comission.
11. Display total sal and comm drawn by dept 30
12. Count number of clerks in dept 10 and 20
13. List Department wise total salary
14. List department wise total sal only if the total sal is > 3000
15. Display job wise total salary excluding dept 30 only if the total salary is > 5000
16. Display job wise max sal only for managers, clerks, salesman working in dept 10 and
20. Sort the data by des order of the max salary.
17. Display job wise number of employees in all the department with total salary > 9000
18. Display the department number having atleast 4 employees in it
19. Display the department having only saleman in it
20 Calculate number of L in string 'HELLLLL'
21. Display all the employees whose job has a string 'MAN'
22. Display all the employees whose job starts with string 'MAN'
23. Display all the employees whose job ends with string 'MAN'
25. Display first 3 characters of ename in lower case and rest everything in upper case.
If ename is 'MITHUNASHOK' then display this as 'mitHUNASHOK'
26. Display the result from emp table as below.
SMITH is a CLERK and gets salary 2000

Here SMITH is ename column, CLERK is JOB and 2000 is SAL column and rest everything
is literal strings.

Answers for SQL Functions


1. SQL> SELECT empno, ename FROM emp WHERE Length(ename) = 4;
2. SQL> SELECT empno, ename, job FROM emp where Length(job)=7;
3. SQL> SELECT Length('qspiders') - Length(replace('qspiders','s','')) FROM dual;
4. SQL> SELECT empno, ename, job FROM emp WHERE Instr(job,'MAN') >0;
5. SQL> SELECT empno, ename, job FROM emp WHERE Instr(job, 'MAN') =1;
6. SQL> SELECT empno, ename, job FROM emp WHERE (Length(ename) Length(Replace(ename, 'L',''))) = 1;
7. SQL> SELECT * FROM dept WHERE Instr(dname,'O') > 0;
8. SQL> SELECT Concat(ename,' working as a ') || Concat(job, ' earns ') || Concat(sal, ' in ') || Conc
at('dept ',deptno) AS text from emp;
OR
SQL> SELECT Concat(Concat(Concat(Concat(Concat(Concat(Concat(ename,' working as a '), job),'
earns '), sal),' in '),'dept '), deptno) AS text FROM emp;
9. SQL> SELECT empno, ename, sal FROM emp WHERE mod(sal,2) > 0;