Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

1.

Highest salary in emp table


Select * from emp e where 1= (select count(sal) from emp where e.sal <=sal);
OR
Select empno,ename,sal from (select empno,ename,sal,row_number() over (order by sal desc)
Rn from emp) where Rn=1;

2. Delete the duplicate records.


Delete from emp where row id in (select max(rowid) from emp group by eno,ename)
OR
Delete from emp e where e.rowid> (select min(rowid) from emp b where a.empid=b.empid);
OR
Delete from emp where rowid !=(select max(rowid) from emp group by empno);
OR without using Rowid,rownum
Delete from emp where (empno, ename,sal) in (select max(empno),ename,sal from emp group
By empno );

3. Display the duplicate records.


Select eno, count(*) from emp group by eno having count(*) >1;
Without count function:
Select * from emp where rowid in (select max(rowid) from emp group by eno);
4. Challenges and Defects found on ETL Testing
1. Found round function is not working on target data is like truncating the data.
2. In source side getting duplicate data, we need to discuss with source to apply the
surrogate key on source data base side.
3. Data truncation like string length char’s are more like 60 truncating some data.
4. Applying the transformations like joiner it involves more tables
5. In reporting side when we do negative testing like applying invalid data found like
accepting data
6. In reporting side when we do security testing like some of the managers should not able
to access the reporting but they are accessing the reports.
7. Some alignments like expecting radio buttons misplaced.
8. Getting some old data like decommission data came on.

5. Write a query for top 10 salaries.


Select ename,sal from (select * from emp order by sal ) where row id <11;
Dense rank: select empno,ename,sal from (select empno,ename,sal ,dense_rank () over (order
by sal desc)) Dr from emp
Where Dr<=10;

6. Display employees for 20 to 30 for each dept


Select deptno, count(*) from dept group by deptno having count(*) between 20 and 30;
7. Display 1’st and Last Record from EMP Table.
Select T.* from (select emp.*,row_number( ) over (order by hiredate) as seqnum, count(*)
over() as Cnt from emp) T where seqnum=1 or seqnum=Cnt;

8. Display 10-30 records from emp table.


Select empno,ename,sal from emp where rowid=(select rowid from emp where rownum<=10
Minus select rowid from emp where nownum<=30);

9. Create table using existing table with any data


Create table temp as (select * from emp where 1=2);

10. How do you combine the 2 tables/view together for instance one table contain 4 rows and 2
table contain 5 rows and have exactly same fields? Write a query to show the all data?
Create view view_name as (select * from A1 union all select * from A2);

11. IN Emp Table I want to display as M as F and F as M to display

Empno Ename Sex


1 ABC M
2 SAM F
3 JAN M
4 JACK M
5 ZoYA F

Select empno,ename,sex,case
When Sex=’M’ then ‘F’
When Sex=’F’ then ‘M’
Else default
End from emp;

12. IN Emp table I want to remove the special char’s


Empno Ename
1 AB#@$C
2 SA%$$M
3 JA*&*N
4 J@#$ACK
5 Z*&&^oYA

