Presentation is loading. Please wait.

Presentation is loading. Please wait.

JOINS cis 407 Subqueries Correlated Subqueries Exists operator performance considerations Examples.

Similar presentations


Presentation on theme: "JOINS cis 407 Subqueries Correlated Subqueries Exists operator performance considerations Examples."— Presentation transcript:

1 JOINS cis 407 Subqueries Correlated Subqueries Exists operator performance considerations Examples

2 Nested subquery Loose query syntax Select from where = (select from where )

3 Nested Subquery Select from where IN (select from where ) Find the product IDs for items sold on the first day Select DISTINCT o.orderDate, od.ProductID from orders o join [order details] od on o.orderID = od.orderid where orderDate = ‘7/4/1996’ – first order date in system –But bit of a kludge so…

4 Nested Subquery Select DISTINCT o.orderDate, od.ProductID from orders o join [order details] od on o.orderID = od.orderid where orderDate = (select min(orderdate) from orders)

5 Nested subquery w/ multiple values Use pubs – find all the stores that have discount records Select stor_id as ‘store id”, stor_name as “store name” from stores where stor_id in (select stor_id from discounts) Same as: select s.stor_id, sotr_name from stores s join discounts d on s.stor_id = d.stor_id

6 Not in and outerjoin Find all the stores that don’t have matching discount records Select s.stor_name from discounts d right outerjoin stores s on d.stor_id = s.stor_id where d.stor_id is null Select stor_id, stor_name from stores where stor_id not in (select stor_id from discounts where stor_id in not null)

7 Correlated subqueries PAY ATTENTION – Can be tricky Orderid & orderDate for first order in the system FOR EACH CUSTOMER Select o1.customerid, o.ordeid, o1.orderdate from orders o1 where o1.orderdate = (select min(o2.orderdate) from orders o2 where o2.customerid = o1.customerid)

8 Correlated subqueries Same query but want to add customer name Select cu.companyName, (select min(orderdate) from orders o where o.customerid = cu.customerid) as “order date” from customers cu

9 ISNULL function Isnull function accepts a variable or expression and tests it for a null value Orderid/orderDate for first order in the system FOR EACH CUSTOMER NAME Select cu.companyName, ISNULL(CAST ((select min(o.orderdate) from orders o where o.customerid = u.customerid) as varchar), ‘ never ordered’) from customers cu

10 Exists operator Customers that placed at least one order Select customerid, companyName from customers cu where exists (select orderid from orders o where o.customerid = cu.customerid) Select distinct cu.customerid, cu.companyname from customers cu join orders o on cu.customerid = o.customerid But top one does not require full row by row join

11 Customers who have not ordered anything Use northwind Select c.customerid, companyname from customers c left outerjoin orders o on c.customerid = o.customerid where o.customerid is null Select customerid, companyname from customers cu where not exists (select orderid from orders o where o.customerid = cu.customerid)

12 Other exist uses If exists (select * from sysobjects where id = object_id(N’[dbo.[shippers]’) and objectProperty(id,n’isusertable’) = 1) drop table [dbo].[shppers]

13 Query performance Page 204-205 How things USUALLY work When in doubt – or even when your not! TEST!!!!!!!!

14 Math functions Pg 603 Note ceiling and floor

15 Metadata functions Functions about the database and DB objects Col_length, col_name, … Pg 608

16 Security Functions Pg 620 Has_DBaccess, is_member, is_sbvrolemember, suser_id, suser_name, suser_SID, user, user_id,

17 String Functions Pg 623 Ascii, char, charindex, differnce, left, len, lower, ltrim. Nchar, parindex, quotename, replace, replicate, reverse, right, rtrim, soundex, space, str, stuff, substring, nicode, upper

18 System functions Pg 628 App_name, case, cast and convert, coalesce, collationproperty, current_timestamp, current_user, datalength, formatmessage, getansinull, host_id, host_name, ident_current, ident_incr, ident_seed, identity, isdate, isnull, isnumeric, newid, nullif, parsename, permissions, rowcount_big, scope_property, session_user, sessionproperty, stats_date, system_user, user_name

19 Aggregate functions Pg 597 appendix B Avg, checksum, checksum=agg, count, count_big, grouping, max, min, stdev, stdevp, sum, var, varp

20 Date time functions Dateadd, Datediff, Datename, Datepart Day, Getdate, getUTCdate, month year See details pg 601 appendix B


Download ppt "JOINS cis 407 Subqueries Correlated Subqueries Exists operator performance considerations Examples."

Similar presentations


Ads by Google