+ SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology...
-
Upload
kory-bryant -
Category
Documents
-
view
222 -
download
0
Transcript of + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology...
![Page 1: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/1.jpg)
+
SQL – Once More With Feeling
![Page 2: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/2.jpg)
+Review and Questions Topics from last lecture
Terminology
Questions?
![Page 3: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/3.jpg)
+Auto Increment
Key Word for MS SQL – Identity
Can specify at time of table creation – The easiest CREATE TABLE PlayerDetails(
PlayerID INT Primary Key Identity,
PlayerFirstName Char(30) Not Null,
…
);
Or alter tables to include Identity
![Page 4: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/4.jpg)
+Happy Valley Golf
![Page 5: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/5.jpg)
+Altering Player ID to include
-- PlayerID
Alter Table PlayerHandicapdrop constraint PLAYERID_FK;
Alter Table Player_Roundsdrop constraint PLAYERID2_FK;
Alter Table PlayerDetailsdrop constraint PLAYERID_PK;
Alter Table PlayerDetailsDrop column PlayerId;
Alter Table PlayerDetailsAdd PlayerID int identity;
Alter Table PlayerDetailsAdd constraint pk_PlayerIDprimary key(PlayerId);
Alter Table PlayerHandicapAdd Constraint PLAYERID_FKForeign Key (PlayerId)References PlayerDetails(PlayerId);
Alter Table Player_RoundsAdd Constraint PLAYERID2_FKForeign Key (PlayerId)References PlayerDetails(PlayerId);
![Page 6: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/6.jpg)
+Group By
The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.
How many rounds of Golf have been played?
How many rounds of Golf have been played at each golf course? (Just need the id for the course)
![Page 7: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/7.jpg)
+Group By
How many rounds of Golf have been played? SELECT Count(RoundId) AS NumberofRounds
FROM GolfRounds;
How many rounds of Golf have been played at each golf course? (just need id for the course)
SELECT CourseID, Count(RoundId) AS NumberofRounds
FROM GolfRounds
GROUP BY CourseID;
![Page 8: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/8.jpg)
+Having
Allows for the results to be displayed for only a selection of the results of the function (count, average, etc).
How many multiple rounds of Golf have been played at each golf course (display results for only courses that have more than 1 round)? (just need id for the course)
![Page 9: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/9.jpg)
+Having
How many multiple rounds of Golf have been played at each golf course (display results for only courses that have more than 1 round)? (just need id for the course)
SELECT CourseID, Count(RoundId) AS NumberofRounds
FROM GolfRounds
GROUP BY CourseID;
HAVING COUNT(RoundId) > 1;
![Page 10: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/10.jpg)
+One Step Farther
How many rounds of Golf have been played at each golf course and what golf course have they been played at (name of the course)?
SELECT CourseName, Count(RoundId) AS NumberofRounds
FROM GolfRounds AS GR , GolfCourses AS GC
WHERE GR.CourseID = GC.CourseID
GROUP BY GR.CourseID, CourseName;
![Page 11: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/11.jpg)
+Join
Needed when the results displayed come from multiple tables. If the results to display come from one table you can use a
subquery
![Page 12: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/12.jpg)
+Inner Joins
Returns the rows when there is at least one match in both tables. SELECT CourseName, Count(RoundId)
FROM GolfRounds AS GR , GolfCourses AS GC
WHERE GR.CourseID = GC.CourseID
GROUP BY GR.CourseID, CourseName;
![Page 13: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/13.jpg)
+Join … On
Will give the same results as the join query on the previous slide
SELECT CourseName, Count(RoundId)
FROM GolfRounds AS GR JOIN GolfCourses AS GC
ON GR.CourseID = GC.CourseID
GROUP BY GR.CourseID, CourseName;
![Page 14: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/14.jpg)
+Outer Join
Returns all rows from both the participating tables which satisfy the join condition along with rows which do not satisfy the join condition.
How many rounds of Golf have been played at each golf course and what golf course have they been played at (name of the course)? Display all golf courses even if there has been no rounds played.
SELECT CourseName, Count(RoundID)
FROM GolfRounds AS GR RIGHT JOIN GolfCourses AS GC
ON GR.CourseID = GC.CourseID
GROUP BY GR.CourseID, CourseName;
![Page 15: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/15.jpg)
+Outer Join
Can use left Join as well. SELECT CourseName, Count(RoundID)
FROM GolfCourses AS GC LEFT JOIN GolfRounds AS GR
ON GR.CourseID = GC.CourseID
GROUP BY GR.CourseID, CourseName;
![Page 16: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/16.jpg)
+Multiple Joins
You can use multiple tables together, not just two.
Show player’s names along with their average hole score and their handicap, for all players that have a handicap and have recorded round scores.
SELECT PlayerFirstName, PlayerLastName, Avg(HoleScore) AS AvgHoleScore, Avg(HandicapScore) AS AvgHandicapScore
FROM PlayerDetails AS PD, Player_Rounds AS PR, PlayerHandicap AS PH
WHERE PD.PlayerID = PR.PlayerIDAND PR.PlayerID = PH.PlayerIDGROUP BY PlayerLastName, PlayerFirstName
![Page 17: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/17.jpg)
+Multiple Joins .. Using JOIN…. ON
Show all player’s names along with their average hole score and their handicap
SELECT PlayerFirstName, PlayerLastName, Avg(HoleScore) AS AvgHoleScore, Avg(HandicapScore) AS AvgHandicapScore
FROM (PlayerDetails AS PD JOIN Player_Rounds AS PR ON PD.PlayerID = PR.PlayerID) JOIN
PlayerHandicap AS PH ON PR.PlayerID = PH.PlayerID
GROUP BY PlayerLastName, PlayerFirstName
![Page 18: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/18.jpg)
+Multiple Joins – Outer Join
Show all player’s names along with their average hole score and their handicap
SELECT PlayerFirstName, PlayerLastName, Avg(HoleScore) AS AvgHoleScore, Avg(HandicapScore) AS AvgHandicapScore
FROM (PlayerDetails AS PD LEFT JOIN Player_Rounds AS PR ON PD.PlayerID = PR.PlayerID)
LEFT JOIN PlayerHandicap AS PH ON PR.PlayerID = PH.PlayerID
GROUP BY PlayerLastName, PlayerFirstName
![Page 19: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/19.jpg)
+Practice
1.) Display each golf courses name, the average yards and par for holes at each course.
2.) Display each golf courses name, the average yards and par for holes at each course, and the number of holes played at the course. (Include all courses)
3.) Display each golf courses name, the average yards and par for holes at each course, and the number of rounds played at the course. (Include all courses)
![Page 20: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/20.jpg)
+Question 1
Display each golf courses name, the average yards and par for holes at each course.
SELECT CourseName, Avg(YardsForHole) AS AvgYardsForHole, Avg(ParForHole) AS ParForHole
FROM GolfCourses AS GC LEFT JOIN CourseHoleDetails AS CHD
ON GC.CourseID = CHD.CourseID
GROUP BY GC.CourseID, CourseName
![Page 21: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/21.jpg)
+Question 2
Display each golf courses name, the average yards and par for holes at each course, and the number of holes played at the course. (Include all courses) SELECT CourseName, Count(RoundId) AS NumberofHolesPlayer,
Avg(YardsForHole) AS AvgYardsForHole, Avg(ParForHole) AS ParForHoleFROM (GolfCourses AS GC LEFT JOIN GolfRounds AS GR ON GR.CourseID = GC.CourseID) RIGHT JOIN CourseHoleDetails AS CHD ON GC.CourseID = CHD.CourseIDGROUP BY GR.CourseID, CourseName;
![Page 22: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/22.jpg)
+Question 3
Display each golf courses name, the average yards and par for holes at each course, and the number of rounds played at the course. (Include all courses) SELECT CourseName, Count(DISTINCT RoundId) AS
NumberofHolesPlayer, Avg(YardsForHole) AS AvgYardsForHole, Avg(ParForHole) AS ParForHole
FROM (GolfCourses AS GC LEFT JOIN GolfRounds AS GR
ON GR.CourseID = GC.CourseID) RIGHT JOIN CourseHoleDetails AS CHD ON GC.CourseID = CHD.CourseIDGROUP BY GR.CourseID, CourseName;
![Page 23: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/23.jpg)
+
SQL Extra’s
![Page 24: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/24.jpg)
+Example – Inner Join
ID FName
1 Jim
2 Bob
3 Sally
4 Beth
ID LName
1 Smith
3 Jones
5 Adams
7 Zhu
TableA TableB
Select FName, LnameFrom TableA Join TableB on TableA.ID = TableB.ID
Inner Join ResultsFName LName
Jim Smith
Sally Jones
![Page 25: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/25.jpg)
+Example – Outer Join - Left
ID FName
1 Jim
2 Bob
3 Sally
4 Beth
ID LName
1 Smith
3 Jones
5 Adams
7 Zhu
TableA TableB
Select FName, LnameFrom TableA Left Join TableB on TableA.ID = TableB.ID
Outer Join Left ResultsFName LName
Jim Smith
Bob Null
Sally Jones
Beth Null
![Page 26: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/26.jpg)
+Example – Outer Join - Right
ID FName
1 Jim
2 Bob
3 Sally
4 Beth
ID LName
1 Smith
3 Jones
5 Adams
7 Zhu
TableA TableB
Select FName, LnameFrom TableA Right Join TableB on TableA.ID = TableB.ID
Outer Join ResultsFName LName
Jim Smith
Sally Jones
FName LName
Jim Smith
Sally Jones
Null Adams
Null Zhu
![Page 27: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/27.jpg)
+Question
How do we get results from both table?
![Page 28: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/28.jpg)
+Union
Combines the results of two or more queries into a single result set that includes all the rows that belong to all queries in the union
The UNION operation is different from using joins that combine columns from two tables.
The following are basic rules for combining the result sets of two queries by using UNION: The number and the order of the columns must be the same
in all queries. The data types must be compatible Column names for the final result set are taken from the first
query
![Page 29: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/29.jpg)
+Union / Union All - keywords
UNION allow you to join multiple datasets into one dataset
and will remove any duplicates that exist. Basically it is performing a DISTINCT operation across all columns in the result set.
UNION ALL allows you to join multiple datasets into one dataset,
but it does not remove any duplicate rows. Because this does not remove duplicate rows this process is faster, but if you don't want duplicate records you will need to use the UNION operator instead.
![Page 30: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/30.jpg)
+Example - Union
ID FName
1 Jim
2 Bob
3 Sally
4 Beth
ID LName
1 Smith
3 Jones
5 Adams
7 Zhu
TableA TableB
Select FName, LnameFrom TableA Left Join TableB on TableA.ID = TableB.IDUNIONSelect FName, LnameFrom TableA Right Join TableB on TableA.ID = TableB.ID
Union
Results
FName LName
Jim Smith
Sally Jones
FName LName
Jim Smith
Bob Null
Sally Jones
Beth Null
Null Adams
Null Zhu
![Page 31: + SQL – Once More With Feeling. + Review and Questions Topics from last lecture Terminology Questions?](https://reader036.fdocuments.in/reader036/viewer/2022081513/5697bf921a28abf838c8f307/html5/thumbnails/31.jpg)
+Example – Union All
ID FName
1 Jim
2 Bob
3 Sally
4 Beth
ID LName
1 Smith
3 Jones
5 Adams
7 Zhu
TableA TableB
Select FName, LnameFrom TableA Left Join TableB on TableA.ID = TableB.IDUNION ALLSelect FName, LnameFrom TableA Right Join TableB on TableA.ID = TableB.ID
Union
Results
FName LName
Jim Smith
Sally Jones
FName LName
Jim Smith
Bob Null
Sally Jones
Beth Null
Jim Smith
Sally Jones
Null Adams
Null Zhu