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&2345 56 $&'A!' !A(L' SAL'S  SAL'-%* %!'/'&,  P&)*+$!-%* %!'/'&,  7'A& %!'/'&,  Quantity %!'/'&,  P&%$' %!'/'& 56

sqlqueriesinterviewquestions-120708121732-phpapp02

Embed Size (px)

Citation preview

Page 1: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 1/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&2345

56

$&'A!' !A(L' SAL'S

  SAL'-%* %!'/'&,

  P&)*+$!-%* %!'/'&,

  7'A& %!'/'&,

  Quantity %!'/'&,

  P&%$' %!'/'&

56

Page 2: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 2/86

INSERT INTO P&)*+$!S 1AL+'S 844, 9okia956

%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

Page 3: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 3/86

!"e products table contains t"e below data.

S'L'$! @ &)0 P&)*+$!S6

P&)*+$!-%* P&)*+$!-A0'

BBBBBBBBBBBBBBBBBBBBBBB

844 okia

244 %P"one

344 Samsung

!"e sales table contains t"e following data.

S'L'$! @ &)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

Page 4: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 4/86

: 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: irst 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&)*+$!-%*

Page 5: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 5/86

  )&*'& (7 S.7'A& *'S$

  5 Q+A-P&'1-7'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=

Page 6: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 6/86

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 years 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'

&)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-*%

&)0 P&)*+$!S P,

  SAL'S S

CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*

Page 7: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 7/86

5A

GROUP (7 P&)*+$!-A0'

HA1%/ 0%Q+A-*%5 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'

&)0 P&)*+$!S P

  L'! )+!'& )%

  SAL'S S

) P.P&)*+$!-%* G S.P&)*+$!-%*56

CH'&' S.Q+A!%!7 %S +LL

Page 8: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 8/86

P&)*+$!-A0'

BBBBBBBBBBBB

L/

Method2: +sing t"e )! % operator.

S'L'$! P.P&)*+$!-A0'

&)0 P&)*+$!S P

CH'&' P.P&)*+$!-%* )! %

  S'L'$! *%S!%$! P&)*+$!-%* &)0 SAL'S56

P&)*+$!-A0'

BBBBBBBBBBBB

L/

Method3: +sing t"e )! 'M%S!S operator.

S'L'$! P.P&)*+$!-A0'

&)0 P&)*+$!S P

Page 9: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 9/86

CH'&' )! 'M%S!S

  S'L'$! 8 &)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'

&)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

Page 10: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 10/86

Page 11: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 11/86

  PA&!%!%) (7 S.7'A&

  )&*'& (7 S.Q+A!%!7 *'S$

  5 &O

&)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

Page 12: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 12/86

Page 13: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 13/86

Page 14: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 14/86

  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

Page 15: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 15/86

Page 16: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 16/86

!"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!%[email protected]&%$'5

  )1'&PA&!%!%) (7 P.P&)*+$!-A0' 5 SAL'S-&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<

Page 17: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 17/86

Page 18: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 18/86

  S.Q+A!%!7,

  S.7'A&

&)0 P&)*+$!S P,

  SAL'S S

CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*5

5A

P%1)! 0AMQ+A!%!75 AS Q+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

&)0 P&)*+$!S P,

  SAL'S S

CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*5

/&)+P (7 P.P&)*+$!-A0'6

Page 19: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 19/86

!. Crite a query to find t"e number  of products sold in eac" yearD

Solution

!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

&)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

Solution

Page 20: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 20/86

SELECT L'1'L FROM DUAL CONNECT (7 L'1'LNG6

2. Crite a query to display only friday dates from an, 2444 to till nowD

Solution

S'L'$! $-*A!',

  !)-$HA&$-*A!',9*795

&)0

  S'L'$! !)-*A!'948BAB24449,9**B0)B777795RL'1'LB8 $-*A!'

  &)0 *+AL

  $)'$! (7 L'1'L NG

  S7S*A!' B !)-*A!'948BAB24449,9**B0)B777795R85

5

CH'&' !)-$HA&$-*A!',9*795 G 9&%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