13. Replace(ename,(‘A-Z’)@#$%^&*’,(A-Z))

Empno Eid Eo Eid


1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1

14. What is the O/P for Leftjon/Right join/outer join

ANS: 16

Left join: 16

Right join: 16

Outer join : 16

15. Display ‘MON’ details from emp table

Empno Date
1 10/1/1990
2 5/9/2009
3 23/9/2017
4 29/6/2017
5 15/6/1999

Select to_char(date,’mon’) from emp;

Select to_char(date,’mm-yyyy’) from emp;


o/p: 01/1990

16. Display +ve and –ve values from emp table.


Select ename,sal,case when no<0 then ‘-ve’
When no>0 then ‘+ve’
Else end
From emp;

17. Using case statement to display the data in target like below
Select ename,case
When no>=1 and no<=10 then 1
when no>=11 and no<=20 then 2
When no>=21 and no<=30 then 3
.
.
.
When no>=91 and no<=100 then 10
End default
From emp;

18. Using Sub string and In-string and Sub-String

Empno Ename Email


1 ABC abc@gmail.com
2 SAM Sam@yahoo.co.in
3 JAN jan@outlook.com
4 JACK jack@hotmail.com
5 ZoYA Zoya@gmail.com

Display gmail.com,yahoo.com
Select ename,eno,substr(email,instr(email,’@’)+1) from emp

Display com, co.in from all Emails


Select ename,eno,substr(email,instr(email,’.’) +1) from emp;

Display abc,sam,jan,jack,zoya from Email


Select ename,eno,substr(email,1,instr(email,’@’,1) -1) from emp;

19. Display LEAD and Lag and Diff data

Empno Ename SAL LAG LEAD DIFF


1 ABC 1000 0 1000 1000
2 SAM 2000 1000 2000 1000
3 JAN 5000 2000 5000 3000
4 JACK 9000 5000 9000 4000
5 ZoYA 12000 9000 0 9000

Select empno,ename,sal,lag(sal,1,0) over(order by sal) as LAG,lead(sal,1,0) over(order by sal) as


LEAD,sal-lag(sal,1,0) over(order by sal) as DIFF from emp;

Lag: It returns values from a previous row in the table


Lead: It return a value from the next row in the table
20.
Empno Ename
1 Dude maha basha
2 Dude nafees Ahmed
3 Dude Zoya shan
4 marry chist jess
5 jai sai ram

In target Ename is splitting into 3 names i.e first name,middle name,last name.Write a query?

Select
Regexp_substr(ename,’[ ^]+’,1,1) FN
Regexp_substr(ename,’[ ^]+’,2,1) MN
Regexp_substr(ename,’[ ^]+’,2,3) LN
From emp;

21. 10:20:30:40 display into separate columns as C1,C2,C3 and C4

With T as
(select ‘10:20:30:40’ no from dual)
Select regexp_substr (no,’[^:]+’,1,1) as C1
regexp_substr (no,’[^:]+’,2,1) as C2
regexp_substr (no,’[^:]+’,2,3) as C3
regexp_substr (no,’[^:]+’,3,3) as C4 from T;

22. Write a query to display the EMP who completed 5 years.


Select ename from emp where sysdate-hiredate>5*365 ;

23. Max salary department wise.


Select max(sal) from emp where sal not in (select max(sal) from emp group by deptno) group by
dept no;
OR Select max(sal) from emp group by dept no;

24. First highest salary depart wise


Select * from emp e1 where &n=(select count(distinct sal) from emp e2 where e1.sal<=e2.sal
and e1.depno=e2.deptno;

25. Display even and odd numbers

Even: Select * from emp where (row_id,0) in (select row_id, mod(row_num,2) from emp);

Odd: Select * from emp where (row_id,1) in (select row_id, mod(row_num,2) from emp);
26. Display every 4’th(4,8,12,16 …) row in a table

Select * from emp where (rowid,0) in (select rowid,mod(rownum,4) from emp;

27. Display the emp name whose sal is greater than manager.
Select ename from emp e,emp m where e.mgr=e.name and e.sal>m.Sal;

28. What is level of granularity?


Level of details that you put in to the fact table.

29. Write a query like sum of positive and negative values.

SELECT SUM (CASE WHEN


POSITIVE>0 THEN POSITIVE
ELSE 0
END) SUM OF POSITIVE VALUES,
SUM (CASE WHEN
NEGATIVE<0 THEN NEGATIVE
ELSE 0
END) SUM OF NEGATIVE VALUES
FROM TABLE_NAME

30. Write a query to fetch only Alphabets?

SELECT COL1 FROM TABLE WHERE REGEXP_LIKE (COL1,’ [A-Z]’)

31. Write a query to fetch only Numerical values.

SELECT COL1 FROM TABLE WHERE REGEXP_LIKE (COL1,’ [0-9]’)

32. Sub string


Substr(‘Helloworld’,3)- Result:- lloworld
Substr(‘Helloworld’,1,1)- Result:-H
Substr(‘Helloworld’,1,2)- Result:-He
Substr(‘Helloworld’,-1,1)- Result:-D

33. Display first and last char from string.

Empno Ename
1 ABC XYZ
2 SAM RAM
3 JAN DEC
4 JACK ROCK
5 ZoYA MAYA

Select ename,substr(ename,1,1)||’ ‘|| substr(ename,length(ename)) from emp;

O/P: A z
A M
J C
J K
Z A

First letter of first string and first letter of next strings

SELECT ENAME,SUBSTR(ENAME,1,1) || ‘ ‘||


SUBSTR(SUBSTR(ENAME,INSTR(ENAME,’’,1,1)+1,LENGTH(ENAME))1,1) FROM EMP;

34. Select ename starts with S without using Like operator.


Select * from emp where charindex(‘S’,ename)=1;
OR Select * from emp where substr(ename,1,1)=’S’

Result: Empno Ename


1 SCOTT
2 SMITH

35. write a query to get the name of departments who do not have any employees
SELECT dname FROM dept d
WHERE not exists (select 1 from emp e where e.deptno = d.deptno);
OR
Select d.dname from emp e , dept d
d.deptno=e.deptno where deptno is null;

36. FETCH POSITIVE VALUES AND NEGATIVE VALUES:

SELECT sex,(CASE WHEN


POSITIVE>0 THEN POSITIVE
ELSE 0
END) POSITIVE VALUES,
(CASE WHEN
NEGATIVE<0 THEN NEGATIVE
ELSE 0
END) NEGATIVE VALUES
FROM TABLE_NAME

37. Pivot table converting row data in columns.

Select * from
(Select Sid, salesagent, salescountry, salesamount from Sales_product) Temp
Pivot
(sum(salesamount) for each salescountry in(‘IND’,’US’,’UK’)) Pivot_table;

38. Replace function

Replace the sequence of char in a string with another set of characters.

Select Replace (‘123123Tech’,123) from dual – Tech

Replace(‘123Tech123’,123) – Tech

Replace(‘222Tech’,’2’,’3’) – 333Tech

Replace(‘000000123’,0) – 123

Replace(‘000000123’,’0’,’ ‘) –‘ 123’

39. Display the hire date who joined 1990 after


Select * from emp where to_char(hiredate,'yyyy')>=1998;

You might also like