You are on page 1of 2

Birla Institute Of Technology & Science-Pilani,K.K.

Birla Goa Campus DBSA IS C332 MySQL-LAB6-Manual Sub Queries (More Operators) The example to be considered to demonstrate all the operators is given below.(which was already discussed in the class). People(PID,name,address,gender,age) Planet(Plname,colour,shade,Galaxy) Live(PID,Plname,migratedfrom,status) (A person can be migrated from one planet to another planet at any time. Depending on that the status will be living or shifted. By birth the value of migratedfrom for a given PID is NULL) Property(PrID,PID,Plname,type) GuestHouse(GID,No.of.Bed rooms) HaveaSite(SID,sqfts) Set Comparison In the last manual using the keyword IN we have seen set membership. Using the keywords some, all set comparison can be done. Select GID from GuestHouse where No.of.Bedrooms > some (select No.of.Bedrooms from GuestHouse,Property where GID=PrID and Plname='earth'); The > some comparison in the where clause of the outer select is true if the No.of.Bedrooms value of the tuple is greater than at least one member of the set of all No.of.Bedrooms for GuestHouse on the planet 'earth' . Select GID from GuestHouse where No.of.Bedrooms > all (select No.of.Bedrooms from GuestHouse,Property where GID=PrID and Plname='earth'); The > all comparison in the where clause of the outer select is true if the No.of.Bedrooms value of the tuple is greater than the set of all No.of.Bedrooms for GuestHouse on the planet 'earth' . Exists and Not Exists The exists construct returns the value true if the argument subquery is nonempty. Eg:Find PID's who lived and have property on the same planet. Select PID from Live l where exists (select PID from Property p where p.Plname=l.Plname) We can test for nonexistence of the tuples in a subquery by using the not exists construct Select PID from Live l where not exists (select PID from Property p where p.Plname=l.Plname)

Some more examples. Find the PID's whose site sqfts is more than the average of the all sites. select SID from HaveaSite where sqfts > ( select avg(sqfts) from HaveaSite ) Subqueries in DML command Eg: update Property set Plname='black' where PID = (select PID from People ) where type='G';

You might also like