Download presentation
Presentation is loading. Please wait.
1
SQL conventions Writing SQL Statements: conventions vs. requirements We try to write SQL statements so they are readable. That is why we try to put SELECT, FROM, WHERE on newlines. Caps are not required for keywords. Table aliases are usually just a single letter. Fully qualified column names are required if ambiguous. Access will complain. SELECT * FROM Employee e INNER JOIN Timecard t ON e.ssn=t.ssn; is the same as: SELECT * FROM Employee e INNER JOIN Timecard t ON e.ssn=t.ssn;
2
More SQL (equi-)joins An equi-join is a join where the constraint is on two columns matching (or being equal). A natural join is an equi-join when the columns share the same name in both tables. Both are also called “normal joins” and the INNER JOIN keywords are used for both. But as seen you could also use a WHERE clause. This is by far the most common type of join used.
3
Inner Join A regular INNER JOIN in with SQL can be written as follows: SELECT * FROM Employee e, Timecard t WHERE e.ssn=t.ssn; or SELECT * FROM Employee e INNER JOIN Timecard t ON e.ssn=t.ssn; e.ssnlastNamefirstNamet.ssndatestartTimeendTimestoreIdpaid 145-09-0967UnoJane145-09-09671/14/20028:15:00 AM12:00:00 PM3Yes 145-09-0967UnoJane145-09-09671/16/20028:15:00 AM12:00:00 PM3Yes 245-11-4554ToulouseJie245-11-45541/14/20028:15:00 AM12:00:00 PM3Yes 376-77-0099ThreatAyisha376-77-00991/3/200210:00:00 AM2:00:00 PM5Yes 376-77-0099ThreatAyisha376-77-00992/23/20022:00:00 PM10:00:00 PM5Yes 376-77-0099ThreatAyisha376-77-00993/21/20023:00:00 PM7:00:00 PM5Yes 376-77-0099ThreatAyisha376-77-00991/3/20023:00:00 PM7:00:00 PM3Yes
4
Why use INNER JOIN SELECT COUNT(*) AS [Count of Cartesian] FROM Employee, Timecard; Count of Cartesian 35 With the INNER JOIN command an ON clause is required so you won’t accidentally get a cross- product instead of a join. This is very likely going to happen to you at some point if you don’t use INNER JOIN.
5
Other types of joins Self join is a table joined with itself. Recall lab 4 (office). An employee had a mentor. To get all the employees and their mentors we’d use a self-join SELECT e.SSN, e.FNAME, e.LNAME, e.MENTOR, m.FNAME FROM EMPLOYEE AS e INNER JOIN EMPLOYEE AS m ON e.MENTOR=m.SSN; SSNFNAMELNAMEMENTOR 111-22-3333BillHomer123-45-6789 JoeSmith 222-33-4444XenaCreek123-45-6789 SSNe.FNAMELNAMEMENTORm.FNAME 111-22-3333BillHomer123-45-6789Joe 222-33-4444XenaCreek123-45-6789Joe
6
Outer joins Notice that we only got 2 results. Why? Because Joe does not have a mentor. That is, the MENTOR field for Joe is NULL. Suppose we want to list all the employees, including those that don’t have a mentor. That is what an outer join is for. Rows in one table without matching rows in the other table can be included in the results. LEFT OUTER JOIN includes those in the left without matches RIGHT OUTER JOIN includes those in the right without matches FULL OUTER JOIN (both, but not in Access -> use union instead)
7
LEFT OUTER self-join SELECT e.SSN, e.FNAME, e.LNAME, e.MENTOR, m.FNAME FROM EMPLOYEE AS e LEFT JOIN EMPLOYEE AS m ON e.MENTOR=m.SSN; SSNe.FNAMELNAMEMENTORm.FNAME 123-45-6789JoeSmith 111-22-3333BillHomer123-45-6789Joe 222-33-4444XenaCreek123-45-6789Joe
8
RIGHT OUTER self-join SELECT e.SSN, e.FNAME, e.LNAME, e.MENTOR, m.FNAME FROM EMPLOYEE AS e RIGHT JOIN EMPLOYEE AS m ON e.MENTOR=m.SSN; SSNe.FNAMELNAMEMENTORm.FNAME Bill 111-22-3333BillHomer123-45-6789Joe 222-33-4444XenaCreek123-45-6789Joe Xena What the heck does this mean?
9
Cross Product e.SSNe.FNAMEe.LNAMEe.MENTORm.SSNm.FNAMEm.LNAMEm.MENTOR 123-45-6789JoeSmith123-45-6789JoeSmith 111-22-3333BillHomer123-45-6789 JoeSmith 222-33-4444XenaCreek123-45-6789 JoeSmith 123-45-6789JoeSmith111-22-3333BillHomer123-45-6789 111-22-3333BillHomer123-45-6789111-22-3333BillHomer123-45-6789 222-33-4444XenaCreek123-45-6789111-22-3333BillHomer123-45-6789 JoeSmith222-33-4444XenaCreek123-45-6789 111-22-3333BillHomer123-45-6789222-33-4444XenaCreek123-45-6789 222-33-4444XenaCreek123-45-6789222-33-4444XenaCreek123-45-6789 SELECT * FROM EMPLOYEE e, EMPLOYEE m;
10
ssnlastNamefirstName 145-09-0967UnoJane 245-11-4554ToulouseJie 376-77-0099ThreatAyisha 479-98-0098FortuneJulian 579-98-8778FivozinskyBruce EMPLOYEE ssndatestartTimeendTimestoreIdpaid 145-09-09671/14/20028:15:00 AM12:00:00 PM3Yes 145-09-09671/16/20028:15:00 AM12:00:00 PM3Yes 245-11-45541/14/20028:15:00 AM12:00:00 PM3Yes 376-77-00991/3/200210:00:00 AM2:00:00 PM5Yes 376-77-00991/3/20023:00:00 PM7:00:00 PM3Yes 376-77-00992/23/20022:00:00 PM10:00:00 PM5Yes 376-77-00993/21/20023:00:00 PM7:00:00 PM5Yes TIMECARD storeIdstreetcitystatezipcodemanager 32010 Liberty Rd.ApopkaFL34505145-09-0967 51004 N. Monroe St.ApopkaFL34506588-99-0093 STORE
11
Multi-table joins Let’s look at all the timecards for employees. SELECT e.firstName, e.lastName, t.date, t.startTime FROM Timecard t INNER JOIN Employee e ON t.ssn=e.ssn; firstNamelastNamedatestartTime JaneUno1/14/20028:15:00 AM JaneUno1/16/20028:15:00 AM JieToulouse1/14/20028:15:00 AM AyishaThreat1/3/200210:00:00 AM AyishaThreat2/23/20022:00:00 PM AyishaThreat3/21/20023:00:00 PM AyishaThreat1/3/20023:00:00 PM
12
3 table join SELECT e.firstName, e.lastName, t.date, t.startTime, s.street AS [Store Street], s.city AS [Store City] FROM Store s INNER JOIN (Timecard t INNER JOIN Employee e ON t.ssn=e.ssn) ON t.storeId=s.storeId; firstNamelastNamedatestartTimeStore StreetStore City JaneUno1/14/20028:15:00 AM2010 Liberty Rd.Apopka JaneUno1/16/20028:15:00 AM2010 Liberty Rd.Apopka JieToulouse1/14/20028:15:00 AM2010 Liberty Rd.Apopka AyishaThreat1/3/200210:00:00 AM1004 N. Monroe St.Apopka AyishaThreat2/23/20022:00:00 PM1004 N. Monroe St.Apopka AyishaThreat3/21/20023:00:00 PM1004 N. Monroe St.Apopka AyishaThreat1/3/20023:00:00 PM2010 Liberty Rd.Apopka
13
Arrangement of joins What happens if I rearrange the joins? SELECT e.firstName, e.lastName, t.date, t.startTime, s.street AS [Store Street], s.city AS [Store City] FROM Employee e INNER JOIN (Timecard t INNER JOIN Store s ON t.storeId=s.storeId) ON e.ssn=t.ssn; firstNamelastNamedatestartTimeStore StreetStore City JaneUno1/14/20028:15:00 AM2010 Liberty Rd.Apopka JaneUno1/16/20028:15:00 AM2010 Liberty Rd.Apopka JieToulouse1/14/20028:15:00 AM2010 Liberty Rd.Apopka AyishaThreat1/3/200210:00:00 AM1004 N. Monroe St.Apopka AyishaThreat2/23/20022:00:00 PM1004 N. Monroe St.Apopka AyishaThreat3/21/20023:00:00 PM1004 N. Monroe St.Apopka AyishaThreat1/3/20023:00:00 PM2010 Liberty Rd.Apopka
14
Non-equi (theta) joins We are not limited to doing equi-joins on foreign key – primary key columns. You can also do joins based on comparison operators >, >=, These are rare. SELECT s.storeId, w.ssn FROM Store s INNER JOIN WorksIn w ON s.storeId <> w.storeId; storeIdssn 3145-09-0967 3245-11-4554 3376-77-0099 5145-09-0967 5245-11-4554
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.