SQL Interview Questions and Answers
SQL Interview Questions and Answers
1. Given a table SALARIES, such as the one below, that has m = male and f = female values. Swap
all f and m values (i.e., change all f values to m and vice versa) with a single update query and no
intermediate temp table.
1 A m 2500
2 B f 1500
3 C m 5500
4 D f 500
Ans: UPDATE SALARIES SET Gender = CASE Gender WHEN 'm' THEN 'f' ELSE 'm' END
2. Given a table TBL with a field Nmbr that has rows with the following values:
1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1
Ans: Update TBL set Nmbr = case when Nmbr > 0 then Nmbr+3 else Nmbr+2 end;
3. How can you select only the even/odd rows from an Oracle table?
select * from (select empno, ename, sal, rownum rn from emp order by empno) where mod (rn,
2) <> 0;
EVEN ROWS
select * from (select empno, ename, sal, rownum rn from emp order by empno) where mod (rn,
2) = 0;
OR
OR
SELECT Number, (CASE WHEN ABS (Number) % 2 = 1 THEN 'odd' ELSE 'even' END) AS 'RESULT'
FROM EMP
8. Find out the monthly Sale and Monthly Average in the below table.
Sale
Pid Date s
101 1-Jan-13 100
101 2-Jan-13 200
103 1-Feb-13 300
103 2-Feb-13 400
MONTHLY AVERAGE :
SELECT SUM(SALES_S)/2 AS AVG, EXTRACT(MONTH FROM DATE_S)
FROM TAB_NAME GROUP BY EXTRACT(MONTH FROM DATE_S)
9. Write a query to find the who joined on ‘Monday’ in the employee table
OR
Select * from emp where extract (day from hiredate) like ‘Monday%’
Ans:
select * from tabl_name where to_char(start_date,'mm-dd-yy') like 'OCT%' and
to_char(end_date,'mm-dd-yy') like 'OCT%'
select * from table_name where extract(month from start_date) like 'OCT%' and extract(month from
end_date) like 'OCT%'
Ans: Select (case when number > =0 then number end) positive, (case when number < 0 then number
end) negative from table
12. Using CASE STATMENT
MOVE CHENNAI AS CHE FROM TAB A TO TAB ‘B’ and all other locations as is:
Select * from emp where sal = (Select max(sal) from where sal < (Select max(sal) from emp))
Select * from
(Select * from
(Select sal from emp order by sal desc) Where rownum<=2 order by sal asc)
Where rownum=1;
select * from ( select sal, Rank() over (order by sal desc) as rnk from Emp ) where rnk = 2;
select * from ( select sal, Dense_rank() over (order by sal desc) as rnk from Emp ) where rnk = 2;
select * from ( select Dept, sal, Dense_rank() over (partition by dept order by sal desc) as rnk from
Emp ) where rnk = 2;
Return highest salary, employee name, department name for each department wise using Joins
Ans: Select Name, Count(*) from emp group by name having count(*)>1
Select * from emp a where rowid> (select min(rowid) from emp b where a.empid= b.empid)
Select * from emp a where rowid < (select max(rowid) from emp b where a.empid = b.empid)
Ans: Select * from emp where rowid NOT IN (select max(rowid) from emp group by deptno)
Ans: Delete from emp where rowid NOT IN (select max(rowid) from emp group by deptno)
Delete from (select rank_over(partition by pk order by rowid) RN from table) where RN > 1
Ans: SELECT * FROM (select * from emp ORDER BY emp_name DESC) employee
WHERE rownum <= 10
ORDER BY rownum DESC;
SELECT * FROM (SELECT ROWNUM rownum, column1, column2, upto columnN FROM DATA_TABLE)
WHERE rownum > ( SELECT (MAX(ROWNUM)-10) FROM DATA_TABLE);
Ans: SELECT EMPNO, MGR, LEVEL FROM EMP E START WITH MGR IS NULL CONNECT BY PRIOR
EMPNO = MGR;
Self-Join for employee report to manager
Ans:
SELECT
e.first_name || ' ' || e.last_name AS employee,
m.first_name || ' ' || m.last_name AS manager
FROM
employees e
INNER JOIN
employees m ON m.employee_id = e.manager_id
ORDER BY manager;
20. To join query for retrieve employee name and dept name where age>25 and ename starts with m.
Employee
ID ENAME AGE ADDRESS Dept
101 manju 26 mys DID DNAME SALARY
103 Mahesh 22 HYD 100 D1 2000
100 Ravi 20 Bang 101 D2 5000
102 mohan 30 Chi 105 D6 2800
104 Satish 30 Bang 102 D3 4000
103 D4 2000
104 D5 8000
24. Write a sql statement to get data before ‘@’ from email column
25. How to calculate first half and last half records in the table
Ans:
Select * from emp where rownum <= (select count (*)/2 from emp)
Union
Select * from (Select * from emp order by emp_id desc)
where rownum <= (select count (*)/2 from emp)
26. How to add email validation using single sql statement
Valid:
Select * from emp where REGEXP_LIKE (email, '[a-zA-Z0-9._%-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,4}')
Invalid:
Select * from emp where email not like '%_@_%'
27. Write query to find the repeated characters from your name?
Ans:
Select REGEXP_COUNT ('manjunatha','a') as reg_count from dual
OR
Select length (name) - length (replace (name, ‘a')) from emp
Union all
Select length (name) - length (replace (name, 'n')) from emp
28. Query to show max marks and min marks together from student table
Ans:
30. Write a query to retrieve names of employees whose name starts with letter S
31. Write a query to retrieve names of employees where name starts with letter M without using like
operator?
Ans:
Select * from employees
where substr(first_name,1,1)='M'
33. Write query to fetch employee not getting salary in below tables
OR
Write a query to fetch values in table table_a that are and not in table_b without using the NOT keyword
Ans: Select a1.*, a2.* from a1, a2 where a1.eid=a2.eid(+) and a2.eid is null
Ans:
Select * from
(Select * from
(Select employee_id from employees order by employee_id)
where rownum<=7 order by employee_id desc)
Where rownum<=4 order by employee_id
OR
select * from employees where employee_id between 4 and 7
36. Write query to get the name as each letters are separated line
Ex: RAJU
OUTPUT: R
A
J
U
Ans: SELECT SUBSTR(RAJU,level,1)
FROM dual CONNECT BY level<=LENGTH(RAJU);
37. Write query to find the users
40. Write a query to find the 1st highest and 1st lowest salary from the employee table
Output:
Salary
5000
1000
4000
2000
Ans:
Select * from (select * from (select * from (select sal from emp order by salary desc) where
rownum<=1) order by sal desc) where rownum=1
union all
Select * from (select * from (select * from (select sal from emp order by sal) where
rownum<=5)order by sal asc) where rownum=1
union all
Select * from (select * from (select * from (select sal from emp order by sal desc) where
rownum<=2)order by sal asc) where rownum=1
union all
Select * from (select * from (select * from (select sal from emp order by sal asc) where
rownum<=2) order by sal desc) where rownum=1