SQL Tutorial: Saad Bashir Alvi 1
SQL Tutorial: Saad Bashir Alvi 1
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
Movie Database
movies
actors
casting
actor actor
Field Name Type Notes
id INTEGER An arbitrary unique identifier.
name CHAR(30) The name of the actor.
casting casting
Field Name Type Notes
movieid INTEGER A reference to the movie table.
actorid INTEGER A reference to the actor table.
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
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);
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
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;
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
Problem: Select the year that Athens 2000
2004
sydney
Athens
Problem: Select the year that Athens 2000
2004
sydney
Athens
Solution:
select yr, city from Games where city
= 'Athens';
yr city
2004 Athens
2000
city
sydney
continent
Australia
2000
city
sydney
continent
Australia
Solution:
select continent, count(yr) from Games group by
continent;
continent count(yr)
Australia 1
Asia 1
Europe 2
Database
................
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)
410196200000
count(name)
29
sum(population)
187300000
2012
Biejing
London
place.
2012
Biejing
London
place.
Solution:
SELECT games.yr, city.country yr country
2008 China
2012 UK
song
Beetlebum
Song 2
Country sad ballad man
Changes 22.98 46
.....................
Saad Bashir Alvi 37
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
Random Hearts
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
ALTER TABLE actor add column age integer;
ALTER TABLE actor change age newage
integer;
ALTER TABLE actor drop column age;
CREATE
INSERT
UPDATE
SELECT
ALTER
DROP
drop table movie;