Download presentation
Presentation is loading. Please wait.
Published byBuck Wheeler Modified over 9 years ago
1
Getting to Know SQL
2
© Jim Hope 2002 All Rights Reserved Data Manipulation SELECT statement INSERT INTO statement UPDATE statement DELETE statement TRANSFORM statement UNION operation
3
© Jim Hope 2002 All Rights Reserved Data Definition CREATE TABLE statement CREATE INDEX statement ALTER TABLE statement CONSTRAINT clause DROP statement SELECT... INTO statement
4
© Jim Hope 2002 All Rights Reserved Example 1 SELECT * FROM people ; Means: Select all the fields (*) for all rows from the table called people
5
© Jim Hope 2002 All Rights Reserved Example 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 2002 All Rights Reserved Example 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 2002 All Rights Reserved Example 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 2002 All Rights Reserved Example 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 2002 All Rights Reserved Example 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 2002 All Rights Reserved Example 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 2002 All Rights Reserved Example 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 duplicate scores presented alphabetically
12
© Jim Hope 2002 All Rights Reserved Example 5a Putting things together SELECT 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?
13
© Jim Hope 2002 All Rights Reserved Example 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, and what else would you add?
14
© Jim Hope 2002 All Rights Reserved Example 5c More Scoping 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”
15
© Jim Hope 2002 All Rights Reserved Example 5d Whatnot SELECT lastname, firstname, score FROM people WHERE lastname NOT IN ("Bundy", "Simpson", "Petrie"); Try this one
16
© Jim Hope 2002 All Rights Reserved Example 6a Counting SELECT count(*) FROM people
17
© Jim Hope 2002 All Rights Reserved Example 6b Counting SELECT count(*) FROM people WHERE score <100
18
© Jim Hope 2002 All Rights Reserved Example 7 Wildcards SELECT lastname, firstname FROM people WHERE lastname like 'b*‘ (WHERE lastname like 'b%‘)
19
© Jim Hope 2002 All Rights Reserved Example 8 You can do math? SELECT lastname, firstname, score, score +10 as bigscore FROM people ORDER BY score DESC
20
© Jim Hope 2002 All Rights Reserved Example 9 Create an Alias SELECT lastname +", " + firstname as fullname FROM people ORDER BY lastname, firstname
21
© Jim Hope 2002 All Rights Reserved Example 10a Max & Min SELECT max (score) FROM people SELECT min (score) FROM people
22
© Jim Hope 2002 All Rights Reserved Example 10b Max again SELECT lastname, firstname, score FROM people WHERE score = (SELECT max(score) FROM people); This is a subquery
23
© Jim Hope 2002 All Rights Reserved Example 11 Keeping things DISTINCT SELECT DISTINCT city FROM people;
24
© Jim Hope 2002 All Rights Reserved Example 12a More than one table SELECT lastname, firstname, score, [show name] FROM people, show ; This creates a Cartesian Product
25
© Jim Hope 2002 All Rights Reserved Example 12b More than one table SELECT lastname, firstname, score, [show name] FROM people, show WHERE people.show=show.show ;
26
© Jim Hope 2002 All Rights Reserved Example 12c Using Join SELECT lastname, firstname, score, [show name] FROM People INNER JOIN Show ON people.show = show.show ; FROM people, show WHERE people.show=show.show ;
27
© Jim Hope 2002 All Rights Reserved Example 12c Using Join SELECT lastname, firstname, score, [show name] FROM People INNER JOIN Show ON people.show = show.show ;
28
© Jim Hope 2002 All Rights Reserved Example 12d Using Left Join SELECT lastname, firstname, score, [show name] FROM People Left JOIN Show ON people.show = show.show ;
29
© Jim Hope 2002 All Rights Reserved Example 12e Using Right Join SELECT lastname, firstname, score, [show name] FROM People Right JOIN Show ON people.show = show.show ;
30
© Jim Hope 2002 All Rights Reserved That’s enough of that
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.