Click here to load reader

SQL Tutorial

  • View
    28

  • Download
    8

Embed Size (px)

Text of SQL Tutorial

Saad Bashir Alvi 1SQL TutoriaISaad Bashir Alvi 2Topics to be covered CREATE NSERT UPDATE SELECT ALTER DROPSaad Bashir Alvi 3irst ExampIe ovie DatabasemoviesactorscastingSaad Bashir Alvi 4TabIes of ovie DatabaseField Name Type Notesd 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.actoractorField Name Type Notesd INTEGER An arbtrary unque dentfer.name CHAR(30) The name of the actor.castingcastingField Name Type NotesINTEGERINTEGER A reference to the actor tabe.movie moviemoved A reference to the move tabe.actordSaad Bashir Alvi 5Topics to be covered #EATE NSERT UPDATE SELECT ALTER DROPSaad Bashir Alvi 6reating Databasecreate 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 7Topics to be covered CREATESE#T UPDATE SELECT ALTER DROPSaad Bashir Alvi 8!opuIating Databaseinsert 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 9Topics to be covered CREATE NSERT&!DATE SELECT ALTER DROPSaad Bashir Alvi 10&pdating #ecordupdate 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 11Topics to be covered CREATE NSERT UPDATE SELET ALTER DROPSaad Bashir Alvi 12SeIecting records !robIem: Select the year that Athenshosted the Olympic games.amesyr city000 sydney004 Athens008 Be|ng01 LondonSaad Bashir Alvi 13SeIecting records !robIem: Select the year that Athenshosted the Olympic games.SoIution:select yr, city from Games wherecity = 'Athens';amesyr city000 sydney004 Athens008 Be|ng01 Londonyr city004 AthensSaad Bashir Alvi 14SeIect with G#&! BY !robIem: SeIect the continentshosting the Iympics with the countof the number of games heId.amesyr city continent000 Austraa004 Athens Europe00801 London EuropesydneyBe|ng AsaSaad Bashir Alvi 15SeIect with G#&! BY !robIem: SeIect the continentshosting the Iympics with the countof the number of games heId.SoIution: seIect continent, count(yr) from Games group by continent;amesyr city continent000 sydney Austraa004 Athens Europe008 Be|ng Asa01 London Europecontinent count(yr)Austraa 1Asa 1Europe Saad Bashir Alvi 16SeIect with aggregate functionsDatabasebbc(name, region, area, population, gdp) !robIem: Give the totaI GD! of 'Africa'name region area population gdpAfghanstan South Asa 655 600000Abana Europe 878 30000 665600000................Saad Bashir Alvi 17SeIect with aggregate functionsDatabase bbc(name, region, area, population, gdp) !robIem: Give the totaI GD! of 'Africa'SoIution:seIect sum(gdp) from bbc where region = 'Africa'sum(gdp)41019600000name region area population gdpAfghanstan South Asa 655 600000Abana Europe 878 30000 665600000................Saad Bashir Alvi 18SeIect with aggregate functionsDatabasebbc(name, region, area, population, gdp) !robIem: How many countries have an area of at Ieast 1000000Saad Bashir Alvi 19SeIect with aggregate functionsDatabasebbc(name, region, area, population, gdp) !robIem: How many countries have an area of at Ieast 1000000SoIution:seIect count(name) from bbc where area >=1000000count(name)9Saad Bashir Alvi 20SeIect with aggregate functionsDatabasebbc(name, region, area, population, gdp) !robIem: What is the totaI popuIation of('rance','Germany','Spain') Saad Bashir Alvi 21SeIect with aggregate functionsDatabasebbc(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)187300000Saad Bashir Alvi 22SeIect with aggregate functionsDatabasebbc(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 23SeIect with aggregate functionsDatabasebbc(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 regionregion count(name)Afrca 1Amercas 3Asa 0Austraa 1Europe 16Mdde East 10North Amerca 3South Amerca 6Saad Bashir Alvi 24SeIect with join !robIem:e want to find theyear and country where the games tookplace.amesyr city1896 Athens1948 London004 Athens008 Be|ng01 Londonityname countrysydney AustraaAthens GreeceBe|ng ChnaLondon UKSaad Bashir Alvi 25SeIect with join !robIem:e want to find theyear and country where the games tookplace.SoIution:SELECT games.yr, city.countryFROM games JON city ON (games.city = city.name) amesyr city1896 Athens1948 London004 Athens008 Be|ng01 Londonityname countrysydney AustraaAthens GreeceBe|ng ChnaLondon UKyr country1896 Greece1948 UK004 Greece008 Chna01 UKSaad Bashir Alvi 26SeIect with joinDatabasealbum(asin, title, artist, price, release, label, rank)track(album, dsk, posn, song) !robIem: ind the titIe and artistwho recorded the song 'AIison'Saad Bashir Alvi 27SeIect with joinDatabasealbum(asin, title, artist, price, release, label, rank)track(album, dsk, posn, song) !robIem: ind the titIe and artistwho recorded the song 'AIison'SoIution: SELET titIe, artist # aIbum J track(aIbum.asin=track.aIbum) WHE#E song = 'AIison'title artistlvis CostelloThe Very Best OI lvis Costello And The AttractionSaad Bashir Alvi 28SeIect with joinDatabasealbum(asin, title, artist, price, release, label, rank)track(album, dsk, posn, song) !robIem: Show the song for each track on theaIbum 'BIur'Saad Bashir Alvi 29SeIect with joinDatabasealbum(asin, title, artist, price, release, label, rank)track(album, dsk, posn, song) !robIem: Show the song for each track on theaIbum 'BIur'SoIution: seIect song # aIbum J track(aIbum.asin=track.aIbum) where titIe = 'BIur'songBeetlebumSong Country sad baad man.....................Saad Bashir Alvi 30SeIect with joinDatabasealbum(asin, title, artist, price, release, label, rank)track(album, dsk, posn, song) !robIem: or each aIbum show the titIeand thetotaI number of track.Saad Bashir Alvi 31SeIect with joinDatabasealbum(asin, title, artist, price, release, label, rank)track(album, dsk, posn, song) !robIem: or each aIbum show the titIeand thetotaI number of track.SoIution: SELET titIe, &T(*) # aIbum J track (asin=aIbum) G#&! BY titIetitle &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 32SeIect with joinDatabasealbum(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 33SeIect with joinDatabasealbum(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 titIetitle &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 34SeIect with joinDatabasealbum(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 35SeIect with joinDatabasealbum(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(*) > 2song &NT(*)Angel 3Best s yet to come 3Changes 3.....................Saad Bashir Alvi 36SeIect with joinDatabasealbum(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 37SeIect with joinDatabasealbum(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(*) < .5title price &NT(*)Angel 11.98 5Best s yet to come 14.99 50Changes .98 46.....................Saad Bashir Alvi 38SeIect with joinDatabasemovie(id, title, yr, score, votes, director)actor(id, name) casting(movieid, actorid, ord) !robIem: List the fiIms in which 'Harrison ord' has appearedSaad Bashir Alvi 39SeIect with joinDatabasemovie(id, title, yr, score, votes, director)actor(id, name) casting(movieid, actorid, ord) !robIem: List the fiIms in which 'Harrison ord' has appearedSoIution: seIect titIe from movie join casting on id = movieid where actorid = (seIect id from actor where name = 'Harrison ord') titlehat Lies BeneathRandom HeartsAr Force One.....................Saad Bashir Alvi 40SeIect with joinDatabasemovie(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 fiImsSaad Bashir Alvi 41SeIect with joinDatabasemovie(id, title, yr, score, votes, director)actor(id, name)