Page 21: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 21/86

 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

Page 22: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 22/86

Solution

S'L'$! P&)*+$!S,

  &'P'A!

&)0 !,

  S'L'$! L'1'L L &)0 *+AL

  $)'$! (7 L'1'L NG S'L'$! 0AM&'P'A!5 &)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

'

Solution

Page 23: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 23/86

S'L'$! S+(S!&9S0%L'9,L'1'L,85 A

&)0 *+AL

$)'$! (7 L'1'L NGL'/!H9S0%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.

Solution8

S'L'$! S+(S!&*+0P9S0%L'95,8;5

&)0 *+AL6

Solution2

S'L'$! C0-$)$A!A5

&)0

S'L'$! AS$%%S+(S!&9S0%L'9,L'1'L,855 A

&)0 *+AL

$)'$! (7 L'1'L NGL'/!H9S0%L'95

56

Page 24: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 24/86

SQL Queries Interview Questions - Oracle Part

8. $onsider t"e following friends table as t"e source

ame, riend-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. or sam6 ram,K"on,viKay and anand are friends of friends. !"e

output s"ould look as

ame, riend-of-irend

BBBBBBBBBBBBBBBBBBBBBB

sam, ram

sam, K"on

sam, viKay

sam, anand

Page 25: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 25/86

Solution

S'L'$! f8.name,

  f2.friend-name as friend-of-friend

&)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, riend-of-riend

BBBBBBBBBBBBBBBBBBBBBB

sam, K"on

sam, viKay

sam, anand

Solution

S'L'$! f8.name,

  f2.friend-name as friend-of-friend

Page 26: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 26/86

&)0 friends f8,

  friends f2

CH'&' f8.name G 9sam9

 A* f8.friend-name G f2.name

 A* )! EXISTS

  S'L'$! 8 &)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>

, 3>4, 244>

Page 27: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 27/86

/, >>>, 2484

H, =84, 2484

%, >84, 2484

, 84>, 2484

L, 2<4, 2484

0, ;=4, 2484

Solution

S'L'$! products,

  quantity-sold,

  year 

&)0

  S'L'$! products,

  quantity-sold,

  year,

  rownum r 

  from t

Page 28: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 28/86

  )&*'& (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

Solution

S'L'$! products,

  quantity-sold,

  year 

&)0

  S'L'$! products,

  quantity-sold,

  year,

  row-number5 )1'&

  )&*'& (7 quantity-sold *'S$5 r 

  from t

5A

Page 29: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 29/86

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

Solution

S'L'$! products,

  quantity-sold,

  year 

&)0

  S'L'$! products,

  quantity-sold,

  year,

  row-number5 )1'&

  PA&!%!%) (7 year 

  )&*'& (7 quantity-sold *'S$5 r 

  from t

5A

CH'&' r NG ;6

Page 30: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 30/86

SQL Quer" Interview Questions - Part !Crite SQL queries for t"e below interview questions

8. Load t"e below products table into t"e target table.

$&'A!' !A(L' P&)*+$!S

  P&)*+$!-%* %!'/'&,

  P&)*+$!-A0' 1A&$HA&2345

56

%S'&! %!) P&)*+$!S 1AL+'S 844, 9okia956

%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

Page 31: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 31/86

S'L'$! @ &)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 are

• 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.

Solution

irst 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

Page 32: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 32/86

  P&)*+$!-%* %!'/'&,

  P&)*+$!-A0' 1A&$HA&2345,

  %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!'

&)0

S'L'$! P&)*+$!-%*,

 P&)*+$!-A0'

&)0 P&)*+$!S S

CH'&' )! 'M%S!S

Page 33: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 33/86

  S'L'$! 8

  &)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'!' &)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&2345

56

Page 34: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 34/86

%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/A%'956

%S'&! %!) $)!'!S 1AL+'S <,90A/A%'956

$)00%!6

S'L'$! @ &)0 $)!'!S6

$)!'!-%* $)!'!-!7P'

BBBBBBBBBBBBBBBBBBBBBBB

8 0)1%'

2 0)1%'

