0% found this document useful (0 votes)
27 views9 pages

SDSFSD

Uploaded by

Vishal Maheshuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views9 pages

SDSFSD

Uploaded by

Vishal Maheshuni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

ProductID, ProductName, Category, Discount

102, XYZ, Electronic, 5%


101, ABC, Electronic, Null

result:
ProductID, ProductName, Category, and Discount
102, XYZ, Electronic, 5%
101, ABC, Electronic, No Discount

select ProductID, ProductName, Category, isnull(discount, no discount) from table

table Transactions with columns TransactionID, AccountID, TransactionDate, and


Amount.
Write a query to find all transactions that occurred in the last 30 days.

select TransactionID, AccountID, TransactionDate, amount from table


where datediff(Days,TransactionDate,getdate()) between 0 to 30

getdate-yesterdaydate =

List the employee those name start with vowels

select name from table where regexp_like(name, '^[aeiou]')

with cte as
(
select *, row_number() over (partition by empid order by salary desc) as rnk from
emp
)

delete from cte


where rnk<1

1. Considerthe following employees data as source


employee_id, salary
10, 1000
20, 2000
30, 3000
40, 5000
Q1. Design a mapping to load the cumulative sum of salaries of employees into
targettable?
The target table data should look like as
employee_id, salary, cumulative_sum
10, 1000, 1000
20, 2000, 3000
30, 3000, 6000
40, 5000, 11000

Sales

10000

null
null

select coalsce(sum(sales)) from table

ProductID, ProductName, Category, Discount

102, XYZ, Electronic, 5%

101, ABC, Electronic, Null

result:

ProductID, ProductName, Category, and Discount

102, XYZ, Electronic, 5%

101, ABC, Electronic, No Discount

select ProductID, ProductName, Category, isnull(discount, no discount) from table

table Transactions with columns TransactionID, AccountID, TransactionDate, and


Amount.

Write a query to find all transactions that occurred in the last 30 days.

select TransactionID, AccountID, TransactionDate, amount from table

where datediff(Days,TransactionDate,getdate()) between 0 to 30

List the employee those name start with vowels

select name from table where regexp_like(name, '^[aeiou]')

with cte as

select *, row_number() over (partition by empid order by salary desc) as rnk from
emp

delete from cte

where rnk<1

1. Considerthe following employees data as source

employee_id, salary

10, 1000

20, 2000

30, 3000
40, 5000

Q1. Design a mapping to load the cumulative sumof salaries of employees into

targettable?

The target table data should look like as

employee_id, salary, cumulative_sum

10, 1000, 1000

20, 2000, 3000

30, 3000, 6000

40, 5000, 11000

Changing the (NO.Rows, TRansaction Boundary, RowType)

AGGREGATOR, Connected, Active , Performs aggregation


SOURCE QUALIFIER, Connected, Active, overrideSQlq, Represents the rows that IS
reads from an application
EXPRESSIONt Connected, Passive, For performing calculations
FILTERt, connected, Active, Filters Data
Input, passive and connected, Defines mapplets Input
JOINERt Connected, Active, Joins datasets
LOOKUpT COn/Uncon, ActiveAndPassive To lookup values and Return on match
NORMALIZER Conn/Active Normalize cobol source data
RANK Con/Active, Limits the Records, Limits the records on ranks
Router Conn/Active Routes the data based on Groups
SEQUENCE GENe CONN/Passive, Generates Sequence
SORTER Con/Active Sorts the data on spcific key
SQL trans A/P Connected

Static Cache
Dynamic Cache
Static cache remains same during the session run. Dynamic cache changes during
session run.
We can use other than relational operators like ,= &=.
We can use only = operator with dynamic cache.
We can handle multiple matches in static cache.
We cannot multiple matches in dynamic cache.
Static can be used to relational and flat file lookup types. Dynamic cache can
be used to only relational lookup types.
Static cache can be used to both unconnected and connected lookup transformation.
Dynamic cache can be used to only connetced lookups.

