Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression

Similar presentations


Presentation on theme: "Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression"— Presentation transcript:

1 Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression Left, Right, Full Outer Joins LEFT ( ), RIGHT ( ), LTRIM ( ), RTRIM ( ) Functions UNION Queries

2 The IN ( ) Expression The IN ( ) Expression can work like a simplified OR test in a WHERE clause Try these two queries SELECT * FROM Products WHERE SupplierID IN (28, 18, 27) WHERE SupplierID = 28 OR SupplierID = 18 OR SupplierID = 27

3 Try these three queries
Subqueries with IN ( ) Try these three queries SELECT Products.* FROM Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID WHERE Country = 'France' SELECT SupplierID FROM Suppliers SELECT * FROM Products WHERE SupplierID IN ( WHERE Country = 'France') Subquery

4 Subqueries with IN ( ) (cont.)
Subqueries always execute first Subqueries may be nested Subqueries must return a single column Subquery return values must be compatible with those of the field being tested Compatible (same) value types SELECT * FROM Products WHERE SupplierID IN ( SELECT SupplierID FROM Suppliers WHERE Country = 'France') Single Column

5 Subquery Advantages—Efficiency
How are the executions of these two queries different? Display all products purchased by customer 'Around the Horn' in 1997 using nested subqueries SELECT Products.* FROM Products INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID WHERE Country = 'France' SELECT * FROM Products WHERE SupplierID IN ( SELECT SupplierID FROM Suppliers WHERE Country = 'France')

6 Subquery Advantages—Unary Relationships
Do you remember this? Write the SQL to retrieve the name of Robert King's supervisor SELECT Sup.FirstName, Sup.LastName FROM Employees Emp INNER JOIN Employees Sup ON Emp.ReportsTo = Sup.EmployeeID WHERE Emp.LastName = 'King' AND Emp.FirstName = 'Robert'

7 Subquery Advantages—Unary Relationship (cont.)
Rewrite as a subquery Use a subquery to find the names of all of the employees that Steven Buchanan supervises SELECT FirstName, LastName FROM Employees WHERE EmployeeID IN ( SELECT ReportsTo WHERE FirstName = 'Robert' AND LastName = 'King')

8 Subquery Advantages—No Direct Test
Write a query to find the company names of all customers who placed orders in May of 1997 Hint: Use a Subquery Now write a query to find the company names of all customers who did not place an order in May of 1997 SELECT CompanyName FROM Customers WHERE CustomerID IN ( SELECT DISTINCT CustomerID FROM Orders WHERE DatePart(mm, OrderDate) = 5 AND DatePart(yy, OrderDate) = 1997)

9 Subquery Advantages—No Direct Test (cont.)
Write a query to list the names of all products that are not discontinued that were not sold during May of 1997

10 Left (and Right) Outer Joins
You will often want to list all records in one table as well as related records in a second, even if there are no related records in the second table Execute the following query Write a query to show all category names along with matching product names How can we get our new category to show? INSERT INTO Categories ( CategoryName, Description) VALUES('Test Category', 'An artificial category added for testing')

11 Left (and Right) Outer Joins (cont.)
SELECT CategoryName, ProductName FROM Categories LEFT JOIN Products ON Categories.CategoryID = Products.CategoryID The query above returns all category names and matching product names if there is a matching product When there is no matching product a Null is returned All Category records are returned because Categories is on the Left in the LEFT JOIN expression Right JOIN would reverse the logic

12 Left (and Right) Outer Joins (cont.)
Run these two queries—What's wrong here? SELECT CompanyName, OrderDate FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID ORDER BY CompanyName, OrderDate WHERE DatePart(mm, OrderDate) = 5 AND DatePart(yy, OrderDate) = 1997

13 Left (and Right) Outer Joins (cont.)
Where clause type filters must be applied in a special way in outer joins Add the row-limiting criteria expressions as part of the join criteria Write a query to list all Employees along with orders they made in 1998 SELECT CompanyName, OrderDate FROM Customers LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID AND DatePart(mm, OrderDate) = 5 AND DatePart(yy, OrderDate) = 1997 ORDER BY CompanyName, OrderDate

14 The Full Outer Join SELECT Emp.LastName AS 'Emp Last', Emp.FirstName AS 'Emp First', Sup.LastName AS 'Sup Last', Sup.FirstName AS 'Sup First' FROM Employees Emp FULL JOIN Employees Sup ON Emp.ReportsTo = Sup.EmployeeID

15 Functions of the Day LEFT ( ) and RIGHT ( ) return a specified number of characters from the left or right sides of a string SELECT FirstName, Left(FirstName, 1) + '.' AS 'Initial' FROM Employees

16 Functions of the Day (cont.)
LTRIM and RTRIM remove Leading/Trailing spaces from a string When did I need to use this SQL? UPDATE LastName SET LastName = LTRIM(LastName)

17 Union Queries A Union Query allows you to append the results of two different queries and return them as two result sets Try this: SELECT CAST(SupplierID AS char(10)) AS 'ID', CompanyName, 'Supplier' AS 'Type' FROM Suppliers UNION SELECT CAST(CustomerID AS char(10)) AS 'ID', CompanyName, 'Customer' AS 'Type' FROM Customers

18 UNION Queries (cont.) All components queries of a Union query must return: The same number of columns Compatible column data types Use CAST function when necessary The same column names Use AS to rename columns when necessary UNION is particularly useful when combining current and archived (or other partitioned) data into one result set Write a query to return the names of all people in the Northwinds DB along with their phone numbers


Download ppt "Advanced Joins IN ( ) Expression Subqueries with IN ( ) Expression"

Similar presentations


Ads by Google