30
Getting to Know SQL

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

Embed Size (px)

Citation preview

Getting to Know SQL

© Jim Hope 2002 All Rights Reserved

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

© Jim Hope 2002 All Rights Reserved

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

© Jim Hope 2002 All Rights Reserved

Example 1SELECT *

FROM people ;

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

© Jim Hope 2002 All Rights Reserved

Example 2Specify FieldsSELECT lastname, firstname

FROM people ;

Means: Select the fields (lastname and firstname) for all rows from the table called people

© Jim Hope 2002 All Rights Reserved

Example 3aSetting the ScopeSELECT 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 2002 All Rights Reserved

Example 3bSetting the ScopeSELECT 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

© Jim Hope 2002 All Rights Reserved

Example 4aSetting the OrderSELECT 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 2002 All Rights Reserved

Example 4bSetting the OrderSELECT lastname, firstnameFROM peopleORDER 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 2002 All Rights Reserved

Example 4cSetting the OrderSELECT lastname, firstname, scoreFROM peopleORDER 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 2002 All Rights Reserved

Example 4dSetting the Order – you trySELECT lastname, firstname, score

FROM people

ORDER BY score DESC

What would you do if you wanted to see duplicate scores presented alphabetically

© Jim Hope 2002 All Rights Reserved

Example 5aPutting things togetherSELECT lastname, firstname, score,

FROM people

WHERE score >=290 or score <=100

ORDER BY score DESC

What is this doing, and what else would you add?

© Jim Hope 2002 All Rights Reserved

Example 5bPutting more things togetherSELECT lastname, firstname, score,city

FROM people

WHERE (score >=290 or score <=100) and city <> "Surrey"

ORDER BY score DESC

What is this doing, and what else would you add?

© Jim Hope 2002 All Rights Reserved

Example 5cMore Scoping with INSELECT lastname, firstname, scoreFROM peopleWHERE lastname IN ("Bundy", "Simpson", "Petrie");(much better than… WHERE lastname = “Bundy” OR

lastname = “Simpson” ORlastname = “Petrie”

© Jim Hope 2002 All Rights Reserved

Example 5dWhatnotSELECT lastname, firstname, score

FROM people

WHERE lastname

NOT IN ("Bundy", "Simpson", "Petrie");

Try this one

© Jim Hope 2002 All Rights Reserved

Example 6aCountingSELECT count(*)

FROM people

© Jim Hope 2002 All Rights Reserved

Example 6bCountingSELECT count(*)

FROM people

WHERE score <100

© Jim Hope 2002 All Rights Reserved

Example 7WildcardsSELECT lastname, firstname

FROM people

WHERE lastname like 'b*‘

(WHERE lastname like 'b%‘)

© Jim Hope 2002 All Rights Reserved

Example 8You can do math?SELECT lastname, firstname, score,

score +10 as bigscore

FROM people

ORDER BY score DESC

© Jim Hope 2002 All Rights Reserved

Example 9Create an AliasSELECT lastname +", " + firstname

as fullname

FROM people

ORDER BY lastname, firstname

© Jim Hope 2002 All Rights Reserved

Example 10aMax & MinSELECT max (score)

FROM people

SELECT min (score)

FROM people

© Jim Hope 2002 All Rights Reserved

Example 10bMax againSELECT lastname, firstname, score

FROM people

WHERE score = (SELECT max(score) FROM people);

This is a subquery

© Jim Hope 2002 All Rights Reserved

Example 11Keeping things DISTINCTSELECT DISTINCT city

FROM people;

© Jim Hope 2002 All Rights Reserved

Example 12aMore than one tableSELECT lastname, firstname, score,

[show name]

FROM people, show ;

This creates a Cartesian Product

© Jim Hope 2002 All Rights Reserved

Example 12bMore than one tableSELECT lastname, firstname, score,

[show name]

FROM people, show

WHERE people.show=show.show ;

© Jim Hope 2002 All Rights Reserved

Example 12cUsing JoinSELECT lastname, firstname, score,

[show name]

FROM People INNER JOIN Show ON people.show = show.show ;

FROM people, show

WHERE people.show=show.show ;

© Jim Hope 2002 All Rights Reserved

Example 12cUsing JoinSELECT lastname,

firstname, score, [show name]

FROM People INNER JOIN Show ON people.show = show.show ;

© Jim Hope 2002 All Rights Reserved

Example 12dUsing Left JoinSELECT lastname,

firstname, score, [show name]

FROM People Left JOIN Show ON people.show = show.show ;

© Jim Hope 2002 All Rights Reserved

Example 12eUsing Right JoinSELECT lastname,

firstname, score, [show name]

FROM People Right JOIN Show ON people.show = show.show ;

© Jim Hope 2002 All Rights Reserved

That’s enough of that