Connected lookup: Receives source data, performs a lookup, and returns data to the
pipeline.
Unconnected lookup: Is not connected to the source or target, and is called by a
transformation in the pipeline by using the :LKP expression. It returns only one
column value to the calling transformation

1.
Nth Salary_____________________
With CTE
as
(
select *, dense_rank() over (Order by salary desc) as RN
)

select * from CTE


where RN= 5
_____________________________________________________________________________
2.New Table from existing table

select * into Test1 from test;

select * into Test2 from test ----Only structure copy


where 1=2;
______________________________________________________________________________
3. mapping Adult and Child

With Adult
as
(
select *, row_number() over (order by age desc) as RN from details
where category="adult"
)

Child
as
(
select *, row_number()over (order by age desc) as RN from details
where category ="child"
)
select a.name as Adult, c.name as Child
from adult a
left join child c
on a.RN=c.RN

________________________________________________

Sequence Puzzle

with Cte
as
(
select *,row_number() over (partition by name order by sequence) as RN,
sequence - row_number() over (partition by name order by sequence) as RNG from
sequenceData
)
cte1
as
(
select name, RNG,min(squence) as Minv, max(sequence) as MaxV
from cte
group by Name , RNG
)
select name, minv,maxv from CTE1

___________________________________________________________________________________
____

Cureent Year vs Prev Yers

select s.student_name, s.year, s.total_marks as current_marks, D.total_marks as


Prev_marks
from studentT S
Inner Join StudentT D
on s.year -1 =D.year
and S.stident_name =D.student_name
were s.total_marks>=d.total_marks

class Solution(object):
def mergeAlternately(self, word1, word2):
"""
:type word1: str
:type word2: str
:rtype: str
"""
result = []
i = 0
while i < len(word1) or i < len(word2):
if i < len(word1):
result.append(word1[i])
if i < len(word2):
result.append(word2[i])
i += 1
return ''.join(result)

Select all employee detail with First name "Vikas","Ashish", and "Nikhil".

select * from employee where firstname IN (Vikas, Ashish, Nikhiil)

Select first name from "EmployeeDetail" table after removing white spaces from
right side

select rtrim(firstname)as Firstname from Emp

#Display first name and Gender as M/F.(if male then M, if Female then F)

select firstname, case when gender= male then 'M'


case when gender =female then 'F' End as Gender
from empl

#Get employee details from "EmployeeDetail" table whose Salary greater than
600000

select firstName, salary from employee


where salry >600000

#Select second highest salary from "EmployeeDetail" table.

select max(salary) from emp where salary<(select max(salary) from emp where salary)

select salary from employee


group by salary
order by salary desc
limt 1,1

select salary from emp


order by salary desc
limt 2 offset 1

#Write the query to get the department and department wise total(sum) salary
from "EmployeeDetail" table

select departmet , sum(salary) as TotalSalary from emp


group by department

####Write down the query to fetch Project name assign to more than one Employee
select Projectname, count(*) Noempp from project
group by Projectname
having count(*)>1

###Get employee name, project name order by firstname from "EmployeeDetail" and
"ProjectDetail" for those employee which have assigned project already.

select firstname, projectname from


EmployeeDetail E
InnerJoin ProjectDetail P
On E.EmpId=P.Empid
order by firstname

Data Validations
- Metadata validation
- Data integrity check
- Rocord count check
- Data transformation check
- Data validation check
- Initial load and incremental loads

Complex Queries in SQL ( Oracle )

These questions are the most frequently asked in interviews.

To fetch ALTERNATE records from a table. (EVEN NUMBERED)


select * from emp where rowid in (select decode(mod(rownum,2),0,rowid, null) from
emp);
To select ALTERNATE records from a table. (ODD NUMBERED)
select * from emp where rowid in (select decode(mod(rownum,2),0,null ,rowid) from
emp);

