Sie sind auf Seite 1von 50

Saad Bashir Alvi 1

SQL TutoriaI
Saad Bashir Alvi 2
Topics to be covered
CREATE
NSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 3
irst ExampIe
ovie Database
movies
actors
casting
Saad Bashir Alvi 4
TabIes of ovie Database
Field Name Type Notes
d INTEGER An arbtrary unque dentfer.
tte CHAR(70) The name of the fm.
yr DECIMAL(4) Year of frst reease.
score FLOAT Average of a the votes cast for the fm.
votes INTEGER The number of votes cast for ths fm.
actor
actor
Field Name Type Notes
d INTEGER An arbtrary unque dentfer.
name CHAR(30) The name of the actor.
casting
casting
Field Name Type Notes
INTEGER
INTEGER A reference to the actor tabe.
movie movie
moved A reference to the move tabe.
actord
Saad Bashir Alvi 5
Topics to be covered
#EATE
NSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 6
reating Database
create table movie (id int NOT NULL primary
key, title varchar(70), yr decimal(4), score float,
votes integer);
create table actor(id int NOT NULL primary key,
name varchar(30));
create table casting(movieid int, actorid int, ord
integer, primary key (movieid, actorid));
Saad Bashir Alvi 7
Topics to be covered
CREATE
SE#T
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 8
!opuIating Database
insert into table movie(id, title, yr, score, votes)
values (1, "Lione King, 2001, 5, 20000);
insert into actor(id, name) values (1, "Sambda);
insert into casting(movieid, actorid, ord) values
(1, 1, 5);
Saad Bashir Alvi 9
Topics to be covered
CREATE
NSERT
&!DATE
SELECT
ALTER
DROP
Saad Bashir Alvi 10
&pdating #ecord
update table movie set title = "Lion King where
id = 1;
update table actor set name = "simba where id
= 1;
update table casting set ord = 1 where movieid
= 1 and actorid = 1;
Saad Bashir Alvi 11
Topics to be covered
CREATE
NSERT
UPDATE
SELET
ALTER
DROP
Saad Bashir Alvi 12
SeIecting records
!robIem: Select the year that Athens
hosted the Olympic games.
ames
yr city
000 sydney
004 Athens
008 Be|ng
01 London
Saad Bashir Alvi 13
SeIecting records
!robIem: Select the year that Athens
hosted the Olympic games.
SoIution:
select yr, city from Games where city
= 'Athens';
ames
yr city
000 sydney
004 Athens
008 Be|ng
01 London
yr city
004 Athens
Saad Bashir Alvi 14
SeIect with G#&! BY
!robIem: SeIect the continents
hosting the Iympics with the count
of the number of games heId.
ames
yr city continent
000 Austraa
004 Athens Europe
008
01 London Europe
sydney
Be|ng Asa
Saad Bashir Alvi 15
SeIect with G#&! BY
!robIem: SeIect the continents
hosting the Iympics with the count
of the number of games heId.
SoIution:
seIect continent, count(yr) from Games group by
continent;
ames
yr city continent
000 sydney Austraa
004 Athens Europe
008 Be|ng Asa
01 London Europe
continent count(yr)
Austraa 1
Asa 1
Europe
Saad Bashir Alvi 16
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: Give the totaI GD! of 'Africa'
name region area population gdp
Afghanstan South Asa 655 600000
Abana Europe 878 30000 665600000
................
Saad Bashir Alvi 17
SeIect with aggregate functions
Database bbc(name,
region, area, population, gdp)
!robIem: Give the totaI GD! of 'Africa'
SoIution:
seIect sum(gdp) from bbc where region =
'Africa'
sum(gdp)
41019600000
name region area population gdp
Afghanstan South Asa 655 600000
Abana Europe 878 30000 665600000
................
Saad Bashir Alvi 18
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: How many countries have an area of
at Ieast 1000000
Saad Bashir Alvi 19
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: How many countries have an area of
at Ieast 1000000
SoIution:
seIect count(name) from bbc where area >=
1000000
count(name)
9
Saad Bashir Alvi 20
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: What is the totaI popuIation of
('rance','Germany','Spain')
Saad Bashir Alvi 21
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: What is the totaI popuIation of
('rance','Germany','Spain')
SoIution:
seIect sum(popuIation) from bbc where name =
'rance' or name = 'Germany' or name = 'Spain'
sum(population)
187300000
Saad Bashir Alvi 22
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: or each region show the region and
number of countries with
popuIations of at Ieast 10 miIIion.
Saad Bashir Alvi 23
SeIect with aggregate functions
Database
bbc(name, region, area, population, gdp)
!robIem: or each region show the region and
number of countries with
popuIations of at Ieast 10 miIIion.
SoIution:
seIect region, count(name) from bbc where
popuIation >= 10000000 group by region
region count(name)
Afrca 1
Amercas 3
Asa 0
Austraa 1
Europe 16
Mdde East 10
North Amerca 3
South Amerca 6
Saad Bashir Alvi 24
SeIect with join
!robIem:
e want to find the year and
country where the games took
place.
ames
yr city
1896 Athens
1948 London
004 Athens
008 Be|ng
01 London
ity
name country
sydney Austraa
Athens Greece
Be|ng Chna
London UK
Saad Bashir Alvi 25
SeIect with join
!robIem:
e want to find the year and
country where the games took
place.
SoIution:
SELECT games.yr, city.country
FROM games JON city ON
(games.city = city.name)
ames
yr city
1896 Athens
1948 London
004 Athens
008 Be|ng
01 London
ity
name country
sydney Austraa
Athens Greece
Be|ng Chna
London UK
yr country
1896 Greece
1948 UK
004 Greece
008 Chna
01 UK
Saad Bashir Alvi 26
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: ind the titIe and artist who recorded
the song 'AIison'
Saad Bashir Alvi 27
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: ind the titIe and artist who recorded
the song 'AIison'
SoIution:
SELET titIe, artist
# aIbum J track
(aIbum.asin=track.aIbum)
WHE#E song = 'AIison'
title artist
lvis Costello
The Very Best OI lvis Costello
And The Attraction
Saad Bashir Alvi 28
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: Show the song for each track on the
aIbum 'BIur'
Saad Bashir Alvi 29
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: Show the song for each track on the
aIbum 'BIur'
SoIution:
seIect song # aIbum J track
(aIbum.asin=track.aIbum) where titIe = 'BIur'
song
Beetlebum
Song
Country sad baad man
.....................
Saad Bashir Alvi 30
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: or each aIbum show the titIe and the
totaI number of track.
Saad Bashir Alvi 31
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: or each aIbum show the titIe and the
totaI number of track.
SoIution:
SELET titIe, &T(*) # aIbum J track
(asin=aIbum) G#&! BY titIe
title &NT(*)
usic Irom the otion Picture Purple Rain 9
(What's The Story) Mornng Gory? 1
..Baby One More Tme |ENHANCED CD| 11
.....................
Saad Bashir Alvi 32
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: or each aIbum show the titIe and the
totaI number of tracks containing the word 'Heart'.
Saad Bashir Alvi 33
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: or each aIbum show the titIe and the
totaI number of tracks containing the word 'Heart'.
SoIution:
SELET titIe, &T(*) # aIbum J track
(asin=aIbum) where song Iike "%Heart%"
G#&! BY titIe
title &NT(*)
usic Irom the otion Picture Purple Rain 1
(What's The Story) Mornng Gory? 4
..Baby One More Tme |ENHANCED CD|
.....................
Saad Bashir Alvi 34
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: ind the songs that appear on more
than 2 aIbums. ncIude a count of the number of
times each shows up.
Saad Bashir Alvi 35
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: ind the songs that appear on more
than 2 aIbums. ncIude a count of the number of
times each shows up.
SoIution:
seIect song, count(*) # aIbum J track
(aIbum.asin=track.aIbum) group by song
having count(*) > 2
song &NT(*)
Angel 3
Best s yet to come 3
Changes 3
.....................
Saad Bashir Alvi 36
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: A "good vaIue" aIbum is one where the
price per track is Iess than 50 cents. ind the good
vaIue aIbum - show the titIe, the price and the number
of tracks.
Saad Bashir Alvi 37
SeIect with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
!robIem: A "good vaIue" aIbum is one where the
price per track is Iess than 50 cents. ind the good
vaIue aIbum - show the titIe, the price and the number
of tracks.
SoIution:
seIect titIe, price, count(*) # aIbum J track
(aIbum.asin=track.aIbum) group by titIe having
price/count(*) < .5
title price &NT(*)
Angel 11.98 5
Best s yet to come 14.99 50
Changes .98 46
.....................
Saad Bashir Alvi 38
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: List the fiIms in which 'Harrison ord' has
appeared
Saad Bashir Alvi 39
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: List the fiIms in which 'Harrison ord' has
appeared
SoIution:
seIect titIe from movie join casting on id = movieid
where actorid = (seIect id from actor where name =
'Harrison ord')
title
hat Lies Beneath
Random Hearts
Ar Force One
.....................
Saad Bashir Alvi 40
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: List the fiIms together with the Ieading star
for aII 1962 fiIms
Saad Bashir Alvi 41
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: List the fiIms together with the Ieading star
for aII 1962 fiIms
SoIution:
seIect titIe, name from movie, actor, casting where
yr = '1962' and ord = 1 and movie.id =
casting.movieid and actor.id = casting.actorid title name
id Galahad Evs Presey
The Man Who Shot Lberty Vaance |ohn Wayne
Mothra Franke Saka
.....................
Saad Bashir Alvi 42
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: Which were the busiest years for 'John
TravoIta'. Show the number of movies he made for
each year.
Saad Bashir Alvi 43
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: Which were the busiest years for 'John
TravoIta'. Show the number of movies he made for
each year.
SoIution:
seIect yr, count(*) from movie, casting, actor where
actor.id = casting.actorid and movie.id =
casting.movieid and actor.name = 'John TravoIta'
group by yr order by count(*) desc Iimit 1
yr count(*)
1997 2
Saad Bashir Alvi 44
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: List the 1978 fiIms by order of cast Iist
size.
Saad Bashir Alvi 45
SeIect with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
!robIem: List the 1978 fiIms by order of cast Iist
size.
SoIution:
seIect titIe, count(actor.id) from movie, actor,
casting where actor.id = casting.actorid and
movie.id = casting.movieid and yr = 1978 group by
titIe order by count(actor.id) desc
title count(*)
id Galahad 16
The Man Who Shot Lberty Vaance 13
Mothra 8
.....................
Saad Bashir Alvi 46
Topics to be covered
CREATE
NSERT
UPDATE
SELECT
ALTE#
DROP
Saad Bashir Alvi 47
ALTE#
ALTER TABLE actor add column age integer;
ALTER TABLE actor change age newage
integer;
ALTER TABLE actor drop column age;
Saad Bashir Alvi 48
Topics to be covered
CREATE
NSERT
UPDATE
SELECT
ALTER
D#!
Saad Bashir Alvi 49
D#!
drop table movie;
Saad Bashir Alvi 50
Thanks and Good Iuck
for your exams

Das könnte Ihnen auch gefallen