Sei sulla pagina 1di 86

SQL Queries Interview Questions - Oracle Part 1

As a database developer, writing SQL queries, PLSQL code is part of daily life. Having a good
knowledge on SQL is really important. Here i am posting some practical eampleson SQL queries.
!o solve t"ese interview questions on SQL queries you "ave to create t"e products, sales tables in your
oracle database. !"e #$reate !able#, #%nsert# statements are provided below.
$&'A!' !A(L' P&)*+$!S
,
P&)*+$!-%* %.!'/'&,
P&)*+$!-.A0' 1A&$HA&2,345
56
$&'A!' !A(L' SAL'S
,
SAL'-%* %.!'/'&,
P&)*+$!-%* %.!'/'&,
7'A& %.!'/'&,
Quantity %.!'/'&,
P&%$' %.!'/'&
56
INSERT INTO P&)*+$!S 1AL+'S , 844, 9.okia956
%.S'&! %.!) P&)*+$!S 1AL+'S , 244, 9%P"one956
%.S'&! %.!) P&)*+$!S 1AL+'S , 344, 9Samsung956
%.S'&! %.!) P&)*+$!S 1AL+'S , :44, 9L/956
%.S'&! %.!) SAL'S 1AL+'S , 8, 844, 2484, 2;, ;44456
%.S'&! %.!) SAL'S 1AL+'S , 2, 844, 2488, 8<, ;44456
%.S'&! %.!) SAL'S 1AL+'S , 3, 844, 2482, =, ;44456
%.S'&! %.!) SAL'S 1AL+'S , :, 244, 2484, 84, >44456
%.S'&! %.!) SAL'S 1AL+'S , ;, 244, 2488, 8;, >44456
%.S'&! %.!) SAL'S 1AL+'S , <, 244, 2482, 24, >44456
%.S'&! %.!) SAL'S 1AL+'S , ?, 344, 2484, 24, ?44456
%.S'&! %.!) SAL'S 1AL+'S , =, 344, 2488, 8=, ?44456
%.S'&! %.!) SAL'S 1AL+'S , >, 344, 2482, 24, ?44456
$)00%!6
!"e products table contains t"e below data.
S'L'$! @ A&)0 P&)*+$!S6
P&)*+$!-%* P&)*+$!-.A0'
BBBBBBBBBBBBBBBBBBBBBBB
844 .okia
244 %P"one
344 Samsung
!"e sales table contains t"e following data.
S'L'$! @ A&)0 SAL'S6
SAL'-%* P&)*+$!-%* 7'A& Q+A.!%!7 P&%$'
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
8 844 2484 2; ;444
2 844 2488 8< ;444
3 844 2482 = ;444
: 244 2484 84 >444
; 244 2488 8; >444
< 244 2482 24 >444
? 344 2484 24 ?444
= 300 2011 8= ?444
> 344 2482 24 ?444
Here Quantity is t"e number of products sold in eac" year. Price is t"e sale price of eac" product.
% "ope you "ave created t"e tables in your oracle database. .ow try to solve t"e belowSQL queries.
1. Crite a SQL query to find t"e products w"ic" "ave continuous increase in sales every yearD
Solution:
Here E%p"oneF is t"e only product w"ose sales are increasing every year.
STEP1: Airst we will get t"e previous year sales for eac" product. !"e SQL query to do t"is is
S'L'$! P.P&)*+$!-.A0',
S.7'A&,
S.Q+A.!%!7,
L'A*,S.Q+A.!%!7,8,45 )1'& ,
PA&!%!%). (7 P.P&)*+$!-%*
)&*'& (7 S.7'A& *'S$
5 Q+A.-P&'1-7'A&
A&)0 P&)*+$!S P,
SAL'S S
CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*6
P&)*+$!-.A0' 7'A& Q+A.!%!7 Q+A.-P&'1-7'A&
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
.okia 2482 = 8<
.okia 2488 8< 2;
.okia 2484 2; 4
%P"one 2482 24 8;
%P"one 2488 8; 84
%P"one 2484 84 4
Samsung 2482 24 8=
Samsung 2488 8= 24
Samsung 2484 24 4
Here t"e lead analytic function will get t"e quantity of a product in its previous year.
STEP2: Ce will find t"e difference between t"e quantities of a product wit" its previous yearHs quantity.
%f t"is difference is greater t"an or equal to Iero for all t"e rows, t"en t"e product is a constantly
increasing in sales. !"e final query to get t"e required result is
S'L'$! P&)*+$!-.A0'
A&)0
,
S'L'$! P.P&)*+$!-.A0',
S.Q+A.!%!7 B
L'A*,S.Q+A.!%!7,8,45 )1'& ,
PA&!%!%). (7 P.P&)*+$!-%*
)&*'& (7 S.7'A& *'S$
5 Q+A.-*%AA
A&)0 P&)*+$!S P,
SAL'S S
CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*
5A
GROUP (7 P&)*+$!-.A0'
HA1%./ 0%.,Q+A.-*%AA5 JG 46
P&)*+$!-.A0'
BBBBBBBBBBBB
%P"one
2. Crite a SQL query to find t"e products w"ic" does not "ave sales at allD
Solution:
EL/F is t"e only product w"ic" does not "ave sales at all. !"is can be ac"ieved in t"ree ways.
Method1: +sing left outer Koin.
S'L'$! P.P&)*+$!-.A0'
A&)0 P&)*+$!S P
L'A! )+!'& L)%.
SAL'S S
). ,P.P&)*+$!-%* G S.P&)*+$!-%*56
CH'&' S.Q+A.!%!7 %S .+LL
P&)*+$!-.A0'
BBBBBBBBBBBB
L/
Method2: +sing t"e .)! %. operator.
S'L'$! P.P&)*+$!-.A0'
A&)0 P&)*+$!S P
CH'&' P.P&)*+$!-%* .)! %.
,S'L'$! *%S!%.$! P&)*+$!-%* A&)0 SAL'S56
P&)*+$!-.A0'
BBBBBBBBBBBB
L/
Method3: +sing t"e .)! 'M%S!S operator.
S'L'$! P.P&)*+$!-.A0'
A&)0 P&)*+$!S P
CH'&' .)! 'M%S!S
,S'L'$! 8 A&)0 SAL'S S CH'&' S.P&)*+$!-%* G P.P&)*+$!-%*56
P&)*+$!-.A0'
BBBBBBBBBBBB
L/
3. Crite a SQL query to find t"e products w"ose sales decreased in 2482 compared to 2488D
Solution:
Here .okia is t"e only product w"ose sales decreased in year 2482 w"en compared wit"t"e sales in
t"e year 2488. !"e SQL query to get t"e required output is
S'L'$! P.P&)*+$!-.A0'
A&)0 P&)*+$!S P,
SAL'S S-2482,
SAL'S S-2488
CH'&' P.P&)*+$!-%* G S-2482.P&)*+$!-%*
A.* S-2482.7'A& G 2482
A.* S-2488.7'A& G 2488
A.* S-2482.P&)*+$!-%* G S-2488.P&)*+$!-%*
A.* S-2482.Q+A.!%!7 N S-2488.Q+A.!%!76
P&)*+$!-.A0'
BBBBBBBBBBBB
.okia
. Crite a query to select t"e top product sold in eac" yearD
Solution:
.okia is t"e top product sold in t"e year 2484. Similarly, Samsung in 2488 and %P"one, Samsung in
2482. !"e query for t"is is
S'L'$! P&)*+$!-.A0',
7'A&
A&)0
,
S'L'$! P.P&)*+$!-.A0',
S.7'A&,
&A.O,5 )1'& ,
PA&!%!%). (7 S.7'A&
)&*'& (7 S.Q+A.!%!7 *'S$
5 &.O
A&)0 P&)*+$!S P,
SAL'S S
CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*
5 A
CH'&' &.O G 86
P&)*+$!-.A0' 7'A&
BBBBBBBBBBBBBBBBBBBB
.okia 2484
Samsung 2488
%P"one 2482
Samsung 2482
!. Crite a query to find t"e total sales of eac" product.D
Solution:
!"is is a simple query. 7ou Kust need to group by t"e data on P&)*+$!-.A0' and t"en find t"e sum
of sales.
S'L'$! P.P&)*+$!-.A0',
.1L, S+0, S.Q+A.!%!7@S.P&%$' 5, 45 !)!AL-SAL'S
A&)0 P&)*+$!S P
L'A! )+!'& L)%.
SAL'S S
). ,P.P&)*+$!-%* G S.P&)*+$!-%*5
/&)+P (7 P.P&)*+$!-.A0'6
P&)*+$!-.A0' !)!AL-SAL'S
BBBBBBBBBBBBBBBBBBBBBBBBBBB
L/ 4
%P"one :4;444
Samsung :4<444
.okia 2:;444
SQL Queries Interview Questions - Oracle Part 2
!"is is continuation to my previous post, SQL Queries %nterview Questions B )racle Part 8, C"ere i
"ave used P&)*+$!S and SAL'S tables as an eample. Here also i am using t"e same tables. So,
Kust take a look at t"e tables by going t"roug" t"at link and it will be easy for you to understand
t"e questions mentioned "ere.
Solve t"e below eamples by writing SQL queries.
1. Crite a query to find t"e products w"ose quantity sold in a year s"ould be greater t"an t"e average
quantity sold across all t"e yearsD
Solution:
!"is can be solved wit" t"e "elp of correlated query. !"e SQL query for t"is is
S'L'$! P.P&)*+$!-.A0',
S.7'A&,
S.Q+A.!%!7
A&)0 P&)*+$!S P,
SAL'S S
CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*
A.* S.Q+A.!%!7 J
,S'L'$! A1/,Q+A.!%!75
A&)0 SAL'S S8
CH'&' S8.P&)*+$!-%* G S.P&)*+$!-%*
56
P&)*+$!-.A0' 7'A& Q+A.!%!7
BBBBBBBBBBBBBBBBBBBBBBBBBB
.okia 2484 2;
%P"one 2482 24
Samsung 2482 24
Samsung 2484 24
2. Crite a query to compare t"e products sales of #%P"one# and #Samsung# in eac" yearD !"e output
s"ould look like as
7'A& %PH).'-Q+A.! SA0-Q+A.! %PH).'-P&%$' SA0-P&%$'
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
2484 84 24 >444 ?444
2488 8; 8= >444 ?444
2482 24 24 >444 ?444
Solution:
(y using selfBKoin SQL query we can get t"e required result. !"e required SQL query is
S'L'$! S-%.7'A&,
S-%.Q+A.!%!7 %PH).'-Q+A.!,
S-S.Q+A.!%!7 SA0-Q+A.!,
S-%.P&%$' %PH).'-P&%$',
S-S.P&%$' SA0-P&%$'
A&)0 P&)*+$!S P-%,
SAL'S S-%,
P&)*+$!S P-S,
SAL'S S-S
CH'&' P-%.P&)*+$!-%* G S-%.P&)*+$!-%*
A.* P-S.P&)*+$!-%* G S-S.P&)*+$!-%*
A.* P-%.P&)*+$!-.A0' G 9%P"one9
A.* P-S.P&)*+$!-.A0' G 9Samsung9
A.* S-%.7'A& G S-S.7'A&
3. Crite a query to find t"e ratios of t"e sales of a productD
Solution:
!"e ratio of a product is calculated as t"e total sales price in a particular year divide by t"e total
sales price across all years. )racle provides &A!%)-!)-&'P)&! analytical function for finding t"e
ratios. !"e SQL query is
S'L'$! P.P&)*+$!-.A0',
S.7'A&,
&A!%)-!)-&'P)&!,S.Q+A.!%!7@S.P&%$'5
)1'&,PA&!%!%). (7 P.P&)*+$!-.A0' 5 SAL'S-&A!%)
A&)0 P&)*+$!S P,
SAL'S S
CH'&' ,P.P&)*+$!-%* G S.P&)*+$!-%*56
P&)*+$!-.A0' 7'A& &A!%)
BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
%P"one 2488 4.333333333
%P"one 2482 4.:::::::::
%P"one 2484 4.222222222
.okia 2482 4.8<32<;34<
.okia 2488 4.32<;34<82
.okia 2484 4.;8424:4=2
Samsung 2484 4.3::=2?;=<
Samsung 2482 4.3::=2?;=<
Samsung 2488 4.3843::=2=
. %n t"e SAL'S table quantity of eac" product is stored in rows for every year. .ow write a query to
transpose t"e quantity for eac" product and display it in columnsD !"e output s"ould look like as
P&)*+$!-.A0' Q+A.-2484 Q+A.-2488 Q+A.-2482
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
%P"one 84 8; 24
Samsung 24 8= 24
.okia 2; 8< =
Solution:
)racle 88g provides a pivot function to transpose t"e row data into column data. !"e SQL query for t"is
is
S'L'$! @ A&)0
,
S'L'$! P.P&)*+$!-.A0',
S.Q+A.!%!7,
S.7'A&
A&)0 P&)*+$!S P,
SAL'S S
CH'&' ,P.P&)*+$!-%* G S.P&)*+$!-%*5
5A
P%1)! , 0AM,Q+A.!%!75 AS Q+A. A)& ,7'A&5 %. ,2484,2488,2482556
%f you are not running oracle 88g database, t"en use t"e below query for transposing t"e row data into
column data.
S'L'$! P.P&)*+$!-.A0',
0AM,*'$)*',S.7'A&,2484, S.Q+A.!%!755 Q+A.-2484,
0AM,*'$)*',S.7'A&,2488, S.Q+A.!%!755 Q+A.-2488,
0AM,*'$)*',S.7'A&,2482, S.Q+A.!%!755 Q+A.-2482
A&)0 P&)*+$!S P,
SAL'S S
CH'&' ,P.P&)*+$!-%* G S.P&)*+$!-%*5
/&)+P (7 P.P&)*+$!-.A0'6
!. Crite a query to find t"e number of products sold in eac" yearD
SolutionP
!o get t"is result we "ave to group by on year and t"e find t"e count. !"e SQL query for t"is question is
S'L'$! 7'A&,
$)+.!,85 .+0-P&)*+$!S
A&)0 SAL'S
/&)+P (7 7'A&6
7'A& .+0-P&)*+$!S
BBBBBBBBBBBBBBBBBB
2484 3
2488 3
2482 3
SQL Queries Interview Questions - Oracle Part 3
Here % am providing )racle SQL Query %nterview Questions. %f you find any bugs in t"e queries, Please
do comment. So, t"at i will rectify t"em.
8. Crite a query to generate sequence numbers from 8 to t"e specified number .D
SolutionP
SELECT L'1'L FROM DUAL CONNECT (7 L'1'LNGQ.6
2. Crite a query to display only friday dates from Lan, 2444 to till nowD
SolutionP
S'L'$! $-*A!',
!)-$HA&,$-*A!',9*795
A&)0
,
S'L'$! !)-*A!',948BLA.B24449,9**B0).B777795RL'1'LB8 $-*A!'
A&)0 *+AL
$)..'$! (7 L'1'L NG
,S7S*A!' B !)-*A!',948BLA.B24449,9**B0).B777795R85
5
CH'&' !)-$HA&,$-*A!',9*795 G 9A&%96
3. Crite a query to duplicate eac" row based on t"e value in t"e repeat columnD !"e input table data
looks like as below
Products, &epeat
BBBBBBBBBBBBBBBB
A, 3
(, ;
$, 2
.ow in t"e output data, t"e product A s"ould be repeated 3 times, ( s"ould be repeated ; times and $
s"ould be repeated 2 times. !"e output will look like as below
Products, &epeat
BBBBBBBBBBBBBBBB
A, 3
A, 3
A, 3
(, ;
(, ;
(, ;
(, ;
(, ;
$, 2
$, 2
SolutionP
S'L'$! P&)*+$!S,
&'P'A!
A&)0 !,
, S'L'$! L'1'L L A&)0 *+AL
$)..'$! (7 L'1'L NG ,S'L'$! 0AM,&'P'A!5 A&)0 !5
5 A
CH'&' !.&'P'A! JG A.L
)&*'& (7 !.P&)*+$!S6
:. Crite a query to display eac" letter of t"e word #S0%L'# in a separate rowD
S
0
%
L
'
SolutionP
S'L'$! S+(S!&,9S0%L'9,L'1'L,85 A
A&)0 *+AL
$)..'$! (7 L'1'L NGL'./!H,9S0%L'956
;. $onvert t"e string #S0%L'# to Ascii valuesD !"e output s"ould look like as =3,??,?3,?<,<>. C"ere =3
is t"e ascii value of S and so on.
!"e AS$%% function will give ascii value for only one c"aracter. %f you pass a string to t"e ascii function, it
will give t"e ascii value of first letter in t"e string. Here i am providing two solutions to get t"e ascii
values of string.
Solution8P
S'L'$! S+(S!&,*+0P,9S0%L'95,8;5
A&)0 *+AL6
Solution2P
S'L'$! C0-$).$A!,A5
A&)0
,
S'L'$! AS$%%,S+(S!&,9S0%L'9,L'1'L,855 A
A&)0 *+AL
$)..'$! (7 L'1'L NGL'./!H,9S0%L'95
56
SQL Queries Interview Questions - Oracle Part
8. $onsider t"e following friends table as t"e source
.ame, Ariend-.ame
BBBBBBBBBBBBBBBBB
sam, ram
sam, vamsi
vamsi, ram
vamsi, K"on
ram, viKay
ram, anand
Here ram and vamsi are friends of sam6 ram and K"on are friends of vamsi and so on. .ow write a
query to find friends of friends of sam. Aor sam6 ram,K"on,viKay and anand are friends of friends. !"e
output s"ould look as
.ame, Ariend-of-Airend
BBBBBBBBBBBBBBBBBBBBBB
sam, ram
sam, K"on
sam, viKay
sam, anand
SolutionP
S'L'$! f8.name,
f2.friend-name as friend-of-friend
A&)0 friends f8,
friends f2
CH'&' f8.name G 9sam9
A.* f8.friend-name G f2.name6
2. !"is is an etension to t"e problem 8. %n t"e output, you can see ram is displayed as friends of
friends. !"is is because, ram is mutual friend of sam and vamsi. .ow etend t"e above query to
eclude mutual friends. !"e outuput s"ould look as
.ame, Ariend-of-Ariend
BBBBBBBBBBBBBBBBBBBBBB
sam, K"on
sam, viKay
sam, anand
SolutionP
S'L'$! f8.name,
f2.friend-name as friend-of-friend
A&)0 friends f8,
friends f2
CH'&' f8.name G 9sam9
A.* f8.friend-name G f2.name
A.* .)! EXISTS
,S'L'$! 8 A&)0 friends f3
CH'&' f3.name G f8.name
A.* f3.friend-name G f2.friend-name56
3. Crite a query to get t"e top ; products based on t"e quantity sold wit"out using t"e row-number
analytical functionD !"e source data looks as
Products, quantity-sold, year
BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
A, 244, 244>
(, 8;;, 244>
$, :;;, 244>
*, <24, 244>
', 83;, 244>
A, 3>4, 244>
/, >>>, 2484
H, =84, 2484
%, >84, 2484
L, 84>, 2484
L, 2<4, 2484
0, ;=4, 2484
SolutionP
S'L'$! products,
quantity-sold,
year
A&)0
,
S'L'$! products,
quantity-sold,
year,
rownum r
from t
)&*'& (7 quantity-sold *'S$
5A
CH'&' r NG ;6
:. !"is is an etension to t"e problem 3. Crite a query to produce t"e same output using row-number
analytical functionD
SolutionP
S'L'$! products,
quantity-sold,
year
A&)0
,
S'L'$! products,
quantity-sold,
year,
row-number,5 )1'&,
)&*'& (7 quantity-sold *'S$5 r
from t
5A
CH'&' r NG ;6
;. !"is is an etension to t"e problem 3. write a query to get t"e top ; products in eac"year based on
t"e quantity soldD
SolutionP
S'L'$! products,
quantity-sold,
year
A&)0
,
S'L'$! products,
quantity-sold,
year,
row-number,5 )1'&,
PA&!%!%). (7 year
)&*'& (7 quantity-sold *'S$5 r
from t
5A
CH'&' r NG ;6
SQL Quer" Interview Questions - Part !
Crite SQL queries for t"e below interview questionsP
8. Load t"e below products table into t"e target table.
$&'A!' !A(L' P&)*+$!S
,
P&)*+$!-%* %.!'/'&,
P&)*+$!-.A0' 1A&$HA&2,345
56
%.S'&! %.!) P&)*+$!S 1AL+'S , 844, 9.okia956
%.S'&! %.!) P&)*+$!S 1AL+'S , 244, 9%P"one956
%.S'&! %.!) P&)*+$!S 1AL+'S , 344, 9Samsung956
%.S'&! %.!) P&)*+$!S 1AL+'S , :44, 9L/956
%.S'&! %.!) P&)*+$!S 1AL+'S , ;44, 9BlackBerry956
%.S'&! %.!) P&)*+$!S 1AL+'S , <44, 90otorola956
$)00%!6
S'L'$! @ A&)0 P&)*+$!S6
P&)*+$!-%* P&)*+$!-.A0'
BBBBBBBBBBBBBBBBBBBBBBB
844 .okia
244 %P"one
344 Samsung
:44 L/
;44 (lack(erry
<44 0otorola
!"e requirements for loading t"e target table areP
Select only 2 products randomly.
*o not select t"e products w"ic" are already loaded in t"e target table wit" in t"e last 34 days.
!arget table s"ould always contain t"e products loaded in 34 days. %t s"ouldnot contain t"e
products w"ic" are loaded prior to 34 days.
SolutionP
Airst we will create a target table. !"e target table will "ave an additional column %.S'&!-*A!' to
know w"en a product is loaded into t"e target table. !"e target
table structure is
$&'A!' !A(L' !/!-P&)*+$!S
,
P&)*+$!-%* %.!'/'&,
P&)*+$!-.A0' 1A&$HA&2,345,
%.S'&!-*A!' *A!'
56
!"e net step is to pick ; products randomly and t"en load into target table. C"ile selecting c"eck
w"et"er t"e products are t"ere in t"e
%.S'&! %.!) !/!-P&)*+$!S
S'L'$! P&)*+$!-%*,
P&)*+$!-.A0',
S7S*A!' %.S'&!-*A!'
A&)0
,
S'L'$! P&)*+$!-%*,
P&)*+$!-.A0'
A&)0 P&)*+$!S S
CH'&' .)! 'M%S!S ,
S'L'$! 8
A&)0 !/!-P&)*+$!S !
CH'&' !.P&)*+$!-%* G S.P&)*+$!-%*
5
)&*'& (7 *(0S-&A.*)0.1AL+' BBRan!" n#"$er %enera&!r in oracle.
5A
CH'&' &)C.+0 NG 26
!"e last step is to delete t"e products from t"e table w"ic" are loaded 34 days back.
*'L'!' A&)0 !/!-P&)*+$!S
CH'&' %.S'&!-*A!' N S7S*A!' B 346
2. Load t"e below $).!'.!S table into t"e target table.
$&'A!' !A(L' $).!'.!S
,
$).!'.!-%* %.!'/'&,
$).!'.!-!7P' 1A&$HA&2,345
56
%.S'&! %.!) $).!'.!S 1AL+'S ,8,90)1%'956
%.S'&! %.!) $).!'.!S 1AL+'S ,2,90)1%'956
%.S'&! %.!) $).!'.!S 1AL+'S ,3,9A+*%)956
%.S'&! %.!) $).!'.!S 1AL+'S ,:,9A+*%)956
%.S'&! %.!) $).!'.!S 1AL+'S ,;,90A/AS%.'956
%.S'&! %.!) $).!'.!S 1AL+'S ,<,90A/AS%.'956
$)00%!6
S'L'$! @ A&)0 $).!'.!S6
$).!'.!-%* $).!'.!-!7P'
BBBBBBBBBBBBBBBBBBBBBBB
8 0)1%'
2 0)1%'
3 A+*%)
: A+*%)
; 0A/AS%.'
< 0A/AS%.'
!"e requirements to load t"e target table areP
Load only one content type at a time into t"e target table.
!"e target table s"ould always contain only one contain type.
!"e loading of content types s"ould follow roundBrobin style. Airst 0)1%', second A+*%), !"ird
0A/AS%.' and again fourt" 0ovie.
SolutionP
Airst we will create a lookup table w"ere we mention t"e priorities for t"e content types. !"e
lookup table E$reate StatementF and data is s"own below.
$&'A!' !A(L' $).!'.!S-LOP
,
$).!'.!-!7P' 1A&$HA&2,345,
P&%)&%!7 %.!'/'&,
L)A*-ALA/ %.!'/'&
56
%.S'&! %.!) $).!'.!S-LOP 1AL+'S,90)1%'9,8,856
%.S'&! %.!) $).!'.!S-LOP 1AL+'S,9A+*%)9,2,456
%.S'&! %.!) $).!'.!S-LOP 1AL+'S,90A/AS%.'9,3,456
$)00%!6
S'L'$! @ A&)0 $).!'.!S-LOP6
$).!'.!-!7P' P&%)&%!7 L)A*-ALA/
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
0)1%' 8 8
AUDIO 2 4
0A/AS%.' 3 4
Here if L)A*-ALA/ is 8, t"en it indicates w"ic" content type needs to be loaded into t"e target table.
)nly one content type will "ave L)A*-ALA/ as 8. !"e ot"er content types will "ave L)A*-ALA/ as
4. !"e target table structure is same as t"e source tablestructure.
!"e second step is to truncate t"e target table before loading t"e data
!&+.$A!' !A(L' !/!-$).!'.!S6
!"e t"ird step is to c"oose t"e appropriate content type from t"e lookup table to load t"e source data
into t"e target table.
%.S'&! %.!) !/!-$).!'.!S
S'L'$! $).!'.!-%*,
$).!'.!-!7P'
A&)0 $).!'.!S
CH'&' $).!'.!-!7P' G ,S'L'$! $).!'.!-!7P' A&)0 $).!'.!S-LOP CH'&'
L)A*-ALA/G856
!"e last step is to update t"e L)A*-ALA/ of t"e Lookup table.
+P*A!' $).!'.!S-LOP
S'! L)A*-ALA/ G 4
CH'&' L)A*-ALA/ G 86
+P*A!' $).!'.!S-LOP
S'! L)A*-ALA/ G 8
CH'&' P&%)&%!7 G ,
S'L'$! *'$)*', P&%)&%!7,,S'L'$! 0AM,P&%)&%!75 A&)0 $).!'.!S-LOP5 ,8 , P&%)&%!7R85
A&)0 $).!'.!S-LOP
CH'&' $).!'.!-!7P' G ,S'L'$! *%S!%.$! $).!'.!-!7P' A&)0 !/!-$).!'.!S5
56
#re$ %o&&and in 'ni( and Linu( E(a&$les
/rep is t"e frequently used command in +ni ,or Linu5. 0ost of us use grep Kust for finding t"e words
in a file. !"e power of grep comes wit" using its options and regularepressions. 7ou can analyIe large
sets of log files wit" t"e "elp of grep command.
/rep stands for /lobal searc" for &egular 'pressions and Print.
The )asic s"nta( o* +re$ co&&and is
grep ToptionsU pattern Tlist of filesU
Let see some practical eamples on grep command.
1. &unning t"e last eecuted grep command
!"is saves a lot of time if you are eecuting t"e same command again and again.
Vgrep
!"is displays t"e last eecuted grep command and also prints t"e result set of t"e command on t"e
terminal.
2. Searc" for a string in a file
!"is is t"e basic usage of grep command. %t searc"es for t"e given string in t"e specified file.
grep #'rror# logfile.tt
!"is searc"es for t"e string #'rror# in t"e log file and prints all t"e lines t"at "as t"e word #'rror#.
3. Searc"ing for a string in multiple files.
grep #string# file8 file2
grep #string# file-pattern
!"is is also t"e basic usage of t"e grep command. 7ou can manually specify t"e list of files you want to
searc" or you can specify a file pattern ,use regular epressions5 tosearc" for.
. $ase insensitive searc"
!"e Bi option enables to searc" for a string case insensitively in t"e give file. %t matc"es t"e words like
#+.%M#, #+ni#, #uni#.
grep Bi #+.i# file.tt
!. Specifying t"e searc" string as a regular epression pattern.
grep #WT4B>U.@# file.tt
!"is will searc" for t"e lines w"ic" starts wit" a number. &egular epressions is "uge topic and % am not
covering it "ere. !"is eample is Kust for providing t"e usage of regularepressions.
,. $"ecking for t"e w"ole words in a file.
(y default, grep matc"es t"e given stringXpattern even if it found as a substring in a file. !"e Bw option
to grep makes it matc" only t"e w"ole words.
grep Bw #world# file.tt
-. *isplaying t"e lines before t"e matc".
Some times, if you are searc"ing for an error in a log file6 it is always good to know t"e lines around t"e
error lines to know t"e cause of t"e error.
grep B( 2 #'rror# file.tt
!"is will prints t"e matc"ed lines along wit" t"e two lines before t"e matc"ed lines.
.. *isplaying t"e lines after t"e matc".
grep BA 3 #'rror# file.tt
!"is will display t"e matc"ed lines along wit" t"e t"ree lines after t"e matc"ed lines.
/. *isplaying t"e lines around t"e matc"
grep B$ ; #'rror# file.tt
!"is will display t"e matc"ed lines and also five lines before and after t"e matc"ed lines.
10. Searc"ing for a sting in all files recursively
7ou can searc" for a string in all t"e files under t"e current directory and subBdirectories wit" t"e "elp Br
option.
grep Br #string# @
11. %nverting t"e pattern matc"
7ou can display t"e lines t"at are not matc"ed wit" t"e specified searc" sting patternusing t"e Bv option.
grep Bv #string# file.tt
12. *isplaying t"e nonBempty lines
7ou can remove t"e blank lines using t"e grep command.
grep Bv #WY# file.tt
13. *isplaying t"e count of number of matc"es.
Ce can find t"e number of lines t"at matc"es t"e given stringXpattern
grep Bc #sting# file.tt
1. *isplay t"e file names t"at matc"es t"e pattern.
Ce can Kust display t"e files t"at contains t"e given stringXpattern.
grep Bl #string# file.tt
1!. *isplay t"e file names t"at do not contain t"e pattern.
Ce can display t"e files w"ic" do not contain t"e matc"ed stringXpattern.
grep Bl #string# file.tt
1,. *isplaying only t"e matc"ed pattern.
(y default, grep displays t"e entire line w"ic" "as t"e matc"ed string. Ce can make t"e grep to display
only t"e matc"ed string by using t"e Bo option.
grep Bo #string# file.tt
1-. *isplaying t"e line numbers.
Ce can make t"e grep command to display t"e position of t"e line w"ic" contains t"e matc"ed string in
a file using t"e Bn option
grep Bn #string# file.tt
1.. *isplaying t"e position of t"e matc"ed string in t"e line
!"e Bb option allows t"e grep command to display t"e c"aracter position of t"e matc"ed string in a file.
grep Bo Bb #string# file.tt
1/. 0atc"ing t"e lines t"at start wit" a string
!"e W regular epression pattern specifies t"e start of a line. !"is can be used in grep to matc" t"e lines
w"ic" start wit" t"e given string or pattern.
grep #Wstart# file.tt
20. 0atc"ing t"e lines t"at end wit" a string
!"e Y regular epression pattern specifies t"e end of a line. !"is can be used in grep to matc" t"e lines
w"ic" end wit" t"e given string or pattern.
grep #endY# file.tt
1asic 'ni( and Linu( %o&&ands 2ith E(a&$les
Learning uni operating system is very easy. %t is Kust t"at you need to understand t"e uni server
concepts and familiar wit" t"e uni commands. Here % am providing some important uni
commands w"ic" will be used in daily work.
+ni $ommands Cit" E(a&$lesP
8. Listing files
!"e first t"ing after logging into t"e uni system, everyone does is listing t"e files in a directory. !"e ls
command is used to list t"e files in a directory.
Jls
add.s"
logfile.tt
prime.pl
%f you simply eecute ls on t"e command prompt, t"en it will display t"e files and directories in t"e
current directory.
Jls XusrXlocalXbin
7ou can pass a directory as an argument to ls command. %n t"is case, t"e ls command prints all t"e files
and directories in t"e specific directory you "ave passed.
2. *isplaying t"e contents of a file.
!"e net t"ing is to display t"e contents of a file. !"e cat command is used to display t"econtents in a
file.
Jcat file.tt
!"is is a sample uni file
Learning about uni server is awesome
3. *isplaying first few lines from a file.
!"e "ead command can be used to print t"e specified number of lines from t"e starting of a file. !"e
below "ead command displays t"e first five lines of file.
J"ead B; logfile.dat
:. *isplaying last few lines from a file.
!"e tail command can be used to print t"e specified number of lines from t"e ending of a file. !"e
below tail command displays t"e last t"ree lines of file.
Jtail B3 logfile.dat
;. $"anging t"e directories
!"e cd command can be used to c"ange from one directory to anot"er directory. 7ou need to specify
t"e target directory w"ere you want to go.
Jcd XvarXtmp
After typing t"is cd command you will be in XvarXtmp directory.
<. $reating a file.
!"e touc" command simply creates an empty file. !"e below touc" command creates a new file in t"e
current directory.
touc" new-file.tt
?. copying t"e contents of one file into anot"er.
!"e cp command is used to copy t"e content of source file into t"e target file. %f t"e target file already
"ave data, t"en it will be overwritten.
Jcp source-file target-file
=. $reating a directory.
*irectories are a way of organiIing your files. !"e mkdir command is used to create t"e specified
directory.
Jmkdir backup
!"is will create t"e backup directory in t"e current directory.
>. &enaming and moving t"e files.
!"e mv command is used to rename t"e files and it also used for moving t"e files from one directory
into anot"er directory.
&enaming t"e file.
Jmv file.tt new-file.tt
0oving t"e file to anot"er directory.
Jmv new-file.tt tmpX
84. Ainding t"e number of lines in a file
!"e wc command can be used to find t"e number of line, words and c"aracters in a file.
Jwc logfile.tt
28 2< 8>= logfile.tt
!o know about t"e uni command, it is always good to see t"e man pages. !o see t"e man pages
simply pass t"e command as an argument to t"e man.
man ls
SQL Queries Interview Questions - Oracle 3nal"tical 4unctions Part 1
Analytic functions compute aggregate values based on a group of rows. !"ey differ from aggregate
functions in t"at t"ey return multiple rows for eac" group. 0ost of t"e SQL developers won9t use
analytical functions because of its cryptic synta or uncertainty about its logic of operation. Analytical
functions saves lot of time in writing queries and gives better performance w"en compared to native
SQL.
(efore starting wit" t"e interview questions, we will see t"e difference between t"e aggregate functions
and analytic functions wit" an eample. % "ave used SAL'S !A(L'as an eample to solve
t"e interview questions. Please create t"e below sales table in your oracle database.
$&'A!' !A(L' SAL'S
,
SAL'-%* %.!'/'&,
P&)*+$!-%* %.!'/'&,
7'A& %.!'/'&,
Quantity %.!'/'&,
P&%$' %.!'/'&
56
%.S'&! %.!) SAL'S 1AL+'S , 8, 844, 244=, 84, ;44456
%.S'&! %.!) SAL'S 1AL+'S , 2, 844, 244>, 82, ;44456
%.S'&! %.!) SAL'S 1AL+'S , 3, 844, 2484, 2;, ;44456
%.S'&! %.!) SAL'S 1AL+'S , :, 844, 2488, 8<, ;44456
%.S'&! %.!) SAL'S 1AL+'S , ;, 844, 2482, =, ;44456
%.S'&! %.!) SAL'S 1AL+'S , <, 244, 2484, 84, >44456
%.S'&! %.!) SAL'S 1AL+'S , ?, 244, 2488, 8;, >44456
%.S'&! %.!) SAL'S 1AL+'S , =, 244, 2482, 24, >44456
%.S'&! %.!) SAL'S 1AL+'S , >, 244, 244=, 83, >44456
%.S'&! %.!) SAL'S 1AL+'S , 84,244, 244>, 8:, >44456
%.S'&! %.!) SAL'S 1AL+'S , 88, 344, 2484, 24, ?44456
%.S'&! %.!) SAL'S 1AL+'S , 82, 344, 2488, 8=, ?44456
%.S'&! %.!) SAL'S 1AL+'S , 83, 344, 2482, 24, ?44456
%.S'&! %.!) SAL'S 1AL+'S , 8:, 344, 244=, 8?, ?44456
%.S'&! %.!) SAL'S 1AL+'S , 8;, 344, 244>, 8>, ?44456
$)00%!6
S'L'$! @ A&)0 SAL'S6
SAL'-%* P&)*+$!-%* 7'A& Q+A.!%!7 P&%$'
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
8 844 244= 84 ;444
2 844 244> 82 ;444
3 844 2484 2; ;444
: 844 2488 8< ;444
; 844 2482 = ;444
< 244 2484 84 >444
? 244 2488 8; >444
= 244 2482 24 >444
> 244 244= 83 >444
84 244 244> 8: >444
88 344 2484 24 ?444
82 300 2011 8= ?444
83 344 2482 24 ?444
8: 344 244= 8? ?444
8; 344 244> 8> ?444
5i**erence 1etween 3++re+ate and 3nal"tic 4unctions:
Q. Crite a query to find t"e number of products sold in eac" yearD
!"e SQL query +sing Aggregate functions is
S'L'$! 7ear,
$)+.!,85 $.!
A&)0 SAL'S
/&)+P (7 7'A&6
7'A& $.!
BBBBBBBBB
244> 3
2484 3
2488 3
244= 3
2482 3
!"e SQL query +sing Aanalytic functions is
S'L'$! SAL'-%*,
P&)*+$!-%*,
7ear,
Q+A.!%!7,
P&%$',
$)+.!,85 )1'& ,PA&!%!%). (7 7'A&5 $.!
A&)0 SAL'S6
SAL'-%* P&)*+$!-%* 7'A& Q+A.!%!7 P&%$' $.!
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
> 244 244= 83 >444 3
8 844 244= 84 ;444 3
8: 344 244= 8? ?444 3
8; 344 244> 8> ?444 3
2 844 244> 82 ;444 3
84 244 244> 8: >444 3
88 344 2484 24 ?444 3
< 244 2484 84 >444 3
3 844 2484 2; ;444 3
82 344 2488 8= ?444 3
: 844 2488 8< ;444 3
? 244 2488 8; >444 3
83 344 2482 24 ?444 3
; 844 2482 = ;444 3
= 244 2482 24 >444 3
Arom t"e ouputs, you can observe t"at t"e aggregate functions return only one row per group w"ereas
analytic functions keeps all t"e rows in t"e gorup. +sing t"e aggregate functions, t"e select clause
contains only t"e columns specified in group by clause and aggregate functions w"ereas in analytic
functions you can specify all t"e columns in t"etable.
!"e PA&!%!%). (7 clause is similar to /&)+P (y clause, it specifies t"e window of rows t"at t"e
analytic funciton s"ould operate on.
% "ope you got some basic idea about aggregate and analytic functions. .ow lets start wit" solving
t"e %nterview Questions on )racle Analytic Aunctions.
1. Crite a SQL query using t"e analytic function to find t"e total sales,Q+A.!%!75 of eac" productD
Solution:
S+0 analytic function can be used to find t"e total sales. !"e SQL query is
S'L'$! P&)*+$!-%*,
Q+A.!%!7,
S+0,Q+A.!%!75 )1'&, PA&!%!%). (7 P&)*+$!-%* 5 !)!-SAL'S
A&)0 SAL'S6
P&)*+$!-%* Q+A.!%!7 !)!-SAL'S
BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
844 82 ?8
844 84 ?8
844 2; ?8
844 8< ?8
844 = ?8
244 8; ?2
244 84 ?2
244 24 ?2
244 8: ?2
244 83 ?2
344 24 >:
344 8= >:
344 8? >:
344 24 >:
344 8> >:
2. Crite a SQL query to find t"e cumulative sum of sales,Q+A.!%!75 of eac" productD Here first sort
t"e Q+A.!%!7 in ascendaing order for eac" product and t"en accumulate t"e Q+A.!%!7.
$umulative sum of Q+A.!%!7 for a product G Q+A.!%!7 of current row R sum of Q+A.!%!%'S all
previous rows in t"at product.
Solution:
Ce "ave to use t"e option #&)CS +.()+.*'* P&'$'*%./# in t"e S+0 analytic function to get t"e
cumulative sum. !"e SQL query to get t"e ouput is
S'L'$! P&)*+$!-%*,
Q+A.!%!7,
S+0,Q+A.!%!75 )1'&, PA&!%!%). (7 P&)*+$!-%*
)&*'& (7 Q+A.!%!7 AS$
&)CS +.()+.*'* P&'$'*%./5 $+0-SAL'S
A&)0 SAL'S6
P&)*+$!-%* Q+A.!%!7 $+0-SAL'S
BBBBBBBBBBBBBBBBBBBBBBBBBBBBB
844 = =
844 84 8=
844 82 34
844 8< :<
844 2; ?8
244 84 84
244 83 23
244 8: 3?
244 8; ;2
244 24 ?2
344 8? 8?
344 8= 3;
344 8> ;:
344 24 ?:
344 24 >:
!"e )&*'& (7 clause is used to sort t"e data. Here t"e &)CS +.()+.*'* P&'$'*%./ option
specifies t"at t"e S+0 analytic function s"ould operate on t"e current row and t"e pervious rows
processed.
3. Crite a SQL query to find t"e sum of sales of current row and previous 2 rows in a product groupD
Sort t"e data on sales and t"en find t"e sum.
Solution:
!"e sql query for t"e required ouput is
S'L'$! P&)*+$!-%*,
Q+A.!%!7,
S+0,Q+A.!%!75 )1'&,
PA&!%!%). (7 P&)*+$!-%*
)&*'& (7 Q+A.!%!7 *'S$
&)CS ('!C''. 2 P&'$'*%./ A.* $+&&'.! &)C5 $AL$-SAL'S
A&)0 SAL'S6
P&)*+$!-%* Q+A.!%!7 $AL$-SAL'S
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
844 2; 2;
844 8< :8
844 82 ;3
844 84 3=
844 = 34
244 24 24
244 8; 3;
244 8: :>
244 83 :2
244 84 3?
344 24 24
344 24 :4
344 8> ;>
344 8= ;?
344 8? ;:
!"e &)CS ('!C''. clause specifies t"e range of rows to consider for calculating t"e S+0.
. Crite a SQL query to find t"e 0edian of sales of a productD
Solution:
!"e SQL query for calculating t"e median is
S'L'$! P&)*+$!-%*,
Q+A.!%!7,
P'&$'.!%L'-$).!,4.;5 C%!H%. /&)+P ,)&*'& (7 Q+A.!%!7 AS$5
)1'& ,PA&!%!%). (7 P&)*+$!-%*5 0'*%A.
A&)0 SAL'S6
P&)*+$!-%* Q+A.!%!7 0'*%A.
BBBBBBBBBBBBBBBBBBBBBBBBBB
844 = 82
844 84 82
844 82 82
844 8< 82
844 2; 82
244 84 8:
244 83 8:
244 8: 8:
244 8; 8:
244 24 8:
344 8? 8>
344 8= 8>
344 8> 8>
344 24 8>
344 24 8>
!. Crite a SQL query to find t"e minimum sales of a product wit"out using t"e group by clause.
Solution:
!"e SQL query is
S'L'$! P&)*+$!-%*,
7'A&,
Q+A.!%!7
A&)0
,
S'L'$! P&)*+$!-%*,
7'A&,
Q+A.!%!7,
&)C-.+0('&,5 )1'&,PA&!%!%). (7 P&)*+$!-%*
)&*'& (7 Q+A.!%!7 AS$5 0%.-SAL'-&A.O
A&)0 SAL'S
5 CH'&' 0%.-SAL'-&A.O G 86
P&)*+$!-%* 7'A& Q+A.!%!7
BBBBBBBBBBBBBBBBBBBBBBBB
844 2482 =
244 2484 84
344 244= 8?
SQL Interview Questions and 3nswers
8. C"at is .ormaliIationD
.ormaliIation is t"e process of organiIing t"e columns, tables of a database to minimiIe t"e
redundancy of data. .ormaliIation involves in dividing large tables into smaller tables and defining
relations"ips between t"em. .ormaliIation is used in )L!P systems.
2. C"at are different types of .ormaliIation Levels or .ormaliIation AormsD
!"e different types of .ormaliIation Aorms areP
Airst .ormal AormP *uplicate columns from t"e same table needs to be eliminated. Ce "ave to
create separate tables for eac" group of related data and identify eac" row wit" a unique column or
set of columns ,Primary Oey5
Second .ormal AormP Airst it s"ould meet t"e requirement of first normal form. &emoves t"e
subsets of data t"at apply to multiple rows of a table and place t"em in separate tables.
&elations"ips must be created between t"e new tables and t"eir predecessors t"roug" t"e use of
foreign keys.
!"ird .ormal AormP Airst it s"ould meet t"e requirements of second normal form. &emove columns
t"at are not depending upon t"e primary key.
Aourt" .ormal AormP !"ere s"ould not be any multiBvalued dependencies.
0ost databases will be in !"ird .ormal Aorm
3. C"at is *eBnormaliIationD
*eBnormaliIation is t"e process of optimiIing t"e read performance of a database by adding redundant
data or by grouping data. *eBnormaliIation is used in )LAP systems.
:. C"at is a !ransactionD
A transaction is a logical unit of work performed against a database in w"ic" all steps must be
performed or none.
;. C"at are A$%* propertiesD
A database transaction must be Atomic, $onsistent, %solation and *urability.
AtomicP !ransactions must be atomic. !ransactions must fail or succeed as a single unit.
$onsistentP !"e database must always be in consistent state. !"ere s"ould not be any partial
transactions
%solationP !"e c"anges made by a user s"ould be visible only to t"at user until t"e transaction is
committed.
*urabilityP )nce a transaction is committed, it s"ould be permanent and cannot be undone.
<. 'plain different storage models of )LAPD
0)LAPP !"e data is stored in multiBdimensional cube. !"e storage is not in t"e relational database,
but in proprietary formats.
&)LAPP &)LAP relies on manipulating t"e data stored in t"e &*(0S for slicing and dicing
functionality.
H)LAPP H)LAP combines t"e advantages of bot" 0)LAP and &)LAP. Aor summary type
information, H)LAP leverages on cube tec"nology for faster performance. Aor detail information,
H)LAP can drill t"roug" t"e cube.
?. 'plain oneBtoBone relations"ip wit" an eampleD
)ne to one relations"ip is a simple reference between two tables. $onsider $ustomer and Address
tables as an eample. A customer can "ave only one address and an address references only one
customer.
=. 'plain oneBtoBmany relations"ip wit" an eampleD
)neBtoBmany relations"ips can be implemented by splitting t"e data into two tables wit" a primary key
and foreign key relations"ip. Here t"e row in one table is referenced by one or more rows in t"e ot"er
table. An eample is t"e 'mployees and *epartments table, w"ere t"e row in t"e *epartments table is
referenced by one or more rows in t"e 'mployees table.
>. 'plain manyBtoBmany relations"ip wit" an eampleD
0anyBtoB0any relations"ip is created between two tables by creating a Kunction table wit" t"e key from
bot" t"e tables forming t"e composite primary key of t"e Kunction table.
An eample is Students, SubKects and Stud-Sub-Kunc tables. A student can opt for one or more
subKects in a year. Similarly a subKect can be opted by one or more students. So a Kunction table is
created to implement t"e manyBtoBmany relations"ip.
84. Crite down t"e general synta of a select statementD
!"e basic synta of a select statement is
S'L'$! $olumns Z @
A&)0 !able-.ame
TCH'&' Searc"-$onditionU
T/&)+P (7 /roup-(y-'pressionU
THA1%./ Searc"-$onditionU
T)&*'& (7 )rder-(y-'pression TAS$Z*'S$UU
Strin+ a++re+atin+ 3nal"tic 4unctions in Oracle 5ata)ase
!"e string aggregate functions concatenate multiple rows into a single row. $onsider t"e
products table as an eample.
!able .ameP Products
7ear product
BBBBBBBBBBBBB
2484 A
2484 (
2484 $
2484 *
2488 M
2488 7
2488 S
Here, in t"e output we will concatenate t"e products in eac" year by a comma separator. !"e desired
output isP
year product-list
BBBBBBBBBBBBBBBBBB
2484 A,(,$,*
2488 M,7,S
L%S!A// analytic function in 88g&2P
!"e L%S!A// function can be used to aggregate t"e strings. 7ou can pass t"e eplicit delimiter to t"e
L%S!A// function.
S'L'$! year,
L%S!A//,product, 9,95 C%!H%. /&)+P ,)&*'& (7 product5 AS product-list
A&)0 products
/&)+P (7 year6
C0-$).$A! functionP
7ou cannot pass an eplicit delimiter to t"e C0-$).$A! function. %t uses comma as t"e string
separator.
S'L'$! year,
wm-concat,product5 AS product-list
A&)0 products
/&)+P (7 year6
Pivot and 'n$ivot O$erators in Oracle 5ata)ase 11+
PivotP
!"e pviot operator converts row data to column data and also can do aggregates w"ileconverting. !o
see "ow pivot operator works, consider t"e following #sales# table as anyeample
!able .ameP Sales
customer-id product 'r(ce
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
8 A 84
8 ( 24
2 A 34
2 ( :4
2 $ ;4
3 A <4
3 ( ?4
3 $ =4
!"e rows of t"e #sales# table needs to be converted into columns as s"own below
!able .ameP sales-rev
cutomer-id a-product b-product c-product
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
8 84 24
2 34 :4 ;4
3 <4 ?4 =4
!"e query for converting t"e rows to columns is
S'L'$! @
A&)0 ,S'L'$! customer-id,product,price from sales5
pivot , sum,price5 as total-price for ,product5 %. , 9A9 as a, 9(9 as b, 9$9 as c5 5
Pivot can be used to generate t"e data in ml format. !"e query for generating t"e data into ml fomat
is s"own below.
S'L'$! @
A&)0 ,S'L'$! customer-id,product,price from sales5
pivot M0L , sum,price5 as total-price for ,product5 %. , S'L'$! distinct product from sales5 5
%f you are not using oracle 88g database, t"en you can implement t"e unpivot feature asconverting rows
to columns
+npivotP
+npivot operator converts t"e columns into rows.
!able .ameP sales-rev
cutomer-id a-product b-product c-product
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
8 84 24
2 34 :4 ;4
3 <4 ?4 =4
!able .ameP sales
customer-id product price
BBBBBBBBBBBBBBBBBBBBBBBBBBB
8 A 84
8 ( 24
2 A 34
2 ( :4
2 $ ;4
3 A <4
3 ( ?4
3 $ =4
!"e query to convert rows into columns is
S'L'$! @
A&)0 sales-rev
+.P%1)! T'M$L+*' .+LLs Z %.$L+*' .+LLsU ,price A)& product %. ,a-product AS 9A9, b-product
AS 9(9, c-product-c AS 9$9556
Points to note about t"e query
!"e columns price and product in t"e unpivot clause are required and t"ese names need not to be
present in t"e table.
!"e unpivoted columns must be specified in t"e %. clause
(y default t"e query ecludes null values.
Min and Ma( values o* conti+uous rows - Oracle SQL Quer"
Q5 How to find t"e 0inimum and maimum values of continuous sequence numbers in a group of rows.
% know t"e problem is not clear wit"out giving an eample. Let say % "ave t"e 'mployees table wit" t"e
below data.
!able .ameP 'mployees
*ept-%d 'mp-Seq
BBBBBBBBBBBBBBB
84 8
84 2
84 3
84 )
10 <
84 =
84 *
10 11
24 8
24 2
% want to find t"e minimum and maimum values of continuous 'mp-Seq numbers. !"e output s"ould
look as.
*ept-%d 0in-Seq 0a-Seq
BBBBBBBBBBBBBBBBBBBBBBB
84 8 3
84 ; <
84 = >
84 88 88
24 8 2
Crite an SQL query in oracle to find t"e minimum and maimum values of continuous 'mp-Seq in
eac" departmentD
S!'P8P Airst we will generate unique sequence numbers in eac" department using t"e &ow-.umber
analytic function in t"e )racle. !"e SQL query is.
S'L'$! *ept-%d,
'mp-Seq,
&)C-.+0('&,5 )1'& ,PA&!%!%). (7 *ept-%d )&*'& (7 'mp-Seq5 rn
A&)0 employees6
*ept-%d 'mp-Seq rn
BBBBBBBBBBBBBBBBBBBB
84 8 8
84 2 2
84 3 3
84 ; :
84 < ;
84 = <
84 > ?
84 88 =
24 8 8
24 2 2
S!'P2P Subtract t"e value of rn from emp-seq to identify t"e continuous sequences as a group. !"e
SQL query is
S'L'$! *ept-%d,
'mp-Seq,
'mp-SeqB&)C-.+0('&,5 )1'& ,PA&!%!%). (7 *ept-%d )&*'& (7 'mp-Seq5 *ept-Split
A&)0 employees6
*ept-%d 'mp-Seq *ept-Split
BBBBBBBBBBBBBBBBBBBBBBBBBBB
84 8 4
84 2 4
84 3 4
84 ; 8
84 < 8
84 = 2
84 > 2
84 88 3
24 8 4
24 2 4
S!'P3P !"e combination of t"e *ept-%d and *ept-Split fields will become t"e group for continuous
rows. .ow use group by on t"ese fields and find t"e min and ma values. !"e final SQL query is
S'L'$! *ept-%d,
0%.,'mp-Seq5 0in-Seq,
0AM,'mp-Seq5 0a-Seq
A&)0
,
S'L'$! *ept-%d,
'mp-Seq,
'mp-SeqB&)C-.+0('&,5 )1'& ,PA&!%!%). (7 *ept-%d )&*'& (7 'mp-Seq5 *ept-Split
A&)0 employees6
5 A
/roup (7 *ept-%d, *ept-Split
6ewrite S7l Quer" 8 S7l Per*or&ance Tunin+
!uning an SQL query for performance is a big topic. Here % will Kust cover "ow to reBwrite a query and
t"ereby improve t"e performance. &ewriting an SQL query is one of t"e ways you can improve
performance. 7ou can rewrite a query in many different ways.
!o eplain t"is, i "ave used t"e sales and products table.
SAL'S,SAL'-%*, 7'A&, P&)*+$!-%*, P&%$'56
P&)*+$!S,P&)*+$!-%*, P&)*+$!-.A0'56
Aollow t"e below steps in re writing a query for optimiIation.
1. Avoid &edundant Logic
% "ave seen people writing redundant subBqueries and worrying about t"eir query performance. As an
eample, find t"e total sales in eac" year and also t"e sales of product wit" id 84 in eac" year.
S'L'$! !.7'A&,
!.!)!-SAL,
P.P&)*-84-SAL
,
S'L'$! 7'A&,
S+0,P&%$'5 !)!-SAL
A&)0 SAL'S
/&)+P (7 7'A&
5 !
L'A! )+!'& L)%.
,
S'L'$! 7'A&,
S+0,P&%$'5 P&)*-84-SAL
A&)0 SAL'S
CH'&' P&)*+$!-%* G 84
5 P
). ,!.7'A& G P.7'A&56
0ost SQL developers write t"e above Sql query wit"out even t"inking t"at it can be solved in a single
query. !"e above query is rewritten as
S'L'$! 7'A&,
S+0,$AS' CH'. P&)*+$!-%* G 84
!H'. P&%$'
'LS' .+LL
'.* 5 P&)*-84-SAL,
S+0,SAL'S5 !)!-SAL
A&)0 SAL'S
/&)+P (7 7'A&6
.ow you can see t"e difference, Kust by reading t"e sales table one time we will able to solve
t"e problem.
Airst take a look at of your query, identify t"e redundant logic and t"en tune it.
2. L'A! )+!'& L)%., .)! 'M%S!S, .)! %.
Some times you can rewrite a L'A! )+!'& L)%. by using .)! 'M%S!S or .)! %. and vice versa. As
an eample, % want to find t"e products w"ic" do not sold in t"e year 2488.
S'L'$! P.P&)*+$!-%*,
P.P&)*+$!-.A0'
A&)0 P&)*+$!S P
L'A! )+!'& L)%.
SAL'S S
). ,P.P&)*+$!-%* G S.P&)*+$!-%*5
CH'&' S.SAL'-%* %S .+LL6
!"e same query can be rewritten using .)! 'M%S!S and .)! %. as
S'L'$! P.P&)*+$!-%*,
P.P&)*+$!-.A0'
A&)0 P&)*+$!S P
CH'&' .)! 'M%S!S
,
S'L'$! 8
A&)0 SAL'S S
CH'&' S.P&)*+$!-%* G P.P&)*+$!-%*56
S'L'$! P.P&)*+$!-%*,
P.P&)*+$!-.A0'
A&)0 P&)*+$!S P
CH'&' P&)*+$!-%* .)! %.
,
S'L'$! P&)*+$!-%*
A&)0 SAL'S
56
AnalyIe t"e performance of t"ese t"ree queries and use t"e appropriate one.
9ote: (e careful w"ile using t"e .)! %.. %f t"e sub query returns at lease row wit" .+LL data, t"en t"e
main query won9t return a row at all.
3. %..'& L)%., 'M%S!S, %.
As similar to L'A! )+!'& L)%., t"e %..'& L)%.S can also be implemented wit" t"e 'M%S!S or
%. operators. As an eample, we will find t"e sales of products w"ose product idHs eists in t"e
products table.
S'L'$! S.P&)*+$!-%*,
S+0,P&%$'5
A&)0 SAL'S S
L)%.
P&)*+$!S P
). ,S.P&)*+$!-%* G P.P&)*+$!-%*5
/&)+P (7 S.P&)*+$!-%*6
As we are not selecting any columns from t"e products table, we can rewrite t"e same query wit" t"e
"elp of 'M%S!S or %. operator.
S'L'$! S.P&)*+$!-%*,
S+0,P&%$'5
A&)0 SAL'S S
CH'&' 'M%S!S
,
S'L'$! 8
A&)0 P&)*+$!S P
CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*56
/&)+P (7 S.P&)*+$!-%*6
S'L'$! S.P&)*+$!-%*,
S+0,P&%$'5
A&)0 SAL'S S
CH'&' P&)*+$!-%* %.
,
S'L'$! P&)*+$!-%*
A&)0 P&)*+$!S P
56
/&)+P (7 S.P&)*+$!-%*6
. %..'& L)%., $)&&'LA!'* Q+'&7
Ce will see a simple Koin between t"e SAL'S and P&)*+$!S table.
S'L'$! S.SAL'-%*,
S.P&)*+$!-%*,
P.P&)*+$!-.A0'
A&)0 SAL'S S
L)%.
P&)*+$!S P
). ,S.P&)*+$!-%* G P.P&)*+$!-%*5
!"e above query can be rewritten wit" correlated query as
S'L'$! S.SAL'-%*,
S.P&)*+$!-%*,
,S'L'$! P&)*+$!-.A0'
A&)0 P&)*+$!S P
CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*5
A&)0 SAL'S S
AnalyIe t"ese two queries wit" t"e eplain plan and c"eck w"ic" one gives better performance.
!. +sing Cit" $lause or !emporary !ables.
!ry to avoid writing comple Sql queries. Split t"e queries and store t"e data in temporary tables or use
t"e )racle Cit" $lause for temporary storage. !"is will improve t"e performance. 7ou can also use t"e
temporary tables or wit" clause w"en you want to reuse t"e same query more t"an once. !"is saves
t"e time and increases t"e performance.
Ti$s *or increasin+ the 7uer" $er*or&ance:
$reate t"e required indees. %n t"e mean time avoid creating too many indees on a table.
&ewrite t"e Sql query.
+se t"e eplain plan, auto trace to know about t"e query eecution.
/enerate statistics on tables.
Specify t"e oracle Hints in t"e query.
Ask t"e *(A to watc" t"e query and gat"er stats like $P+ usage, number of row read etc.
To$ 'ni( Interview Questions - Part 1
8. How to display t"e 84t" line of a fileD
"ead B84 filename Z tail B8
2. How to remove t"e "eader from a fileD
sed Bi 98 d9 filename
3. How to remove t"e footer from a fileD
sed Bi 9Y d9 filename
:. Crite a command to find t"e lengt" of a line in a fileD
!"e below command can be used to get a line from a file.
sed [n 9NnJ p9 filename
Ce will see "ow to find t"e lengt" of 84t" line in a file
sed Bn 984 p9 filenameZwc Bc
;. How to get t"e nt" word of a line in +niD
cut [fNnJ Bd9 9
<. How to reverse a string in uniD
ec"o #Kava# Z rev
?. How to get t"e last word from a line in +ni fileD
ec"o #uni is good# Z rev Z cut Bf8 Bd9 9 Z rev
=. How to replace t"e nBt" line in a file wit" a new line in +niD
sed Bi99 984 d9 filename \ d stands for delete
sed Bi99 984 i new inserted line9 filename \ i stands for insert
>. How to c"eck if t"e last command was successful in +niD
ec"o YD
84. Crite command to list all t"e links from a directoryD
ls Blrt Z grep #Wl#
88. How will you find w"ic" operating system your system is running on in +.%MD
uname Ba
82. $reate a readBonly file in your "ome directoryD
touc" file6 c"mod :44 file
83. How do you see command line "istory in +.%MD
!"e 9"istory9 command can be used to get t"e list of commands t"at we are eecuted.
8:. How to display t"e first 24 lines of a fileD
(y default, t"e "ead command displays t"e first 84 lines from a file. %f we c"ange t"e option of "ead,
t"en we can display as many lines as we want.
"ead B24 filename
An alternative solution is using t"e sed command
sed 928,Y d9 filename
!"e d option "ere deletes t"e lines from 28 to t"e end of t"e file
8;. Crite a command to print t"e last line of a fileD
!"e tail command can be used to display t"e last lines from a file.
tail B8 filename
Alternative solutions areP
sed Bn 9Y p9 filename
awk 9'.*]print Y4^9 filename
To$ 'ni( Interview Questions - Part 2
8. How do you rename t"e files in a directory wit" -new as suffiD
ls BlrtZgrep 9WB9Z awk 9]print #mv #Y># #Y>#.new#^9 Z s"
2. Crite a command to convert a string from lower case to upper caseD
ec"o #apple# Z tr TaBIU TABSU
3. Crite a command to convert a string to %nitcap.
ec"o apple Z awk 9]print toupper,substr,Y8,8,855 tolower,substr,Y8,255^9
:. Crite a command to redirect t"e output of date command to multiple filesD
!"e tee command writes t"e output to multiple files and also displays t"e output on t"e terminal.
date Z tee Ba file8 file2 file3
;. How do you list t"e "idden files in current directoryD
ls Ba Z grep 9W_.9
<. List out some of t"e Hot Oeys available in bas" s"ellD
$trlRl B $lears t"e Screen.
$trlRr B *oes a searc" in previously given commands in s"ell.
$trlRu B $lears t"e typing before t"e "otkey.
$trlRa B Places cursor at t"e beginning of t"e command at s"ell.
$trlRe B Places cursor at t"e end of t"e command at s"ell.
$trlRd B Oills t"e s"ell.
$trlRI B Places t"e currently running process into background.
?. How do you make an eisting file emptyD
cat XdevXnull J filename
=. How do you remove t"e first number on 84t" line in fileD
sed 984 sXT4B>UT4B>U@XX9 N filename
>. C"at is t"e difference between Koin Bv and Koin BaD
Koin Bv P outputs only matc"ed lines between two files.
Koin Ba P %n addition to t"e matc"ed lines, t"is will output unmatc"ed lines also.
84. How do you display from t"e ;t" c"aracter to t"e end of t"e line from a fileD
cut Bc ;B filename
To$ 'ni( Interview Questions - Part 3
8. *isplay all t"e files in current directory sorted by siIeD
ls Bl Z grep 9WB9 Z awk 9]print Y;,Y>^9 Zsort BnZawk 9]print Y2^9
2. Crite a command to searc" for t"e file 9map9 in t"e current directoryD
find Bname map Btype f
3. How to display t"e first 84 c"aracters from eac" line of a fileD
cut Bc B84 filename
:. Crite a command to remove t"e first number on all lines t"at start wit" #`#D
sed 9_,W`, sXT4B>UT4B>U@XX9 N filename
;. How to print t"e file names in a directory t"at "as t"e word #term#D
grep Bl term @
!"e 9Bl9 option make t"e grep command to print only t"e filename wit"out printing t"e content of t"e file.
As soon as t"e grep command finds t"e pattern in a file, it prints t"e pattern and stops searc"ing ot"er
lines in t"e file.
<. How to run awk command specified in a fileD
awk Bf filename
?. How do you display t"e calendar for t"e mont" marc" in t"e year 8>=;D
!"e cal command can be used to display t"e current mont" calendar. 7ou can pass t"e mont" and year
as arguments to display t"e required year, mont" combination calendar.
cal 43 8>=;
!"is will display t"e calendar for t"e 0arc" mont" and year 8>=;.
=. Crite a command to find t"e total number of lines in a fileD
wc Bl filename
)t"er ways to pring t"e total number of lines are
awk 9('/%. ]sumG4^ ]sumGsumR8^ '.* ]print sum^9 filename
awk 9'.*]print .&^9 filename
>. How to duplicate empty lines in a fileD
sed 9XWYX p9 N filename
84. 'plain iostat, vmstat and netstatD
%ostatP reports on terminal, disk and tape %X) activity.
1mstatP reports on virtual memory statistics for processes, disk, tape and $P+ activity.
.etstatP reports on t"e contents of network data structures.
To$ 'ni( Interview Questions - Part
8. How do you write t"e contents of 3 files into a single fileD
cat file8 file2 file3 J file
2. How to display t"e fields in a tet file in reverse orderD
awk 9('/%. ])&SG##^ ] for,iG.A6iJ46iBB5 print Yi,# #6 print #_n#^9 filename
3. Crite a command to find t"e sum of bytes ,siIe of file5 of all files in a directory.
ls Bl Z grep 9WB9Z awk 9('/%. ]sumG4^ ]sum G sum R Y;^ '.* ]print sum^9
:. Crite a command to print t"e lines w"ic" end wit" t"e word #end#D
grep 9endY9 filename
!"e 9Y9 symbol specifies t"e grep command to searc" for t"e pattern at t"e end of t"e line.
;. Crite a command to select only t"ose lines containing #Kuly# as a w"ole wordD
grep Bw Kuly filename
!"e 9Bw9 option makes t"e grep command to searc" for eact w"ole words. %f t"e specified pattern is
found in a string, t"en it is not considered as a w"ole word. Aor eampleP %n t"e string #mikeKulymak#,
t"e pattern #Kuly# is found. However #Kuly# is not a w"ole word in t"at string.
<. How to remove t"e first 84 lines from a fileD
sed 98,84 d9 N filename
?. Crite a command to duplicate eac" line in a fileD
sed 9p9 N filename
=. How to etract t"e username from 9w"o am i9 comamndD
w"o am i Z cut Bf8 Bd9 9
>. Crite a command to list t"e files in 9Xusr9 directory t"at start wit" 9c"9 and t"en display t"e number of
lines in eac" fileD
wc Bl XusrXc"@
Anot"er way is
find Xusr Bname 9c"@9 Btype f Beec wc Bl ]^ _6
84. How to remove blank lines in a file D
grep Bv aWYH filename J new-filename
To$ 'ni( Interview Questions - Part !
8. How to display t"e processes t"at were run by your user name D
ps Baef Z grep Nuser-nameJ
2. Crite a command to display all t"e files recursively wit" pat" under current directoryD
find . Bdept" Bprint
3. *isplay Iero byte siIe files in t"e current directoryD
find BsiIe 4 Btype f
:. Crite a command to display t"e t"ird and fift" c"aracter from eac" line of a fileD
cut Bc 3,; filename
;. Crite a command to print t"e fields from 84t" to t"e end of t"e line. !"e fields in t"e line are delimited
by a commaD
cut Bd9,9 Bf84B filename
<. How to replace t"e word #/un# wit" #Pen# in t"e first 844 lines of a fileD
sed 98,44 sX/unXPenX9 N filename
?. Crite a +ni command to display t"e lines in a file t"at do not contain t"e word #&A0#D
grep Bv &A0 filename
!"e 9Bv9 option tells t"e grep to print t"e lines t"at do not contain t"e specified pattern.
=. How to print t"e squares of numbers from 8 to 84 using awk command
awk 9('/%. ] for,iG86iNG846iRR5 ]print #square of#,i,#is#,i@i6^^9
>. Crite a command to display t"e files in t"e directory by file siIeD
ls Bl Z grep 9WB9 Zsort Bnr Bk ;
84. How to find out t"e usage of t"e $P+ by t"e processesD
!"e top utility can be used to display t"e $P+ usage by t"e processes.
To$ 'ni( Interview Questions - Part ,
8. Crite a command to remove t"e prefi of t"e string ending wit" 9X9.
!"e basename utility deletes any prefi ending in X. !"e usage is mentioned belowP
basename XusrXlocalXbinXfile
!"is will display only file
2. How to display Iero byte siIe filesD
ls Bl Z grep 9WB9 Z awk 9XWBX ]if ,Y; VG4 5 print Y> ^9
3. How to replace t"e second occurrence of t"e word #bat# wit" #ball# in a fileD
sed 9sXbatXballX29 N filename
:. How to remove all t"e occurrences of t"e word #K"on# ecept t"e first one in a line wit" in t"e entire
fileD
sed 9sXK"onXX2g9 N filename
;. How to replace t"e word #lite# wit" #lig"t# from 844t" line to last line in a fileD
sed 9844,Y sXliteXlig"tX9 N filename
<. How to list t"e files t"at are accessed ; days ago in t"e current directoryD
find Batime ; Btype f
?. How to list t"e files t"at were modified ; days ago in t"e current directoryD
find Bmtime ; Btype f
=. How to list t"e files w"ose status is c"anged ; days ago in t"e current directoryD
find Bctime ; Btype f
>. How to replace t"e c"aracter 9X9 wit" 9,9 in a fileD
sed 9sX_XX,X9 N filename
sed 9sZXZ,Z9 N filename
84. Crite a command to find t"e number of files in a directory.
ls BlZgrep 9WB9Zwc Bl
To$ 'ni( Interview Questions - Part -
8. Crite a command to display your name 844 times.
!"e 7es utility can be used to repeatedly output a line wit" t"e specified string or 9y9.
yes Nyour-nameJ Z "ead B844
2. Crite a command to display t"e first 84 c"aracters from eac" line of a fileD
cut Bc B84 filename
3. !"e fields in eac" line are delimited by comma. Crite a command to display t"ird field from eac" line
of a fileD
cut Bd9,9 Bf2 filename
:. Crite a command to print t"e fields from 84 to 24 from eac" line of a fileD
cut Bd9,9 Bf84B24 filename
;. Crite a command to print t"e first ; fields from eac" lineD
cut Bd9,9 BfB; filename
<. (y default t"e cut command displays t"e entire line if t"ere is no delimiter in it. C"ic" cut option is
used to supress t"ese kind of linesD
!"e Bs option is used to supress t"e lines t"at do not contain t"e delimiter.
?. Crite a command to replace t"e word #bad# wit" #good# in fileD
sed sXbadXgoodX N filename
=. Crite a command to replace t"e word #bad# wit" #good# globally in a fileD
sed sXbadXgoodXg N filename
>. Crite a command to replace t"e word #apple# wit" #,apple5# in a fileD
sed sXappleX,Q5X N filename
84. Crite a command to switc" t"e two consecutive words #apple# and #mango# in a fileD
sed 9sX_,apple_5 _,mango_5X_2 _8X9 N filename
88. Crite a command to display t"e c"aracters from 84 to 24 from eac" line of a fileD
cut Bc 84B24 filename
8. Crite a command to print t"e lines t"at "as t"e t"e pattern #Kuly# in all t"e files in a particular
directoryD
grep Kuly @
!"is will print all t"e lines in all files t"at contain t"e word EKulyF along wit" t"e file name. %f any of t"e
files contain words like #L+L7# or #Luly#, t"e above command would not print t"ose lines.
2. Crite a command to print t"e lines t"at "as t"e word #Kuly# in all t"e files in a directory and also
suppress t"e filename in t"e output.
grep B" Kuly @
3. Crite a command to print t"e lines t"at "as t"e word #Kuly# w"ile ignoring t"e case.
grep Bi Kuly @
!"e option i make t"e grep command to treat t"e pattern as case insensitive.
:. C"en you use a single file as input to t"e grep command to searc" for a pattern, it won9t print t"e
filename in t"e output. .ow write a grep command to print t"e filename in t"e output wit"out using t"e 9B
H9 option.
grep pattern filename XdevXnull
!"e XdevXnull or null device is special file t"at discards t"e data written to it. So, t"e XdevXnull is always
an empty file.
Anot"er way to print t"e filename is using t"e 9BH9 option. !"e grep command for t"is is
grep BH pattern filename
;. Crite a command to print t"e file names in a directory t"at does not contain t"e word #Kuly#D
grep BL Kuly @
!"e 9BL9 option makes t"e grep command to print t"e filenames t"at do not contain t"e specified pattern.
<. Crite a command to print t"e line numbers along wit" t"e line t"at "as t"e word #Kuly#D
grep Bn Kuly filename
!"e 9Bn9 option is used to print t"e line numbers in a file. !"e line numbers start from 8
?. Crite a command to print t"e lines t"at starts wit" t"e word #start#D
grep 9Wstart9 filename
!"e 9W9 symbol specifies t"e grep command to searc" for t"e pattern at t"e start of t"e line.
=. %n t"e tet file, some lines are delimited by colon and some are delimited by space. Crite a command
to print t"e t"ird field of eac" line.
awk 9] if, Y4 b XPX 5 ] ASG#P#6 ^ else ] AS G# #6 ^ print Y3 ^9 filename
>. Crite a command to print t"e line number before eac" lineD
awk 9]print .&, Y4^9 filename
84. Crite a command to print t"e second and t"ird line of a file wit"out using .&.
awk 9('/%. ]&SG##6ASG#_n#^ ]print Y2,Y3^9 filename
88. How to create an alias for t"e comple command and remove t"e aliasD
!"e alias utility is used to create t"e alias for a command. !"e below command createsalias for ps Baef
command.
alias pgG9ps Baef9
%f you use pg, it will work t"e same way as ps Baef.
!o remove t"e alias simply use t"e unalias command as
unalias pg
82. Crite a command to display todays date in t"e format of 9yyyyBmmBdd9D
!"e date command can be used to display todays date wit" time
date 9Rc7BcmBcd9

Potrebbero piacerti anche