Sie sind auf Seite 1von 2

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

Birla Goa Campus DBSA IS C332 MySQL-LAB5-Manual Sub Queries using IN keyword 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) What is a sub query? A Subquery is a query with another query.The outer query is called as main query and inner query is called as subquery. Observe the following: Ex1.Display the details of people who is having property on Earth . ANS: select * from People where PID IN (select PID from Property where Plname =Earth); Ex2.Display the property details of Samulya. ANS:select * from Property where PID IN (select PID from People where name='Samulya); In both the queries the keyword IN can be replaced by '=' Normally '=' is used for the attributes which can have unique value. i.e.the result can contain exactly one row or no row at all.When the result is expected to be multi rows then the keyword IN is used. Ex3.Find the properties which are not in 'Milki way' Galaxy. ANS: select * from Property where Plname NOT IN (select Plname from Planet where Galaxy='Milki way'); Nested Subqueries Ex1:Find the names of people who are living on Black planet. ANS:select name from People where PID IN (select PID from Live where Plname IN (select Plname from Planet where colour='black'));

Comparing more than one value Ex1:Find the living details such that they have property on the same planet. ANS: select * from Live where (PID,Plname ) IN (select PID,PLname from Property); Only the keyword IN is discussed here. More keywords will be introduced in the next lab manual.

Das könnte Ihnen auch gefallen