Sie sind auf Seite 1von 50

Saad Bashir Alvi 1

SQL Tutorial
Saad Bashir Alvi 2
Topics to be covered
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 3
First Example
Movie Database
movies
actors
casting
Saad Bashir Alvi 4
Tables of Movie 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
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 6
Creating 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
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 8
Populating 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
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 10
Updating Record
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
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 12
Selecting records
Problem: 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
Selecting records
Problem: Select the year that Athens
hosted the Olympic games.
Solution:
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
Select with GROUP BY
Problem: Select the continents
hosting the Olympics with the count
of the number of games held.
*ames
yr city continent
000 Austraa
004 Athens Europe
008
01 London Europe
sydney
Be|ng Asa
Saad Bashir Alvi 15
Select with GROUP BY
Problem: Select the continents
hosting the Olympics with the count
of the number of games held.
Solution:
select 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
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: Give the total GDP of 'Africa'
name region area population gdp
Afghanstan South Asa 655 600000
Abana Europe 878 30000 665600000
................
Saad Bashir Alvi 17
Select with aggregate functions
Database bbc(name,
region, area, population, gdp)
Problem: Give the total GDP of 'Africa'
Solution:
select 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
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: How many countries have an area of
at least 1000000
Saad Bashir Alvi 19
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: How many countries have an area of
at least 1000000
Solution:
select count(name) from bbc where area >=
1000000
count(name)
9
Saad Bashir Alvi 20
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: What is the total population of
('France','Germany','Spain')
Saad Bashir Alvi 21
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: What is the total population of
('France','Germany','Spain')
Solution:
select sum(population) from bbc where name =
'France' or name = 'Germany' or name = 'Spain'
sum(population)
187300000
Saad Bashir Alvi 22
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: For each region show the region and
number of countries with
populations of at least 10 million.
Saad Bashir Alvi 23
Select with aggregate functions
Database
bbc(name, region, area, population, gdp)
Problem: For each region show the region and
number of countries with
populations of at least 10 million.
Solution:
select region, count(name) from bbc where
population >= 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
Select with join
Problem:
We 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
Select with join
Problem:
We want to find the year and
country where the games took
place.
Solution:
SELECT games.yr, city.country
FROM games JOIN 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
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: Find the title and artist who recorded
the song 'Alison'
Saad Bashir Alvi 27
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: Find the title and artist who recorded
the song 'Alison'
Solution:
SELECT title, artist
FROM album JOIN track
ON (album.asin=track.album)
WHERE song = 'Alison'
title artist
Elvis Costello
The Very Best Of Elvis Costello
And The Attraction
Saad Bashir Alvi 28
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: Show the song for each track on the
album 'Blur'
Saad Bashir Alvi 29
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: Show the song for each track on the
album 'Blur'
Solution:
select song FROM album JOIN track ON
(album.asin=track.album) where title = 'Blur'
song
Beetlebum
Song
Country sad baad man
.....................
Saad Bashir Alvi 30
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: For each album show the title and the
total number of track.
Saad Bashir Alvi 31
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: For each album show the title and the
total number of track.
Solution:
SELECT title, COUNT(*) FROM album JOIN track
ON (asin=album) GROUP BY title
title &28NT(*)
"Music from the Motion Picture ""Purple Rain""" 9
(What's The Story) Mornng Gory? 1
..Baby One More Tme |ENHANCED CD| 11
.....................
Saad Bashir Alvi 32
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: For each album show the title and the
total number of tracks containing the word 'Heart'.
Saad Bashir Alvi 33
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: For each album show the title and the
total number of tracks containing the word 'Heart'.
Solution:
SELECT title, COUNT(*) FROM album JOIN track
ON (asin=album) where song like "%Heart%"
GROUP BY title
title &28NT(*)
"Music from the Motion Picture ""Purple Rain""" 1
(What's The Story) Mornng Gory? 4
..Baby One More Tme |ENHANCED CD|
.....................
Saad Bashir Alvi 34
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: Find the songs that appear on more
than 2 albums. Include a count of the number of
times each shows up.
Saad Bashir Alvi 35
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: Find the songs that appear on more
than 2 albums. Include a count of the number of
times each shows up.
Solution:
select song, count(*) FROM album JOIN track
ON (album.asin=track.album) group by song
having count(*) > 2
song &28NT(*)
Angel 3
Best s yet to come 3
Changes 3
.....................
Saad Bashir Alvi 36
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: A "good value" album is one where the
price per track is less than 50 cents. Find the good
value album - show the title, the price and the number
of tracks.
Saad Bashir Alvi 37
Select with join
Database
album(asin, title, artist, price, release, label, rank)
track(album, dsk, posn, song)
Problem: A "good value" album is one where the
price per track is less than 50 cents. Find the good
value album - show the title, the price and the number
of tracks.
Solution:
select title, price, count(*) FROM album JOIN track
ON (album.asin=track.album) group by title having
price/count(*) < .5
title price &28NT(*)
Angel 11.98 5
Best s yet to come 14.99 50
Changes .98 46
.....................
Saad Bashir Alvi 38
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: List the films in which 'Harrison Ford' has
appeared
Saad Bashir Alvi 39
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: List the films in which 'Harrison Ford' has
appeared
Solution:
select title from movie join casting on id = movieid
where actorid = (select id from actor where name =
'Harrison Ford')
title
What Lies Beneath
Random Hearts
Ar Force One
.....................
Saad Bashir Alvi 40
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: List the films together with the leading star
for all 1962 films
Saad Bashir Alvi 41
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: List the films together with the leading star
for all 1962 films
Solution:
select title, name from movie, actor, casting where
yr = '1962' and ord = 1 and movie.id =
casting.movieid and actor.id = casting.actorid title name
Kid Galahad Evs Presey
The Man Who Shot Lberty Vaance |ohn Wayne
Mothra Franke Saka
.....................
Saad Bashir Alvi 42
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: Which were the busiest years for 'John
Travolta'. Show the number of movies he made for
each year.
Saad Bashir Alvi 43
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: Which were the busiest years for 'John
Travolta'. Show the number of movies he made for
each year.
Solution:
select yr, count(*) from movie, casting, actor where
actor.id = casting.actorid and movie.id =
casting.movieid and actor.name = 'John Travolta'
group by yr order by count(*) desc limit 1
yr count(*)
1997 2
Saad Bashir Alvi 44
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: List the 1978 films by order of cast list
size.
Saad Bashir Alvi 45
Select with join
Database
movie(id, title, yr, score, votes, director)
actor(id, name)
casting(movieid, actorid, ord)
Problem: List the 1978 films by order of cast list
size.
Solution:
select title, count(actor.id) from movie, actor,
casting where actor.id = casting.actorid and
movie.id = casting.movieid and yr = 1978 group by
title order by count(actor.id) desc
title count(*)
Kid Galahad 16
The Man Who Shot Lberty Vaance 13
Mothra 8
.....................
Saad Bashir Alvi 46
Topics to be covered
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 47
ALTER
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
INSERT
UPDATE
SELECT
ALTER
DROP
Saad Bashir Alvi 49
DROP
drop table movie;
Saad Bashir Alvi 50
Thanks and Good luck
for your exams

Das könnte Ihnen auch gefallen