Download presentation
Presentation is loading. Please wait.
Published byJasper McGee Modified over 8 years ago
1
Unit 4 Queries and Joins
2
Key Concepts Using the SELECT statement Statement clauses Subqueries Multiple table statements Using table pseudonyms Inner joins Outer joins Cross joins Explicit joins Additional conditions Other joins
3
SELECT Statement SELECT (column list or expressions) FROM (table list) WHERE (filter and join conditions)
4
Using SELECT Statements Introduction to SQL, Figure 6.1, pg. 156
5
SELECT Statements SELECT clause is always required. SELECT and FROM clauses are required if using WHERE, GROUP BY, HAVING or ORDER BY. A HAVING clause requires a GROUP BY clause. Clause order is fixed: –SELECT clause must be first –ORDER BY clause (if used) must be last
6
Sample Introduction to SQL, Example 6.1, pg. 160
7
SELECT Processing Introduction to SQL, Figure 6.3, pg. 161
8
UNION Operator Put the two statement examples at the top of page 170 side-by-side on the slide.
9
Subquery Introduction to SQL, Example 6.6, pg. 171
10
Subquery (cont'd) Introduction to SQL, Example 6.9, pg. 176
11
Multiple Table Specification (Joining Tables) Introduction to SQL, Example 7.6, pg. 189.
12
Join Example Introduction to SQL, Example 7.7, pg. 191.
13
Table Pseudonyms Introduction to SQL, Example 7.8, pg. 194 and Example 7.12, pg. 198.
14
Required Pseudonyms Introduction to SQL, Example 7.14, pg. 199.
15
Explicit Join Introduction to SQL, Example 7.16, pg. 205
16
USING Keyword SELECT TEAMNO, NAME FROM TEAMS INNER JOIN PLAYERS USING (PLAYERNO)
17
Outer Join Left outer join returns all rows from the left table and qualifying rows from the right table. Right outer join returns all rows from the right table and qualifying rows from the left table. Full outer join returns all rows from both tables. Depending on the DBMS, A question mark (?) or NULL is used in the result set where there is no value returned. SQL Server returns a NULL.
18
Left Outer Join Introduction to SQL, Example 7.17, pg. 207.
19
Right Outer Join Introduction to SQL, Example 7.21, pg. 211.
20
Full Outer Join Introduction to SQL, Example 7.22, pg. 212.
21
Additional Conditions WHERE Clause Introduction to SQL, Example 7.23, pg. 216
22
Additional Conditions FROM Clause SELECT TEAMS.PLAYERNO, TEAMS.TEAMNO, PENALTIES.PAYMENTNO FROM TEAMS LEFT OUTER JOIN PENALTIES ON TEAMS.PLAYERNO = PENALTIES.PLAYERNO AND DIVISION = 'second'
23
Other Joins UNION join NATURAL join Equi join Theta join
24
Subquery Example SELECT Fname, Lname, DepartName FROM (SELECT E.EmployeeID, DepartName FROM Employee E, Department D WHERE E.Department = D.DepartmentID) AS SQ, Person P WHERE SQ.EmployeeID = P.EmployeeID ORDER BY Lname, Fname
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.