3 A+*%)

Page 35: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 35/86

: A+*%)

; 0A/A%'

< 0A/A%'

!"e requirements to load t"e target table are

• 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. irst 0)1%', second A+*%), !"ird

0A/A%' and again fourt" 0ovie.

Solution

irst 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&2345,

  P&%)&%!7 %!'/'&,

  L)A*-LA/ %!'/'&

56

%S'&! %!) $)!'!S-LOP 1AL+'S90)1%'9,8,856

Page 36: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 36/86

%S'&! %!) $)!'!S-LOP 1AL+'S9A+*%)9,2,456

%S'&! %!) $)!'!S-LOP 1AL+'S90A/A%'9,3,456

$)00%!6

S'L'$! @ &)0 $)!'!S-LOP6

$)!'!-!7P' P&%)&%!7 L)A*-LA/

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

0)1%' 8 8

AUDIO 2  4

0A/A%' 3 4

Here if L)A*-LA/ 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*-LA/ as 8. !"e ot"er content types will "ave L)A*-LA/ 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

Page 37: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 37/86

S'L'$! $)!'!-%*,

 $)!'!-!7P'

&)0 $)!'!S

CH'&' $)!'!-!7P' G S'L'$! $)!'!-!7P' &)0 $)!'!S-LOP CH'&'

L)A*-LA/G856

!"e last step is to update t"e L)A*-LA/ of t"e Lookup table.

+P*A!' $)!'!S-LOP

S'! L)A*-LA/ G 4

CH'&' L)A*-LA/ G 86

+P*A!' $)!'!S-LOP

S'! L)A*-LA/ G 8

CH'&' P&%)&%!7 G

S'L'$! *'$)*' P&%)&%!7,S'L'$! 0AMP&%)&%!75 &)0 $)!'!S-LOP5 ,8 , P&%)&%!7R85

&)0 $)!'!S-LOP

CH'&' $)!'!-!7P' G S'L'$! *%S!%$! $)!'!-!7P' &)0 !/!-$)!'!S5

56

Page 38: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 38/86

#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 regular epressions. 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"

Page 39: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 39/86

!"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

Page 40: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 40/86

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.

Page 41: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 41/86

Page 42: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 42/86

+ni $ommands Cit" E(a&$les

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

Page 43: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 43/86

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

Page 44: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 44/86

=. $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. inding 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

Page 45: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 45/86

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 nal"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

Page 46: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 46/86

%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

Page 47: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 47/86

%S'&! %!) SAL'S 1AL+'S 8:, 344, 244=, 8?, ?44456

%S'&! %!) SAL'S 1AL+'S 8;, 344, 244>, 8>, ?44456

$)00%!6

S'L'$! @ &)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

Page 48: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 48/86

> 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 etween ++re+ate and nal"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 $!

&)0 SAL'S

/&)+P (7 7'A&6

Page 49: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 49/86

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 $!

&)0 SAL'S6

Page 50: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 50/86

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

Page 51: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 51/86

= 244 2482 24 >444 3

rom 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 clausecontains 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 unctions.

1. Crite a SQL query using t"e analytic function to find t"e total salesQ+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+0Q+A!%!75 )1'& PA&!%!%) (7 P&)*+$!-%* 5 !)!-SAL'S

&)0 SAL'S6

P&)*+$!-%* Q+A!%!7 !)!-SAL'S

BBBBBBBBBBBBBBBBBBBBBBBBBBBBB

844 82 ?8

Page 52: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 52/86

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 salesQ+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.

Page 53: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 53/86

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+0Q+A!%!75 )1'& PA&!%!%) (7 P&)*+$!-%*

  )&*'& (7 Q+A!%!7 AS$

  &)CS +()+*'* P&'$'*%/5 $+0-SAL'S

&)0 SAL'S6

P&)*+$!-%* Q+A!%!7 $+0-SAL'S

BBBBBBBBBBBBBBBBBBBBBBBBBBBBB

844 = =

844 84 8=

844 82 34

844 8< :<

844 2; ?8

Page 54: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 54/86

