12_May_Interview Question SQL Group by, Case When, Partition By
12_May_Interview Question SQL Group by, Case When, Partition By
case when
subqueries
decode
lead lag
constraints all (pk,fk,unique,not,null,default,check)
operators (union , union all, minus/except, intersect)
JOINS
==============================
You have a table named orders with columns:
customer_id, order_amount, and order_date.
c101 3000 2022-01-01
c101 4000 2022-01-02
=================
9_MAY_ASSIGNMENT _solve below query :
1>
cid cname city
c101 abc Hyd
c102 cde Hyd
c101 abc Hyd
Indore
=====================================
==============================
--eid, ename, salary, did,dname, e_address...
101, abc, 2000, d01, PHYSICS, St01 HYD
sel count(1), did from emp group by did;
sel ename,count(1)
from emp
group by ename
having count(1) =1;
abc 2
CDE 1
efg 2
sel ename
from table
group by ename
having count(ename) =1;
op
ename
abc
========================
Q> How to search duplicates id?
sel id, count(1)
from table
group by id
having count(1)>1
emp_name,dept_name
--------------------
insert ALL
into EMP_63 (eid, ename, salary, did,address) values(103,'John',70000,'d101','40,
HYD')
into EMP_63 (eid, ename, salary, did,address) values(103,'John',70000,'d101','40,
HYD')
into EMP_63 (eid, ename, salary, did,address) values(103,'John',70000,'d101','40,
HYD')
into EMP_63 (eid, ename, salary, did,address) values(103,'John',70000,'d101','40,
HYD')
select * from dual;
select unique count(*) from emp_63 ;
select count(unique eid) from emp_63 ;
count(1)
count(*)
select count(distinct eid) from emp_63;
id name sal
101 ....
101 .......
101 ......
102
dups
select eid, count(*) from emp_63
group by eid
having count(*) > 1;
====================================================
Q>fetch the department names in which only one employee is working .
=============================
select t.* from
(
select u.*,
count(1) over(partition by items order by items) as cnt
from user_66 u
) t where t.cnt =1;
select u.*,
count(1) over(partition by items order by items) as cnt
from user_66 u ;
=======================================
select * from emp_63 where eid in (select eid from emp_63 group by eid having
count(1) =1);
IN
AND
OR
EXIST
BETWEEN
LIKE
select eid, count(*) from emp_63
group by eid
having count(*) > 1;
having count(*) == 1
desc EMP_63;
commit;
sel cid,sum(order_amt)
from table
group by cid
having sum(order_amt)>1000
================================
Product Categories:
Consider a table named products with columns:
product_id, category_id, and price.
Write a query to find the average price for each product category.
Log Analysis:
You have a table named log_data with columns:
user_id, action_type, and timestamp.
u01, login, 20241217 11.15:000
u01 changed pwd
u01 loogeout
uid count_of_action
u02 5
u01 3
u03 1
grouped by user_id,
and display the results in descending order of action count.
Movie Ratings:
Given a table named movie_ratings with columns:
movie_id, user_id, and rating,movie_title.
m01,u01,4, 'Dr. Strange'
m01,u02,5, 'Dr. Strange'
m01,u03,2, 'Dr. Strange'
=================================
show all data of those students who got maximum marks classwise....
STUDENTS
student_id, marks,student_name,classID
s101,90,ABC,C101
s102,80,HBC,C101
s103,70,HBC,C101
C102
sel classid,max(marks)
from student
group by classid
c101 90
c102 80
C101 90
C102 80
C103 70
======================
select country,max(sal)
from employee
group by country;
Q> what is second max salary or 3rd max salary...nth max salary country wise
select e.*, row_number() over (partition by e.country order by e.sal desc) rownum1,
rank() over(partition by country order by e.sal desc) rank,
dense_rank() over(partition by country order by e.sal desc) dense_rank
from employee e;
) m
where m.dense_rank =&n;
=================================
------------------------------------
Q> the detail(name_) of those employees who earn max sal in their country
select m.*
from(
select e.*,max(sal) over(partition by e.country) max_sal
from employee e
) m
where m.max_sal =m.sal;
-----------------
select e.* from
(
select e1.*,avg(sal) over(partition by country) as avg
from employee e1 ) e
where e.sal > e.avg;
from T1
join T2
on t1.c1= t2.c1;
-------
sel * from table where id is not null;
T2
c1 c2
_ _
a 11
a 12
a 13
b 14
b 15
c
g 20
----------------------
d 8 _ _
_ _ g 20
left anti
right anti
semi
===========================
NULLS ll be treated uniquly and not common and ll not resulten of\
ineer join
inner join ll not give common op on NULL
left and right join ll treate them uniquly extras
==================
self
UPCOMING
=================
Abinitio
JOIN key
JOIN {}
lookup key
without key
gather (union, union all, intersect, minus) ==> no. column and corresponding data
type of columns should be same
FUSE (keeping together all first record ...) keep one to one records together in
one record
-----------------------
CASE
when <cond> then <res>
when <cond> then <res>
....
else <res>
end as flag
select e.*,
case
when e.sal<700000 then 'LOW'
when e.sal < 900000 then 'avg'
else 'high'
end as status
from employee e;
% ==> reminder
14%2== 1
==========================
DEPT Gender, name,...
IT , F
IT , M
IT, M
CS, M
CS, M
CS,M
CS, F
o/p
IT F 1
IT M 2
CS F 1
Cs M 3
o/p
Dept male_count, female_count
IT 2 1
CS 1 3
sel dept,
count(case when gender='M' then 1 ) as male_count,
count(case when gender='F' then 1 ) female_count
from dept
group by dept ;
=================================
select DEPT, (select count(1) from table T2 where T1.dept_id=T2.dept_id
gender='M' ) as MALE_count,
(select count(1) from table T2 where T1.dept_id=T2.dept_id
gender='F' ) as FEMALE_count
from table T1
group by DEPT
90,80,70
sel student_id,marks,
where marks=(
sel max(marks),cid
from table
group by cid )
table
A B
1 0
1 0
1 0
==============
T1
A
2
3
2
o/p
3
2
3
2
T1
A
2
3
2
T2
A B
2 X
3 Y
o/p
Y
X
Y
X
UPDATE T1
SET A = T2.B
FROM T1
JOIN T2 ON T1.A = T2.A;
UPDATE T1
SET A = CASE
WHEN T1.A = 2 THEN (SELECT DISTINCT B FROM T2 WHERE A = 2)
WHEN T1.A = 3 THEN (SELECT DISTINCT B FROM T2 WHERE A = 3)
ELSE T1.A
END;
M=>F
F=>M
----------------------------
how to remove dups
============
depid gender
IT M
IT F
IT F
CS M
CS F
o/p
deptid male_count female_count
IT 1 2
CS 1 1
=======================
I'd status
101 account
103 account
104 bad
102 nic
107 account
100 iso
105 bad
So we need status column like account >bad>nic>iso in that order
sel * from table
order by
case
when status='account' then 1
when status='bad' then 2
when status='nic' then 3
else 4
end;
================
case when
1> cond based column status
2> cond based counting of male , female
3> cond based ordering priority
===========================
id fname lname
1 sai null
1 null ram
in above table they need output in below one line format using join
1 Sai ram
/*================================================*/
/*delete duplicates*/
select e.*,rowid,rownum from emp e;
delete from emp91 where rowid not in (select max(rowid) from emp91 group by id);
========================
windows function
row_number
rank
dense_rank
dept wise 5th highest salary
select e.*,
row_number() over (partition by e.country order by e.sal desc) rownum1,
rank() over (partition by e.country order by e.sal desc) rank,
dense_rank() over (partition by e.country order by e.sal desc) dense_rank,
rowid
from employee e
leg
lead
=============
delete from table where rowid not in
( sel max(rowid) from table group by id)
eid rowid
1 nnnnA
1 nhnnB
1 nnnnC
---------------------------
select e.country, e.sal, lag(sal,1) over( partition by country order by sal desc)
lag
from emp451 e;
----------------------------------
find out nth highest
================
how to delete nth
delete from (
select e.*,
dense_rank() over (partition by e.country order by e.sal desc) dense_rank
from emp451 e) m
where m.dense_rank=&n;
================
how to delete dups on ranks== rank basis dups removal
--+++++++++++++++++STORED PROC++++++++++++++++++++=
call greet_person('Mina');
execute greet_person('John');
execute greet_person('Mina');
--========================================
--update order set ord_description='Delivered' where agent_code='A007';
--2024-11-06
select * from orders1;
====================
in oracle 11g we use, rownum, rowid, level for limiting and offsetting
capgemini
c
a
p
-----------------
Real time ex
Seq Trigger on insert
view and materialized view
=================================
GROUp BY
CASE WHEN
SUBQUERY
Operators
===============================
docs--->
index
view mv
performance tuning..
===========================
join scd-2
SK
oracle seq +trigger for sk
=======================================