Presentation is loading. Please wait.

Presentation is loading. Please wait.

SQL. Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science.

Similar presentations


Presentation on theme: "SQL. Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science."— Presentation transcript:

1 SQL

2 Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science HewlettProf. Maths SmithSL Maths PetersSL MathsJonesPL select course_nm from course where credit_hrs<=4 and staff_name in (select name from faculty where rank='Prof.'); 'What are the course names where the credit hours are four or less and staffed by professors?'

3 subquery first from faculty ScienceHewlettProf. Maths SmithSL MathsPetersSL Maths JonesPL where rank='Prof. Science HewlettProf. select name Hewlett

4 then outer query from course logic 3Smith semantics5Peters programming5 Jones physics3Hewlett where credit_hrs<=4 and staff_name in Hewlett physics 3Hewlett select course_nm physics result of sub query

5 Correlated Subqueries SELECT customer.name FROM customer WHERE EXISTS (SELECT city FROM SUPPLIER WHERE supplier.city=customer.city); Often a sub-query can be processed independently of the main query.In correlated sub-queries this is not possible. Processing of the sub-query relies on some value from the main query as above i.e.customer.city. Processing must start with the main query. FROM clause produces an intermediate table identical to CUSTOMER. For each row in this intermediate table the WHERE clause is executed. So there are probably multiple executions of the sub-query unless the intermediate table consists of a single row.

6 1.Sub-query FROM clause produces a table identical to SUPPLIER. 2. Sub-query WHERE clause picks out all rows satisfying the sub-query condition. 3. That condition uses the value of city in the current row of the main query being considered. 4. Sub-query SELECT clause projects CITY column to form final table for sub-query. 5. EXISTS returns TRUE if the final table from sub- query contains any values. FALSE otherwise. 6. If TRUE returned then main query SELECT clause chooses the current value of customer row to be included in final result. 7. SELECT projects from this row the customer.name. 8. If EXISTS returns FALSE current customer row not selected for final table. 9. Sub-query then executed for next row of customer table. SELECT customer.name FROM customer WHERE EXISTS (SELECT city FROM SUPPLIER WHERE supplier.city=customer.city;

7 Customer NameCity United BiscuitsNewcastle General Food OilsHull Tate and LyleYork Supplier Name City General Flour MillsCambridge Eastern Crops Hull RHMYork select customer.name from customer where exists (select city from supplier where supplier.city=customer.city); 'Find the customers that have a supplier in the same city.' Compares the first row in customer with all the rows in supplier. If one or more rows in supplier match the where condition the customer name from customer is placed in result table. None match where condition so- Partial Result Name

8 Then takes next row of customer and compares with all the rows in supplier- Second row of supplier matches condition so 'exists' returns true and customer.name selected for partial result. NameCity United Biscuits Newcastle General Food Oils Hull Tate and Lyle York Customer Supplier Name City General Flour Mills Cambridge Eastern Crops Hull RHM York select customer.name from customer where exists (select city from supplier where supplier.city=customer.city); Partial Result Name General Food Oils

9 Customer NameCity United BiscuitsNewcastle General Food OilsHull Tate and LyleYork Supplier NameCity General Flour MillsCambridge Eastern CropsHull RHMYork Result Name General Food Oils Tate and Lyle

10 IN, ALL, ANY IN Lists a set of values and tests if a value is or is not in the list. SELECT * FROM SUPPLIER WHERE PRICE IN (2.00, 3.00, 4.00); This list could be a set of column values returned from a sub-query. ALL/ANY ALL has the meaning 'every member of the set.'. So all can be used to test every value in a column against some condition. SELECT S_NAME FROM SUPPLIER WHERE PRICE < ALL (SELECT PRICE FROM SUPPLIER WHERE CITY='Leeds'); 'names of suppliers whose prices are lower than all the suppliers in Leeds'.

11 SELECT S_NAME FROM SUPPLIER WHERE PRICE< ANY (SELECT PRICE FROM SUPPLIER WHERE CITY='Leeds'); 'find all the suppliers with a price lower than any supplier in Leeds'.


Download ppt "SQL. Sub-query Course Course_nmcredit_hrsstaff_name logic3Smith semantics5Peters programming5 Jones physics3 Hewlett Faculty Faculty_nameNamerank Science."

Similar presentations


Ads by Google