0% found this document useful (0 votes)
11 views

SQL Interview Questions and Answers

Uploaded by

santoshdvg1997
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

SQL Interview Questions and Answers

Uploaded by

santoshdvg1997
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL Interview questions for queries

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.

Id Name Gender Salary

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

Write a query to add 2 where Nmbr is 0 and add 3 where Nmbr is 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?

Ans: ODD ROWS

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

SELECT * FROM tablename WHERE MOD (rowindex, 2) = 0; FOR EVEN ROWS

SELECT * FROM tablename WHERE MOD (rowindex, 2) = 1; FOR ODD ROWS

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

Ans: SELECT SUM(SALES_S), EXTRACT(MONTH FROM DATE_S)

FROM TAB_NAME GROUP BY EXTRACT(MONTH FROM DATE_S)

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

Eno Ename Dept_id Mgr_id Hiredate Salary


7698 BLAKE 30 7839 05-01-1981 3135
7782 CLARK 10 7839 06-09-1981 2695
7566 JONES 20 7839 04-02-1981 3272.5
7788 SCOTT 20 7566 12-09-1982 3300
7902 FORD 20 7566 12-03-1981 3300
7369 SMITH 20 7902 01-01-2010 800

Ans: Select * from emp where to_char(hiredate, 'day') like 'monday%'

OR

Select * from emp where extract (day from hiredate) like ‘Monday%’

10. EMPLOYEE WHO JOINED AND RELEASED IN THE SAME MONTH

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%'

11. Write a query to find positive and negative values

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:

Ans: CASE WHEN LOC=’CHENNAI’ THEN LOC=’CHE’


ELSE
LOCATION-à SIGNIFIES ALL OTHER LOCATIONS SHOULD GO AS IN THE TAB ‘A’
END

13. Write a query for 2nd highest salary

Ans: Using Max function

Select * from emp where sal = (Select max(sal) from where sal < (Select max(sal) from emp))

Using sub query:

Select * from
(Select * from
(Select sal from emp order by sal desc) Where rownum<=2 order by sal asc)
Where rownum=1;

Using Rank and Dense Rank()

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;

Using group by and rank()

select * from ( select Dept, sal, Dense_rank() over (partition by dept order by sal desc) as rnk from
Emp ) where rnk = 2;

Return employee name, highest salary and department Using Joins

select e.first_name ,e.last_name, e.salary, d.department_name from employee e Inner join


department d ON (e.department_id=e.department_id)
where salary IN (select max(salary) from employee)

Return highest salary, employee name, department name for each department wise using Joins

select e.first_name, e.last_name, e.salary, d.department_name from employee e Inner Join


department d ON (e.department_id=d.department_id)
where salary IN (select max(salary) from employee group by department_id
14. Write a query to find the duplicate records.

Ans: Select Name, Count(*) from emp group by name having count(*)>1

Without using group by and having clause

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)

15. Write a query to find the duplicates record rows

Ans: Select * from emp where rowid NOT IN (select max(rowid) from emp group by deptno)

16. Write a query to delete the duplicate rows.

Ans: Delete from emp where rowid NOT IN (select max(rowid) from emp group by deptno)

Delete duplicate using rank():

Delete from (select rank_over(partition by pk order by rowid) RN from table) where RN > 1

17. How to get the latest 10 records from the table?

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

SELECT ename, sal


FROM ( SELECT ename, sal, RANK() OVER (ORDER BY sal) sal_rank
FROM emp )
WHERE sal_rank <= 10;

18. To find the top3 records in the table

Ans: Select * from


(Select * from emp order by empno) emp2 where rownum<=3 order by rownum

19. SELECT query to below lists employees reporting to a Manager:

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

Ans: Select * from


(Select e.ename, e.age, d.dname from employee e, Dept d where e.id=d.did)
Where age>25 and ename like'm%'

ENAME AGE DNAME


manju 26 D2
mohan 30 D3

21. Write a query to display first n last name separately

Ans: Select firstname, substr(first_name, instr(first_name'',1,1) -1), substr(firstname(firstname,


'',1,1)+1 from T1)

22. To find the first_name and last_name sepreate into 2 columns?


Ans:
select fullname, substr(fullname, 1, instr(fullname,' ', 1, 1)-1) as first_name from emp
select fullname, substr(fullname, 1, instr(fullname,' ', 1, 1)+1) as last_name from emp

23. To merge the first_name and last_name into 1 column?


Ans:
select first_name, last_name, substr(first_name, 1, instr(first_name,' ', 1, 1)-1) || ‘ ‘ ||
substr(last_name, instr(first_name,' ', 1, 1)+1), from emp

24. Write a sql statement to get data before ‘@’ from email column

Select substr(name, 1, instr(name,'@',1,1)-1) from email

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:

Select sid, sname, marks from

(Select sid, sname, marks, dense_rank() over(order by marks) lowest,

dense_rank() over(order by marks desc) highest from student)

where lowest = 1 or highest = 1

29. To fetch the names starts from aeiou

Ans: Select * from emp where REGEXP_LIKE (name, ‘^[:AEIOU:]’)

30. Write a query to retrieve names of employees whose name starts with letter S

Ans: Select * from employees where first_name like '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'

End with ‘n’


Select * from employees where substr (first_name,-1)='a'

32. Write a query to create a empty table from an existing table.

Ans: Create table Student as Select * from emp where 1=2

33. Write a query to select unique id’s from table.

Ans: Select distinct (id) from emp

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

EID Ename EID Sal


100 A 100 2000
101 B 101 4000
102 C
103 D 103 500

Ans: Select a1.*, a2.* from a1, a2 where a1.eid=a2.eid(+) and a2.eid is null

34. Display 4 to 7 records from employee table?

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

35.Write query to get the natural number (1…10)

Ans: Select level from dual connect by level<=10

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

Ans: Select * from dba_users

38. Write query to find the table spaces


Ans: Select * from dba_tablespaces

39. Write query to find the constraints in a table

Ans: Select * from ALL_CONSTRAINTS

Select * from all_constraints where TABLE_NAME = 'EMP$'

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

You might also like