SQL - Examples - FB Interview
SQL - Examples - FB Interview
Answer : There are multiple way to find this i.e., either you can use either set
operator or outer join. Which to use depends on the requirement.
Option 1)
select customer_id from a
minus
select customer_id from b
option 2)
select a.*
from a
, b
where a.customer_id=b.customer_id(+)
and b.customer_id is null;
-----------------------------------------------------------------------------------
----------------
-- Below example is how to avoid group by and still use aggregate functions with
other columns :
select empno , ename , deptno , sal , sum(sal) over( order by null ) -- is same as
select sum(sal) over() from emp
from emp
10 9300
10 9300
20 20175
30 29575
-- cumulative salary total, row by row, by department
SUM(sal) OVER (PARTITION BY department_id ORDER BY last_name, first_name)
10 3000 3000
10 5000 8000
10 1300 9300
20 1100 1100
20 3000 4100
20 2975 7075
10 60000 120000
10 60000 120000
10 70000 190000
20 65000 65000
-----------------------------------
unpivot :
with sale_stats
( id, fiscal_year, product_a, product_b, product_c )
as (select 1, 2017, to_number(NULL) , 200 , 300 from
dual union all
select 2, 2018, 150 , to_number(NULL), 250 from
dual union all
select 3, 2019, 150 , 220 , to_number(NULL) from
dual
)
select * from sale_stats
unpivot
( quantity -- which is a column that represents the unpivoted values from the
product_a, product_b, and product_c columns.
for product_code -- this is a column which represents column name
in ( product_a as 'A',
-- product_b AS 'B',
product_c AS 'C'
)
)
order by product_code
;
------------------------------------------------------------
select *
from ( select deptno,job from emp )
pivot
( count(*) -- this count` wil be applied for every job at deptno and other column
if in select clause.
for deptno in (10 dept_10 -- this deptno row value will be converted to column
header ( row got converted to column )
,20
,30)
)
job dept_10 20 30
------ ---- -- --
CLERK 1 2 1
SALESMAN 0 0 4
PRESIDENT 1 0 0
MANAGER 1 1 1
ANALYST 0 2 0
with
property(prop_name, prop_type, prop_val, environment) as (
select 'BANKING', 'A', 'true' , 'DEV' from dual union all
select 'BANKING', 'A', 'false' , 'DEV' from dual union all
select 'BANKING', 'A', 'true' , 'PROD' from dual union all
select 'BURGER' , 'B', 'true' , 'DEV' from dual union all
select 'BURGER' , 'C', 'true' , 'DEV' from dual
)
select *
from property
pivot (-- max(prop_val)
listagg(prop_val, ',') within group (order by null)
for environment in ('DEV' dev, 'TEST' test, 'STAGE' stage, 'UAT' uat,
'PROD' prod)) -- row to columns
;
---------------------------------------------------------
---------------------------------------------------------------------------------
DEPTNO , ENAME
10 CLARK,KING,MILLER
20 ADAMS,FORD,JONES,SCOTT,SMITH
30 ALLEN,BLAKE,JAMES,MARTIN,TURNER,WARD
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 4
7900 30 950 1
7654 30 1250 2
7521 30 1250 2
7844 30 1500 3
---------------------------------------------------------------------------------
The FIRST and LAST functions can be used to return the first or last value from an
ordered sequence,
---------------------------------------------------------------------------------
Will list employee rank for salary = 1000 and bonus = 500
example :
Rank
--------------
4
---------------------------------------------------------------------------------
SELECT level
FROM dual
CONNECT BY level <= 10;
1
2
3
---------------------------------------------------------------------------------
SELECT val
FROM rownum_order_test
ORDER BY val DESC
FETCH FIRST 5 ROWS ONLY; -- Row Limiting Clause (12c onward)
VAL
----------
10
10
9
9
8
5 rows selected.
---------------------------------------------------------------------------------
generatingeven and odd numbers
---------------------------------------------------------------------------------
select level
, case when mod(level,2) <> 0 then 'YES' else 'NO' end as odd_number
, case when mod(level,2) = 0 then 'YES' else 'NO' end as even_number
, sum(level) over()
from dual
connect by level <=10