sailors textbook

29
Tables Sailor Reserves

Upload: pradeep

Post on 17-Feb-2016

228 views

Category:

Documents


0 download

DESCRIPTION

it is used foe preparation for sailors examination

TRANSCRIPT

Page 1: Sailors textbook

Tables Sailor

Reserves

Page 2: Sailors textbook

Boats

Quries Q)Find the names oss sailors who reserved boats

- select s.sname from sailors s

where s.sid in(select r.sid from reserves r)

Q) find the names of sailors who have resvered boat no 103

- select s.sname from sailors s

where s.sid in ( select r.sid from reserves r where r.bid=103)

Page 3: Sailors textbook

Q) find all sailor whose raring is better than some sailor called Haratio

- select s.name from sailor s

where s.rating > any ( select s1.rating from sailor s1 where s1.name='Haratio')

Q) find the sailor name with highest rating

- select s.name from sailor s where s.rating >=all(select s1.rating from sailor s1)

Page 4: Sailors textbook

Q) find count of no of sailor

- select max (s.rating ) from sailors s

Q) find the avg of all sailor

- select avg(s.age) from sailor s

Q)Find name of sailors who are older than oldest sailor wih rating of 10

- select s.name from sailor s where s.age > some(select max(s1.age) from sailor s1 where s1.rating=10)

Q) find the max rating of sailors

Page 5: Sailors textbook

- select max(s.rating) from sailor s

Q) find no of different sailor

- select count(distinct s.name) from sailor s

Q) find names of sailors whose reseved boats

- select s.name from sailor s where exists(select r1.sid from reserves r1 where r1.sid=s.sid)

13/8/15

Q) Find the name of sailors who reserved atleast two boats

- select s.name from sailor s

where s.sid in(select r.sid from reserves r,reserves r1

where r.sid=r1.sid and r.sid=r1.sid)

Page 6: Sailors textbook

Q) Find the name of sailors who does not reserved a boat

- select s.name from sailor s

where s.sid not in(select r.sid from reserves r)

Q) Find avg age of each sailor rating whose age>20

select s.rating,avg(s.age)

from sailor s

where s.age>=20

group by s.rating

Page 7: Sailors textbook

Q) Find the avg age of sailors who are of voting age of each rating level that have atleast two

-select s.rating,avg(s.age)

from sailor s

where s.age>=18

group by s.rating

having count(*)>1

Page 8: Sailors textbook

20/08/2015

create table t1(a1 integer not null,a2 integer null)

desc t1

insert into t1 values(null,null)

- cannot insert NULL into ("IT07"."T1"."A1") 

insert into t1 values(101,null)

insert into t1 values(102,1)

insert into t1 values(104,null)

insert into t1 values(103,3)

insert into t1 values(105,3)

select * fro t1

A1 A2101  102 1103 3104  105 3

alter table t1 add primary key(a1)

Name Null? TypeA1 NOT NULL NUMBER(38)A2   NUMBER(38)

Page 9: Sailors textbook

alter table t1 add primary key(a2)

ERROR:- column contains NULL values; cannot alter to NOT NULL 

alter table t1 add primary key(a1)

- desc t1

Name Null? TypeA1 NOT NULL NUMBER(38)A2   NUMBER(38)

JOINS :--select * from sailor s left join reserves r on s.sid=r.sid

SID NAME RATING AGE SID BID DAY22 Dustin 7 45 22 101 10-OCT-98

Page 10: Sailors textbook

22 Dustin 7 45 22 102 10-OCT-9822 Dustin 7 45 22 103 10-AUG-9822 Dustin 7 45 22 104 10-JUL-9831 Lubber 8 55.5 31 102 11-OCT-9831 Lubber 8 55.5 31 103 11-JUN-9831 Lubber 8 55.5 31 104 11-DEC-9864 Haratio 7 35 64 101 09-JUN-9864 Haratio 7 35 64 102 09-AUG-9874 Haratio 9 35 74 103 09-AUG-9871 Zorba 9 35      85 Art 3 25.5      58 Rusty 10 35      32 Andy 8 25      

SID NAME RATING AGE SID BID DAY29 Brutus 1 33      95 Bob 3 63.5      

- select * from reserves r left join sailor s on r.sid=s.sid

SID BID DAY SID NAME RATING AGE22 104 10-JUL-98 22 Dustin 7 4522 103 10-AUG-98 22 Dustin 7 4522 102 10-OCT-98 22 Dustin 7 4522 101 10-OCT-98 22 Dustin 7 4531 104 11-DEC-98 31 Lubber 8 55.531 103 11-JUN-98 31 Lubber 8 55.531 102 11-OCT-98 31 Lubber 8 55.564 102 09-AUG-98 64 Haratio 7 3564 101 09-JUN-98 64 Haratio 7 3574 103 09-AUG-98 74 Haratio 9 35

