57
SQL by Example 1

SQL by Example

  • Upload
    braden

  • View
    36

  • Download
    0

Embed Size (px)

DESCRIPTION

SQL by Example. SQL: What is it?. SQL stands for Structured Query Language . It was originally developed in the early 1970s by IBM as a way to manipulate and retrieve data stored in IBM’s relational DBMS, System R. It can be pronounced “Sequel” or “S-Q-L”. - PowerPoint PPT Presentation

Citation preview

Page 1: SQL by Example

1

SQL by Example

Page 2: SQL by Example

2

SQL: What is it?

• SQL stands for Structured Query Language.• It was originally developed in the early 1970s

by IBM as a way to manipulate and retrieve data stored in IBM’s relational DBMS, System R.

• It can be pronounced “Sequel” or “S-Q-L”.• With some variations, SQL is the standard

query language for relational databases.

Page 3: SQL by Example

3

When do we use SQL?

• SQL is most commonly used to retrieve data from a database.

• It is also commonly used to modify data.• SQL also contains commands for creating

tables and other database objects, and for maintaining data security through granting and denying privileges to users.

Page 4: SQL by Example

4

SQL is Intuitive

• To show how easy it is to learn SQL, we’ll start out simply using examples.

• We’ll review what you see in the examples in upcoming lectures.

Page 5: SQL by Example

5

Players Table

• SELECT LastName FROM Players WHERE PlayerID = 8• SELECT Position FROM Players WHERE LastName =

‘Schrute’• SELECT Age FROM Players WHERE FirstName =

‘Dwight’

Page 6: SQL by Example

6

Answers

• SELECT LastName FROM Players WHERE PlayerID = 8– Jennings

• SELECT Position FROM Players WHERE LastName = ‘Schrute’– Left Field

• SELECT Age FROM Players WHERE FirstName = ‘Dwight’– 27

Page 7: SQL by Example

7

Trick Question

• SELECT PlayerID FROM Players WHERE LastName = ‘Johnson’

Page 8: SQL by Example

8

• SELECT PlayerID FROM Players WHERE LastName = ‘Johnson’

Query1PlayerID

1517

Page 9: SQL by Example

9

• SELECT PlayerID FROM Players WHERE Position = 'AH'

Page 10: SQL by Example

10

• SELECT PlayerID• FROM Players WHERE Position = 'AH'

Query1PlayerID

11172231

Page 11: SQL by Example

11

• SELECT FirstName, LastName, Age FROM Players WHERE Position = 'Pitcher'

Page 12: SQL by Example

12

• SELECT FirstName, LastName, Age FROM Players WHERE Position = 'Pitcher'

Query1FirstName LastName AgeWalter Williams 40Derrick Markel 28Tim Ford 29

Page 13: SQL by Example

13

• SELECT * FROM Players WHERE PlayerID = 1

Page 14: SQL by Example

14

• SELECT * FROM Players WHERE PlayerID = 1

Query1PlayerID LastName FirstName Age Position PhoneNumber TeamID

1 Cage Nick 25 1st Base (734)431-9880 1

Page 15: SQL by Example

15

• SELECT * FROM Players WHERE Position = 'Catcher'

Page 16: SQL by Example

16

• SELECT * FROM Players WHERE Position = 'Catcher'

Query1PlayerID LastName FirstName Age Position PhoneNumber TeamID

10 Gomez Felipe 36 Catcher (734)697-4356 116 Sherman Mike 28 Catcher (734)607-0436 234 Malone Kevin 26 Catcher (734)695-9019 3

Page 17: SQL by Example

17

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE Age < 22

Page 18: SQL by Example

18

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE Age < 22

Query1PlayerID FirstName LastName Age

8 Greg Jennings 2130 Hector Gonzales 21

Page 19: SQL by Example

19

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND Age>35

Page 20: SQL by Example

20

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND Age>35

Query1PlayerID FirstName LastName Age

7 Walter Williams 4010 Felipe Gomez 3611 Chris Swanson 36

Page 21: SQL by Example

21

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND Age>37 OR Position='1st Base'

Page 22: SQL by Example

22

Query1PlayerID FirstName LastName Age

1 Nick Cage 257 Walter Williams 40

21 Joey Szarek 3725 Justin Fleeson 33

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND Age>37 OR Position='1st Base'

Page 23: SQL by Example

23

Elements Table

SELECT symbol FROM elements WHERE grp=13 AND period=4

Page 24: SQL by Example

24

SELECT symbol FROM elements WHERE grp=13 AND period=4

Query1

symbol grp periodGa 13 4

Page 25: SQL by Example

25

SELECT symbol FROM elements WHERE grp=13 OR period=4

Page 26: SQL by Example

26

• SELECT symbol,grp,period FROM elements WHERE grp=13 OR period=4

Query1symbol grp periodAl 13 3As 15 4B 13 2Br 17 4Ca 2 4Co 9 4Cr 6 4Cu 11 4Fe 8 4Ga 13 4Ge 14 4In 13 5K 1 4Kr 18 4Mn 7 4Ni 10 4Sc 3 4Se 16 4Ti 4 4Tl 13 6Uut 13 7V 5 4Zn 12 4

Page 27: SQL by Example

27

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND (Age>37 OR Position='1st Base') ORDER BY Age

Page 28: SQL by Example

28

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND (Age>37 OR Position='1st Base') ORDER BY Age

Query1PlayerID FirstName LastName Age

1 Nick Cage 257 Walter Williams 40

Page 29: SQL by Example

29

You are the DBMS

• In these exercises, you are acting like the DBMS.

• All major DBMS’s have SQL interpreters.• You can submit queries like these to a

database and the DBMS will find the matching results for you.

Page 30: SQL by Example

30