Find the 3rd MAX salary in the emp table.


select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp e2
where e1.sal <= e2.sal);

Find the 3rd MIN salary in the emp table.


select distinct sal from emp e1 where 3 = (select count(distinct sal) from emp
e2where e1.sal >= e2.sal);

Select FIRST n records from a table.


select * from emp where rownum <= &n;

Select LAST n records from a table


select * from emp minus select * from emp where rownum <= (select count(*) - &n
from emp);

List dept no., Dept name for all the departments in which there are no employees in
the department.
select * from dept where deptno not in (select deptno from emp);

alternate solution: select * from dept a where not exists (select * from emp b
where a.deptno = b.deptno);
altertnate solution: select empno,ename,b.deptno,dname from emp a, dept b where
a.deptno(+) = b.deptno and empno is null;

How to get 3 Max salaries ?


select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b
where a.sal <= b.sal) order by a.sal desc;

How to get 3 Min salaries ?


select distinct sal from emp a where 3 >= (select count(distinct sal) from emp b
where a.sal >= b.sal);

How to get nth max salaries ?


select distinct hiredate from emp a where &n = (select count(distinct sal) from emp
b where a.sal >= b.sal);

Select DISTINCT RECORDS from emp table.


select * from emp a where rowid = (select max(rowid) from emp b where
a.empno=b.empno);

How to delete duplicate rows in a table?


delete from emp a where rowid != (select max(rowid) from emp b where
a.empno=b.empno);

Count of number of employees in department wise.


select count(EMPNO), b.deptno, dname from emp a, dept b where a.deptno(+)=b.deptno
group by b.deptno,dname;

Suppose there is annual salary information provided by emp table. How to fetch
monthly salary of each and every employee?select ename,sal/12 as monthlysal from
emp;

Select all record from emp table where deptno =10 or 40.select * from emp where
deptno=30 or deptno=10;

Select all record from emp table where deptno=30 and sal>1500.select * from emp
where deptno=30 and sal>1500;

Select all record from emp where job not in SALESMAN or CLERK.select * from emp
where job not in ('SALESMAN','CLERK');

Select all record from emp where ename in 'BLAKE','SCOTT','KING'and'FORD'.select *


from emp where ename in('JONES','BLAKE','SCOTT','KING','FORD');

Select all records where ename starts with ‘S’ and its lenth is 6 char.select *
from emp where ename like'S____';

Select all records where ename may be any no of character but it should end with
‘R’.select * from emp where ename like'%R';

Count MGR and their salary in emp table.select count(MGR),count(sal) from emp;

In emp table add comm+sal as total sal .select ename,(sal+nvl(comm,0)) as totalsal


from emp;

Select any salary <3000 from emp table. select * from emp where sal> any(select sal
from emp where sal<3000);

Select all salary <3000 from emp table. select * from emp where sal> all(select sal
from emp where sal<3000);

Select all the employee group by deptno and sal in descending order.select
ename,deptno,sal from emp order by deptno,sal desc;

How can I create an empty table emp1 with same structure as emp?Create table emp1
as select * from emp where 1=2;

How to retrive record where sal between 1000 to 2000?

Select * from emp where sal>=1000 And sal<2000

Select all records where dept no of both emp and dept table matches.
select * from emp where exists(select * from dept where emp.deptno=dept.deptno)
If there are two tables emp1 and emp2, and both have common record. How can I fetch
all the recods but common records only once?

(Select * from emp) Union (Select * from emp1)


How to fetch only common records from two tables emp and emp1?
(Select * from emp) Intersect (Select * from emp1)
How can I retrive all records of emp1 those should not present in emp2?
(Select * from emp) Minus (Select * from emp1)
Count the totalsa deptno wise where more than 2 employees exist.
SELECT deptno, sum(sal) As totalsal
FROM emp
GROUP BY deptno
HAVING COUNT(empno) > 2

You might also like