Page 55: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 55/86

Page 56: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 56/86

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

Page 57: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 57/86

Page 58: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 58/86

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

&)0

S'L'$! P&)*+$!-%*,

  7'A&,

  Q+A!%!7,

  &)C-+0('&5 )1'&PA&!%!%) (7 P&)*+$!-%*

  )&*'& (7 Q+A!%!7 AS$5 0%-SAL'-&AO

&)0 SAL'S

Page 59: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 59/86

Page 60: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 60/86

• ourt" ormal orm !"ere s"ould not be any multiBvalued dependencies.

0ost databases will be in !"ird ormal orm

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.

•  Atomic !ransactions must be atomic. !ransactions must fail or succeed as a single unit.

• $onsistent !"e database must always be in consistent state. !"ere s"ould not be any partial

transactions

• %solation !"e c"anges made by a user s"ould be visible only to t"at user until t"e transaction is

committed.

• *urability )nce a transaction is committed, it s"ould be permanent and cannot be undone.

<. 'plain different storage models of )LAPD

• 0)LAP !"e data is stored in multiBdimensional cube. !"e storage is not in t"e relational database,

but in proprietary formats.

• &)LAP &)LAP relies on manipulating t"e data stored in t"e &*(0S for slicing and dicing

functionality.

• H)LAP H)LAP combines t"e advantages of bot" 0)LAP and &)LAP. or summary type

information, H)LAP leverages on cube tec"nology for faster performance. or 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.

Page 61: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 61/86

=. '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 @

&)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+ nal"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 ame Products

7ear product

Page 62: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 62/86

BBBBBBBBBBBBB

2484 A

2484 (

2484 $

2484 *

2488 M

2488 7

2488

Here, in t"e output we will concatenate t"e products in eac" year by a comma separator. !"e desired

output is

year product-list

BBBBBBBBBBBBBBBBBB

2484 A,(,$,*

2488 M,7,

L%S!A// analytic function in 88g&2

!"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,

Page 63: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 63/86

 L%S!A//product, 9,95 C%!H% /&)+P )&*'& (7 product5 AS product-list

&)0 products

/&)+P (7 year6

C0-$)$A! function

7ou cannot pass an eplicit delimiter to t"e C0-$)$A! function. %t uses comma as t"e string

separator.

S'L'$! year,

 wm-concatproduct5 AS product-list

&)0 products

/&)+P (7 year6

Pivot and 'n$ivot O$erators in Oracle 5ata)ase 11+

Pivot

!"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 ame Sales

customer-id product 'r(ce

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

8 A 84

8 ( 24

Page 64: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 64/86

Page 65: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 65/86

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'$! @

&)0 S'L'$! customer-id,product,price from sales5

pivot M0L sumprice5 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

+npivot

+npivot operator converts t"e columns into rows.

!able ame sales-rev

cutomer-id a-product b-product c-product

BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

8 84 24

2 34 :4 ;4

3 <4 ?4 =4

!able ame sales

customer-id product price

BBBBBBBBBBBBBBBBBBBBBBBBBBB

8 A 84

Page 66: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 66/86

Page 67: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 67/86

!able ame '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

Page 68: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 68/86

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 ineac" departmentD

S!'P8 irst 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

&)0 employees6

*ept-%d 'mp-Seq rn

BBBBBBBBBBBBBBBBBBBB

84 8 8

84 2 2

Page 69: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 69/86

84 3 3

84 ; :

84 < ;

84 = <

84 > ?

84 88 =

24 8 8

24 2 2

S!'P2 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

&)0 employees6

*ept-%d 'mp-Seq *ept-Split

BBBBBBBBBBBBBBBBBBBBBBBBBBB

Page 70: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 70/86

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!'P3 !"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

&)0

Page 71: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 71/86

Page 72: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 72/86

  S'L'$! 7'A&,

  S+0P&%$'5 !)!-SAL

  &)0 SAL'S

  /&)+P (7 7'A&

5 !

L'! )+!'& )%

  S'L'$! 7'A&,

  S+0P&%$'5 P&)*-84-SAL

  &)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

