Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT...

40
Getting to Know SQL

Transcript of Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT...

Page 1: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

Getting to Know SQL

Page 2: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

Data Manipulation• SELECT statement • INSERT INTO statement • UPDATE statement • DELETE statement • UNION operation

Page 3: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

Data Definition• CREATE TABLE statement • CREATE INDEX statement • ALTER TABLE statement • CONSTRAINT clause • DROP statement • SELECT... INTO statement

Page 4: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

1 SELECT• SELECT * • FROM People ;

Means: Select all the fields (*) for all rows from the table called People

Page 5: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 6: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 7: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 8: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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.

Page 9: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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)

Page 10: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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.

Page 11: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 12: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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?

Page 13: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 14: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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?

Page 15: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 16: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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”

Page 17: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

5f Whatnot• SELECT LastName, FirstName, Score• FROM People• WHERE LastName

NOT IN ("Bundy", "Simpson", "Petrie");• Try this one

Page 18: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

6a Counting• SELECT count(*)• FROM People

Page 19: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

6b Counting• SELECT count(*)• FROM People• WHERE Score <100

Page 20: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

6b Counting• SELECT count(*)• FROM People• WHERE Score <100

Page 21: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

7 Wildcards• SELECT LastName, FirstName• FROM People• WHERE LastName like 'b*'

or• MS Access

(WHERE LastName like 'b*')

Page 22: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 23: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 24: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 25: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

10a Max & Min• SELECT MAX (Score)• FROM People

• SELECT MIN (Score)• FROM People

Page 26: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 27: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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?

Page 28: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 29: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

11 Keeping things DISTINCTTry This…• SELECT City• FROM PeopleThen Try• SELECT DISTINCT City• FROM People

Page 30: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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!

Page 31: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 32: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 33: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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 ;

Page 34: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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 ;

Page 35: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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 ;

Page 36: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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`

Page 37: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 38: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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
SELECT `Team Name`, avg(Score) as AvgScoreFROM People INNER JOIN team ON People.Team = team.Teamgroup by `Team Name`
Page 39: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© 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

Page 40: Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION.

© Jim Hope 2004 All Rights Reserved

That’s enough of that