- select * from sailor s right join reserves r on s.sid=r.sid

SID NAME RATING AGE SID BID DAY22 Dustin 7 45 22 104 10-JUL-9822 Dustin 7 45 22 103 10-AUG-9822 Dustin 7 45 22 102 10-OCT-9822 Dustin 7 45 22 101 10-OCT-98

Page 11: Sailors textbook

31 Lubber 8 55.5 31 104 11-DEC-9831 Lubber 8 55.5 31 103 11-JUN-9831 Lubber 8 55.5 31 102 11-OCT-9864 Haratio 7 35 64 102 09-AUG-9864 Haratio 7 35 64 101 09-JUN-9874 Haratio 9 35 74 103 09-AUG-98

- select * from reserves r right join sailor s on r.sid=s.sid

SID BID DAY SID NAME RATING AGE22 101 10-OCT-98 22 Dustin 7 4522 102 10-OCT-98 22 Dustin 7 4522 103 10-AUG-98 22 Dustin 7 4522 104 10-JUL-98 22 Dustin 7 4531 102 11-OCT-98 31 Lubber 8 55.531 103 11-JUN-98 31 Lubber 8 55.531 104 11-DEC-98 31 Lubber 8 55.564 101 09-JUN-98 64 Haratio 7 3564 102 09-AUG-98 64 Haratio 7 3574 103 09-AUG-98 74 Haratio 9 35

      71 Zorba 9 35      85 Art 3 25.5      58 Rusty 10 35      32 Andy 8 25

SID BID DAY SID NAME RATING AGE      29 Brutus 1 33      95 Bob 3 63.5

-select * from reserves r full join sailor s on s.sid=r.sid

SID BID DAY SID NAME RATING AGE22 104 10-JUL-98 22 Dustin 7 4522 103 10-AUG-98 22 Dustin 7 4522 102 10-OCT-98 22 Dustin 7 4522 101 10-OCT-98 22 Dustin 7 4531 104 11-DEC-98 31 Lubber 8 55.531 103 11-JUN-98 31 Lubber 8 55.531 102 11-OCT-98 31 Lubber 8 55.564 102 09-AUG-98 64 Haratio 7 3564 101 09-JUN-98 64 Haratio 7 3574 103 09-AUG-98 74 Haratio 9 35

      71 Zorba 9 35      85 Art 3 25.5

Page 12: Sailors textbook

      58 Rusty 10 35      32 Andy 8 25

SID BID DAY SID NAME RATING AGE      29 Brutus 1 33      95 Bob 3 63.5

-select * from sailor r full join reserves s on s.sid=r.sid

SID NAME RATING AGE SID BID DAY22 Dustin 7 45 22 101 10-OCT-9822 Dustin 7 45 22 102 10-OCT-9822 Dustin 7 45 22 103 10-AUG-9822 Dustin 7 45 22 104 10-JUL-9831 Lubber 8 55.5 31 102 11-OCT-9831 Lubber 8 55.5 31 103 11-JUN-9831 Lubber 8 55.5 31 104 11-DEC-9864 Haratio 7 35 64 101 09-JUN-9864 Haratio 7 35 64 102 09-AUG-9874 Haratio 9 35 74 103 09-AUG-9871 Zorba 9 35      85 Art 3 25.5      58 Rusty 10 35      32 Andy 8 25      

SID NAME RATING AGE SID BID DAY29 Brutus 1 33      95 Bob 3 63.5      

VIEWS create view s1

as select s.sid,s.rating

from sailor s

where s.sid>23

select * from s1

SID RATING29 1

Page 13: Sailors textbook

31 832 858 1064 771 974 985 395 3

- insert into s1 values(234,0)

SID RATING29 131 832 858 1064 771 974 985 395 3

234 0

NOTE:- commit will logout else u can find in s1 table ,new inserted values

Page 14: Sailors textbook

-create view s10 (sailorsid,sailorrating) as select s.sid,s.rating from sailor s

SAILORSID SAILORRATING

22 729 131 832 858 1064 771 974 985 395 3

234 0

10/9/15UPPER & LOWER

-select lower(name) "lowwer" from sailor

lowwerDustinBrutusLubberAndyRustyHaratioZorbaHaratioArtBob 

Page 15: Sailors textbook

11 rows selected.

-select upper(name) "upper" from sailor

upperDUSTINBRUTUSLUBBERANDYRUSTYHARATIOZORBAHARATIOARTBOB 

11 rows selected.