Page 73: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 73/86

  !H' P&%$'

  'LS' +LL

  '* 5 P&)*-84-SAL,

  S+0SAL'S5 !)!-SAL

&)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.

irst take a look at of your query, identify t"e redundant logic and t"en tune it.

2. L'! )+!'& )%, )! 'M%S!S, )! %

Some times you can rewrite 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'

&)0 P&)*+$!S P

 L'! )+!'& )%

 SAL'S S

) P.P&)*+$!-%* G S.P&)*+$!-%*5

Page 74: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 74/86

CH'&' S.SAL'-%* %S +LL6

!"e same query can be rewritten using )! 'M%S!S and )! % as

S'L'$! P.P&)*+$!-%*,

 P.P&)*+$!-A0'

&)0 P&)*+$!S P

CH'&' )! 'M%S!S

 

 S'L'$! 8

 &)0 SAL'S S

 CH'&' S.P&)*+$!-%* G P.P&)*+$!-%*56

S'L'$! P.P&)*+$!-%*,

 P.P&)*+$!-A0'

&)0 P&)*+$!S P

CH'&' P&)*+$!-%* )! %

Page 75: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 75/86

Page 76: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 76/86

 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+0P&%$'5

&)0 SAL'S S

CH'&' 'M%S!S

 

 S'L'$! 8

 &)0 P&)*+$!S P

 CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*56

/&)+P (7 S.P&)*+$!-%*6

S'L'$! S.P&)*+$!-%*,

 S+0P&%$'5

&)0 SAL'S S

CH'&' P&)*+$!-%* %

Page 77: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 77/86

 

 S'L'$! P&)*+$!-%*

 &)0 P&)*+$!S P

 56

/&)+P (7 S.P&)*+$!-%*6

. %'& )%, $)&&'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'

&)0 SAL'S S

 )%

 P&)*+$!S P

) S.P&)*+$!-%* G P.P&)*+$!-%*5

!"e above query can be rewritten wit" correlated query as

S'L'$! S.SAL'-%*,

Page 78: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 78/86

 S.P&)*+$!-%*,

 S'L'$! P&)*+$!-A0'

  &)0 P&)*+$!S P

 CH'&' P.P&)*+$!-%* G S.P&)*+$!-%*5

&)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

Page 79: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 79/86

:. 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 directoryDls 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

Page 80: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 80/86

!"e tail command can be used to display t"e last lines from a file.

tail B8 filename

 Alternative solutions are

sed Bn 9Y p9 filename

awk 9'*]print Y4^9 filename

To$ 'ni( Interview Questions - Part 28. 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 TABU

3. Crite a command to convert a string to %nitcap.

ec"o apple Z awk 9]print touppersubstrY8,8,855 tolowersubstrY8,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 outputs only matc"ed lines between two files.

 Koin Ba %n addition to t"e matc"ed lines, t"is will output unmatc"ed lines also.

Page 81: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 81/86

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 38. *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 fileDsed 9XWYX p9 N filename

84. 'plain iostat, vmstat and netstatD

• %ostat reports on terminal, disk and tape %X) activity.

• 1mstat reports on virtual memory statistics for processes, disk, tape and $P+ activity.

• etstat reports on t"e contents of network data structures.

To$ 'ni( Interview Questions - Part

Page 82: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 82/86

Page 83: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 83/86

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('/% ] foriG86iNG846iRR5 ]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 below

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

Page 84: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 84/86

<. 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

Page 85: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 85/86

>. Crite a command to replace t"e word #apple# wit" #apple5# in a fileD

sed sXappleX5X 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 #+L7# or #uly#, 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 9BH9 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 XX 5 ] SG##6 ^ else ] S G# #6 ^ print Y3 ^9 filename

Page 86: sqlqueriesinterviewquestions-120708121732-phpapp02

8/12/2019 sqlqueriesinterviewquestions-120708121732-phpapp02

http://slidepdf.com/reader/full/sqlqueriesinterviewquestions-120708121732-phpapp02 86/86

>. 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##6SG#_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 9R7BmBd9