Trick Question!

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND (Age>37 AND Position='1st Base') ORDER BY Age

Page 31: SQL by Example

31

• SELECT PlayerID, FirstName, LastName, Age FROM Players WHERE TeamID=1 AND (Age>37 AND Position='1st Base') ORDER BY Age

Page 32: SQL by Example

32

• SELECT * FROM Beings WHERE BirthYear>17

Page 33: SQL by Example

33

Query1BeingID BeingName HomePlanetID BirthYear

6 Obiwan 3 1912 Ohuru 1 1913 Scott 5 19

• SELECT * FROM Beings WHERE BirthYear>17

Page 34: SQL by Example

34

• SELECT * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear

Page 35: SQL by Example

35

Query1BeingID BeingName HomePlanetID BirthYear

15 Armstrong 1 220 Ilya 1 416 Fred 1 5

1 Luke 1 122 Leia 1 17

12 Ohuru 1 19

• SELECT * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear

Page 36: SQL by Example

36

• SELECT * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear ASC

Page 37: SQL by Example

37

Query1BeingID BeingName HomePlanetID BirthYear

15 Armstrong 1 220 Ilya 1 416 Fred 1 5

1 Luke 1 122 Leia 1 17

12 Ohuru 1 19

• SELECT * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear ASC

Page 38: SQL by Example

38

SELECT * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear DESC

Page 39: SQL by Example

39

Query1BeingID BeingName HomePlanetID BirthYear

12 Ohuru 1 192 Leia 1 171 Luke 1 12

16 Fred 1 520 Ilya 1 415 Armstrong 1 2

SELECT * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear DESC

Page 40: SQL by Example

40

SELECT * FROM Beings WHERE BirthYear BETWEEN 11 AND 15

Page 41: SQL by Example

41

SELECT * FROM Beings WHERE BirthYear BETWEEN 11 AND 15

Query1BeingID BeingName HomePlanetID BirthYear

1 Luke 1 127 Chewbaca 5 128 Anakin 4 11

23 Bob 2 12

Page 42: SQL by Example

42

SELECT Symbol, Element, AtomicNumber, AtomicMass, AtomicMass / AtomicNumber AS WeightRatio FROM Elements WHERE Grp = 4

Page 43: SQL by Example

43

Query1Symbol Element AtomicNumber AtomicMass WeightRatioHf Hafnium 72 178.49 2.47902774810791Rf Rutherfordium 104 261 2.50961542129517Ti Titanium 22 47.867 2.17577266693115Zr Zirconium 40 91.224 2.28060007095337

SELECT Symbol, Element, AtomicNumber, AtomicMass, AtomicMass / AtomicNumber AS WeightRatio FROM Elements WHERE Grp = 4

Page 44: SQL by Example

44

SELECT TOP 1 * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear

Page 45: SQL by Example

45

SELECT TOP 1 * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear

Query1BeingID BeingName HomePlanetID BirthYear

15 Armstrong 1 2

Page 46: SQL by Example

46

• SELECT TOP 1 * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear DESC

Page 47: SQL by Example

47

• SELECT TOP 1 * FROM Beings WHERE HomePlanetID = 1 ORDER BY BirthYear DESC

Query1BeingID BeingName HomePlanetID BirthYear

12 Ohuru 1 19

Page 48: SQL by Example

48

SELECT TOP 3 * FROM Beings ORDER BY BeingName

Page 49: SQL by Example

49

Query1BeingID BeingName HomePlanetID BirthYear

8 Anakin 4 1115 Armstrong 1 223 Bob 2 12

SELECT TOP 3 * FROM Beings ORDER BY BeingName

Page 50: SQL by Example

50

SELECT * FROM Elements WHERE AtomicNumber BETWEEN 80 AND 90 ORDER BY AtomicNumber DESC

Page 51: SQL by Example

51

Query1

Symbol Element AtomicNumber AtomicMass Grp Period Series

Th Thorium 90 232.0381 0 7 ActinideAc Actinium 89 227 0 7 ActinideRa Radium 88 226 2 7 Alkaline earth metal

Fr Francium 87 223 1 7 Alkali(metal)

Rn Radon 86 220 18 6 Noble gasAt Astatine 85 210 17 6 HalogenPo Polonium 84 210 16 6 MetalloidBi Bismuth 83 208.9804 15 6 Poor metalPb Lead 82 207.2 14 6 Poor metalTl Thallium 81 204.3833 13 6 Poor metalHg Mercury 80 200.59 12 6 Transition metal

SELECT * FROM Elements WHERE AtomicNumber BETWEEN 80 AND 90 ORDER BY AtomicNumber DESC

Page 52: SQL by Example

52

SELECT COUNT(*) AS NumberOfElements FROM Elements

Page 53: SQL by Example

53

Query1NumberOfElements

117

SELECT COUNT(*) AS NumberOfElements FROM Elements

Page 54: SQL by Example

54

SELECT Series, COUNT(*) AS NumberOfElements FROM Elements GROUP BY Series ORDER BY Series

Page 55: SQL by Example

55

Query1Series NumberOfElementsActinide 15Alkali(metal) 6Alkaline earth metal 6Halogen 5Lanthanide 15Metalloid 7Noble gas 7Nonmetal 7Poor metal 11Transition metal 38

SELECT Series, COUNT(*) AS NumberOfElements FROM Elements GROUP BY Series ORDER BY Series

Page 56: SQL by Example

56

SELECT Period, MAX(AtomicNumber) AS MaxAtNum FROM Elements GROUP BY Period ORDER BY Period

Page 57: SQL by Example

57

Query1Period MaxAtNum

1 22 103 184 365 546 867 118

SELECT Period, MAX(AtomicNumber) AS MaxAtNum FROM Elements GROUP BY Period ORDER BY Period