Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT...
-
Upload
harold-henderson -
Category
Documents
-
view
215 -
download
0
Transcript of Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT...
Getting to Know SQL
© Jim Hope 2004 All Rights Reserved
Data Manipulation• SELECT statement • INSERT INTO statement • UPDATE statement • DELETE statement • UNION operation
© Jim Hope 2004 All Rights Reserved
Data Definition• CREATE TABLE statement • CREATE INDEX statement • ALTER TABLE statement • CONSTRAINT clause • DROP statement • SELECT... INTO statement
© Jim Hope 2004 All Rights Reserved
1 SELECT• SELECT * • FROM People ;
Means: Select all the fields (*) for all rows from the table called People
© Jim Hope 2004 All Rights Reserved
2 Specify Fields• SELECT LastName, FirstName• FROM People ;
Means: Select the fields (LastName and FirstName) for all rows from the table called People
© Jim Hope 2004 All Rights Reserved
3a Setting the Scope• SELECT LastName, FirstName, Score• FROM People• WHERE Score >=250
Means: Select the fields (LastName, FirstName, Score) for only rows where the Score is greater than or equal to 250
© Jim Hope 2004 All Rights Reserved
3b Setting the Scope• SELECT LastName, FirstName, Score• FROM People• WHERE Score >=250 OR Score
<=100Means: Select the fields (LastName and
FirstName, Score) for only rows where the Score is greater than or equal to 250 or the Score is less than or equal to 100
© Jim Hope 2004 All Rights Reserved
4a Setting the Order• SELECT LastName, FirstName• FROM People• ORDER BY LastName ;
Means: Select the fields (LastName and FirstName) for all rows from the table called People, in alphabetical (ascending) order by the values in the LastName field.
© Jim Hope 2004 All Rights Reserved
4b Setting the Order• SELECT LastName, FirstName• FROM People• ORDER BY LastName, FirstName ;
Means: Select the fields (LastName and FirstName) for all rows from the table called People, in alphabetical (ascending) order by the values in the LastName field. If there are duplicates – use the FirstName (ascending)
© Jim Hope 2004 All Rights Reserved
4c Setting the Order• SELECT LastName, FirstName, Score• FROM People• ORDER BY Score DESC, LastName,
FirstName;Means: Select the fields (LastName and
FirstName) for all rows from the table called People, in (descending) order by the values in the Score field.
© Jim Hope 2004 All Rights Reserved
4d Setting the Order – you try• SELECT LastName, FirstName, Score• FROM People• ORDER BY Score DESC
What would you do if you wanted to see rows with duplicate Scores presented alphabetically by player
© Jim Hope 2004 All Rights Reserved
5a Putting things together• SELECT LastName, FirstName, Score• FROM People• WHERE Score >=290 or Score <=100• ORDER BY Score DESC
What is this doing?
© Jim Hope 2004 All Rights Reserved
5b Putting more things together• SELECT LastName, FirstName, Score,
City• FROM People• WHERE (Score >=290 or Score <=100)
and City <> "Surrey"• ORDER BY Score DESCWhat is this doing
© Jim Hope 2004 All Rights Reserved
5c Putting more things together• SELECT LastName, FirstName, Score, City• FROM People• WHERE
(Score >=290 or Score <=100) and City <> "Surrey"
• and City <> "Burnaby"• ORDER BY Score DESCWhat is this doing, and what else would you add?
© Jim Hope 2004 All Rights Reserved
5d Putting more things together• SELECT LastName, FirstName, Score, City• FROM People• WHERE
(Score >=290 and City <> "Surrey") or (Score <=100 and City <> "New York")
• ORDER BY Score DESCWhat is this doing
© Jim Hope 2004 All Rights Reserved
5e Scope with IN• SELECT LastName, FirstName, Score• FROM People• WHERE LastName
IN ("Bundy", "Simpson", "Petrie");
(much better than… • WHERE LastName = “Bundy” OR
LastName = “Simpson” ORLastName = “Petrie”
© Jim Hope 2004 All Rights Reserved
5f Whatnot• SELECT LastName, FirstName, Score• FROM People• WHERE LastName
NOT IN ("Bundy", "Simpson", "Petrie");• Try this one
© Jim Hope 2004 All Rights Reserved
6a Counting• SELECT count(*)• FROM People
© Jim Hope 2004 All Rights Reserved
6b Counting• SELECT count(*)• FROM People• WHERE Score <100
© Jim Hope 2004 All Rights Reserved
6b Counting• SELECT count(*)• FROM People• WHERE Score <100
© Jim Hope 2004 All Rights Reserved
7 Wildcards• SELECT LastName, FirstName• FROM People• WHERE LastName like 'b*'
or• MS Access
(WHERE LastName like 'b*')
© Jim Hope 2004 All Rights Reserved
8a You can do math?• SELECT LastName, FirstName,
Score, Score +10 as BigScore• FROM People• ORDER BY Score DESC
© Jim Hope 2004 All Rights Reserved
9a Create an Alias with CONCAT• SELECT
CONCAT(LastName,", ",FirstName) AS FullName
• FROM PEOPLE• ORDER BY LastName, FirstNameQuestion: who is [Null]?• --- alternate form (MS Access)• SELECT LastName +", " + FirstName as FullName• FROM People
© Jim Hope 2004 All Rights Reserved
9b Who was NULL?
• SELECT LastName, FirstName, CONCAT(LastName,", ",FirstName)
AS FullName• FROM PEOPLE• ORDER BY LastName, FirstName• This answers the question – who is [NULL] in the previous example
© Jim Hope 2004 All Rights Reserved
10a Max & Min• SELECT MAX (Score)• FROM People
• SELECT MIN (Score)• FROM People
© Jim Hope 2004 All Rights Reserved
10b Simple Stats and Aliases• SELECT • MIN(Score) as `Lowest Score`,• ROUND(AVG(Score),2) as Average, • MAX(Score) as `Highest Score`,• ROUND(STD(Score),2)
as `Standard Deviation`• FROM People
© Jim Hope 2004 All Rights Reserved
10c Limiting Rows Returned• SELECT LastName, FirstName, Score• FROM People• ORDER BY Score DESC• LIMIT 1
Who are we missing here?
© Jim Hope 2004 All Rights Reserved
10c Max again• SELECT LastName, FirstName, Score• FROM People• WHERE Score =
(SELECT MAX(Score) FROM People);
This is a Subquery Version 4.1 Alpha
© Jim Hope 2004 All Rights Reserved
11 Keeping things DISTINCTTry This…• SELECT City• FROM PeopleThen Try• SELECT DISTINCT City• FROM People
© Jim Hope 2004 All Rights Reserved
12a More than one tableTry • SELECT `Team Name` FROM TeamThen Try• SELECT `Team Name` ,
LastName, FirstNameFROM Team, People
This creates a Cartesian Product!
© Jim Hope 2004 All Rights Reserved
12b More than one table• SELECT `Team Name` , LastName,
FirstName • FROM Team, People• WHERE
Team.Team=People.Team• ORDER BY `Team Name` ,
LastName, FirstName
© Jim Hope 2004 All Rights Reserved
12c Using Join• SELECT `Team Name` , LastName,
FirstName • FROM
Team INNER JOIN People ON Team.Team=People.Team
• ORDER BY `Team Name` , LastName, FirstName
Last two lines no longer required• FROM People, Team• WHERE People.Team=Team.Team
© Jim Hope 2004 All Rights Reserved
12c Using Join• SELECT LastName,
FirstName, Score, `Show Name`
• FROM People INNER JOIN `Show` ON People.Show = Show.Show ;
© Jim Hope 2004 All Rights Reserved
12d Using Left Join• SELECT LastName,
FirstName, Score, `Show Name`
• FROM People Left JOIN `Show` ON People.Show = Show.Show ;
© Jim Hope 2004 All Rights Reserved
12e Using Right Join• SELECT LastName,
FirstName, Score, `Show Name`
• FROM People Right JOIN `Show` ON People.Show = Show.Show ;
© Jim Hope 2004 All Rights Reserved
13a Group By – putting it together• SELECT `Team Name`,
ROUND(AVG(Score),2) AS `Average`
• FROM People,Team WHERE People.Team=Team.Team
• GROUP BY Team.`Team Name`
© Jim Hope 2004 All Rights Reserved
13b Inner Join and Group By• SELECT `Team Name`,
ROUND(AVG(Score),2) AS `Average`
• FROM Team INNER JOIN People ON People.Team=Team.Team
• GROUP BY Team.`Team Name
© Jim Hope 2004 All Rights Reserved
14 SELECT INTO (new table)• SELECT Team.`Team Name`,
AVG(Score) AS `Average`• INTO TeamSummary• FROM People INNER JOIN Team ON
People.Team = Team.Team• GROUP BY Team.`Team Name`Sorry – you don’t have permissions to
do this on the netpub instance of MySQL
© Jim Hope 2004 All Rights Reserved
16 SELECT with IF• SELECT LastName, • IF(FirstName IS NULL, Description,
FirstName) as Salutation, Score • FROM People, Titles• WHERE People.t = Titles.Titles
© Jim Hope 2004 All Rights Reserved
That’s enough of that