-select upper(bname) "upper" from boats

upperINTERLAKEMARINEINTERLAKECILPER

-select lower(bname) "lowwer" from boats

lowwerInterlakeMarineInterlakeCilper

-select lower(color) "lowwer" from boats

Page 16: Sailors textbook

lowwer

BlueRedRedGreen

-select upper(color) "upper" from boatsupper

BLUEREDREDGREEN

INITCAP:-

-select initcap(name) "initcap" from sailorinitcap

DustinBrutusLubberAndyRustyHaratioZorbaHaratioArtBob 

11 rows selected.

Page 17: Sailors textbook

-select initcap(bname) "initcap" from boatsinitcap

InterlakeMarineInterlakeCilper

-select initcap(color) "initcap" from boatsinitcap

BlueRedRedGreen

SUBSTRING:-

-select substr(name,2,4) "example" from sailorexam

ustirutuUbbeNdyUstyAratOrbaAratRtOb 

11 rows selected.

Page 18: Sailors textbook

-select substr(bname,2,4) "example" from boats

ExamNterArinNterIlpe

-select DISTINCT substr('haratio',1,5) "example" from sailor

exampharat

-select DISTINCT substr('interlake',1,5) "example" from boats

exampinter

-select DISTINCT substr('interlake',-5,5) "example" from boats

exampRlake

ASCII

Page 19: Sailors textbook

select distinct ascii('haratio') from sailorASCII('HARATIO')

104

LTRIM

select distinct ltrim(name,'H') from sailorLTRIM(NAME,'H')

AndyArtBobBrutusDustinLubberRustyZorbaAratio 

RTRIM

select distinct rtrim('art','t') "rtrim" from sailor rt

Ar

TRIM

select trim(both 'H' from name) "rtrim" from sailor

rtrimDustinBrutusLubberAndyRusty

Page 20: Sailors textbook

AratioZorbaAratioArtBob 

11 rows selected.

SYSTEM DATE

select distinct sysdate from sailorSYSDATE

10-SEP-15

select distinct sysdate from reservesSYSDATE

10-SEP-15

select distinct sysdate from boatsSYSDATE

10-SEP-15

Page 21: Sailors textbook

NEXT_DAY

select distinct next_day('09-oct-98','monday') "DAY" from reserves

DAY12-OCT-98

MONTHS_BETWWEN

select distinct months_between('10-oct-98','10-sep-98') "DAY" from reserves

DAY1

Page 22: Sailors textbook

CONCAT

select distinct concat('sailor:',name)||concat('sid:',sid) "DAY" from sailor

DAYsailor:Andy sid:32sailor:Art sid:85sailor:Bob sid:95sailor:Brutus sid:29sailor:Dustin sid:22sailor:Haratio sid:64sailor:Haratio sid:74sailor:Lubber sid:31sailor:Rusty sid:58sailor:Zorba sid:71sailor:sid:234

-select distinct concat('sailor ','dustin')||concat(' reserved a boat on ',day) "DAY" from sailor,reserves

DAYsailor dustin reserved a boat on 09-AUG-98sailor dustin reserved a boat on 09-JUN-98sailor dustin reserved a boat on 10-AUG-98sailor dustin reserved a boat on 10-JUL-98sailor dustin reserved a boat on 10-OCT-98sailor dustin reserved a boat on 11-DEC-98sailor dustin reserved a boat on 11-JUN-98sailor dustin reserved a boat on 11-OCT-98

Page 23: Sailors textbook

- select distinct concat('sailor ',s.name)||concat(' reserved a boat number ',r.bid)|| concat(' of color ',b.color)||concat('on day ',r.day) "DAY" from sailor s,boats b,reserves r where s.sid=r.sid group by concat('sailor ',s.name)||concat(' reserved a boat number ',r.bid)|| concat(' of color ',b.color)||concat('on day ',r.day) having count(r.sid)>1

DAYsailor Dustin reserved a boat number 101 of color Red on day 10-OCT-98sailor Dustin reserved a boat number 102 of color Red on day 10-OCT-98sailor Dustin reserved a boat number 103 of color Red on day 10-AUG-98sailor Dustin reserved a boat number 104 of color Red on day 10-JUL-98sailor Haratio reserved a boat number 101 of color Red on day 09-JUN-98sailor Haratio reserved a boat number 102 of color Red on day 09-AUG-98sailor Haratio reserved a boat number 103 of color Red on day 09-AUG-98sailor Lubber reserved a boat number 102 of color Red on day 11-OCT-98sailor Lubber reserved a boat number 103 of color Red on day 11-JUN-98sailor Lubber reserved a boat number 104 of color Red on day 11-DEC-98