Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION."— Presentation transcript:

1 Getting to Know SQL

2 © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION operation

3 © Jim Hope 2004 All Rights Reserved Data Definition CREATE TABLE statement CREATE INDEX statement ALTER TABLE statement CONSTRAINT clause DROP statement SELECT... INTO statement

4 © Jim Hope 2004 All Rights Reserved 1 SELECT SELECT * FROM People ; Means: Select all the fields (*) for all rows from the table called People

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

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

7 © Jim Hope 2004 All Rights Reserved 3b Setting the Scope SELECT LastName, FirstName, Score FROM People WHERE Score >=250 OR Score <=100 Means: 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

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

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

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

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

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

13 © Jim Hope 2004 All Rights Reserved 5b Putting more things together SELECT LastName, FirstName, Score, City FROM People WHERE (Score >=290 or Score "Surrey" ORDER BY Score DESC What is this doing

14 © Jim Hope 2004 All Rights Reserved 5c Putting more things together SELECT LastName, FirstName, Score, City FROM People WHERE (Score >=290 or Score "Surrey" and City <> "Burnaby" ORDER BY Score DESC What is this doing, and what else would you add?

15 © 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 "New York") ORDER BY Score DESC What is this doing

16 © 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” OR LastName = “Petrie”

17 © Jim Hope 2004 All Rights Reserved 5f Whatnot SELECT LastName, FirstName, Score FROM People WHERE LastName NOT IN ("Bundy", "Simpson", "Petrie"); Try this one

18 © Jim Hope 2004 All Rights Reserved 6a Counting SELECT count(*) FROM People

19 © Jim Hope 2004 All Rights Reserved 6b Counting SELECT count(*) FROM People WHERE Score <100

20 © Jim Hope 2004 All Rights Reserved 6b Counting SELECT count(*) FROM People WHERE Score <100

21 © Jim Hope 2004 All Rights Reserved 7 Wildcards SELECT LastName, FirstName FROM People WHERE LastName like 'b*' or MS Access (WHERE LastName like 'b*')

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

23 © Jim Hope 2004 All Rights Reserved 9a Create an Alias with CONCAT SELECT CONCAT(LastName,", ",FirstName) AS FullName FROM PEOPLE ORDER BY LastName, FirstName Question: who is [Null]? --- alternate form (MS Access) SELECT LastName +", " + FirstName as FullName FROM People

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

25 © Jim Hope 2004 All Rights Reserved 10a Max & Min SELECT MAX (Score) FROM People SELECT MIN (Score) FROM People

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

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

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

29 © Jim Hope 2004 All Rights Reserved 11 Keeping things DISTINCT Try This… SELECT City FROM People Then Try SELECT DISTINCT City FROM People

30 © Jim Hope 2004 All Rights Reserved 12a More than one table Try SELECT `Team Name` FROM Team Then Try SELECT `Team Name`, LastName, FirstName FROM Team, People This creates a Cartesian Product!

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

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

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

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

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

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

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

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

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

40 © Jim Hope 2004 All Rights Reserved 16 SELECT and GROUP BY SELECT `Team Name`, avg(Score) as AvgScore FROM People, Team WHERE People.Team = Team.Team GROUP BY `Team Name`

41 © Jim Hope 2004 All Rights Reserved That’s enough of that


Download ppt "Getting to Know SQL. © Jim Hope 2004 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement UNION."

Similar presentations


Ads by Google