Sowe'regoingtouseanalbum I d here, whichisgoingtobeourforeignkey.
Andwe'lljustmakethis.
I'm notknowimageraswell.
Next, weneedtofindourdifferentkeys.
Soourfirstkeyisgoingtobeourprimarykey, whichwillbethe I.
D columnthatwecreatedearlier, andthenweneededto.
Ourforeignkeyintheforeignkeyisgoingtobeouralbum.
I D.
Onthisforeignkeyisgoingtoreferencecomein a referencethealbum's tableonthe I D callinghereandnowifwerunthisqueryrighthere, wehavecreated a song's tablefor a database, andwecanseethatweopenthisup, godowntoourrecordcompany, openupourtablesandrefresh.
Thisis a bitmoreof a complextask, sinceweneedtonowdosomeorderingandsomefilteringinordertogetthealbumthatistheoldestbutalsomakesurewedon't returnanyalbumsthatdon't have a releaseyou, becausetherearesomealbumsinourdatabasethatdon't have a releasehere.
Soit's alltheinformationwe'rereturningisbasedonthealbum, sowewanttodo a selectandwewanttogetthealbum's namesoalbumsdotnameandweneedtoreturnthatas a name.
Sothesewillbeallthesongsrelatedtothealbum, andthatwillgiveusthedurationofthealbum, andwecouldjustsayasdurationand I forgottoput a commauphere, sincewehavetoseparateallofourdifferentrosethatwereselectingfromcommas.
Like I said, sincehisinformationisdependentuponthealbumandthenweneededtojointothesongstableinordertogetthatdurationforthesongssowecoulddoourjointdownherejoinedsongswewanttojointhesongsonalbums.
Theyearisreleasedandthedurationofthatalbum, whichisexactlywhatwewantNextwecouldmoveontoourotherproblem, whichisgoingtobeupdatingthereleaseyearofthealbumthatdoesnothave a release, sir, inourdatabase.
Inordertodothis, wefirstneedtofigureoutwhichalbumsinourdatabasedon't have a releaseyou, sowecouldjustdo a selectjustlikeeverythingwewanttoselectfromthealbum's table.
Wewanttostartwherethereleaseyearis.
No, soit's goingtogetusalltheroadsthatdon't haveanyreallysurefortheiralbums.
Wewanttosetitequalto 1986 astheproblemsays, andthereasonwedidourselectearliersowecouldgetthe I D thatwewanttosetsowecansaywhere I d equalsfour, becauseifwedon't specifyawarecausehere, it's goingtoupdateeverysinglealbuminourentiredatabasetobe 1986.
Wejustwanttogetthatfromthesongstable, as I mentionedearlier.
Ifyourunthat, youseethatwegettheaveragesongdurationtobeabout a littleoverfiveminuteslong, whichisperfect, andthat's fairlysimpleandstraightforwardandisgoingtoParisgreatlyforthenexttwoproblemsthatwehave, whichare a bitmorecomplex.
Sowehavethe 11thproblemdownhere, whichisaskingustoselectthelongestsongoffofeachalbum, whichisgoingtorequireustodo a bitofjoiningandgroupingby.
Wealsowanttogetthereallysureofthatalbumsowe'lljustgetthereleaseyearhereandweneedtoreturnthatas a releaseyearandthen, lastly, wewanttogetthemaximumsonglikesowe'llsayMaxofSongstartlinked, andwewanttogetthatasduration.
Andagain, weneedtomakesure I putthecommasattheendofthesedifferentroseinordertosignifythatwereselectingmultiplecolumns, andweneedtodothatfromthealbum's tablesince, as I said, thesevaluesaredependentuponthealbums.
Next, weconjoinedtheseonsongstableandmakesure, I suppose, jointcorrectly.
I'm gonnadoitwherethealbumsthat I d peopledwiththesongs, thatalbumideaHerewego.
Andthen, inordertogetthelongestsong, weneedtogroupallofoursongsbyalbum I D.
SothateachoftheseMaxsongsthatlengthisbaseduponthealbumthatit's inSowecanjustsaygroupbysongsthatalbum I D.
Andif I runthat, youseethatwenowgetthelongestsongforallofourdifferentalbumslistedouthere, anditperfectlymatchesthelistthatwehaveoverhereontherightside.
Sofirst, togettotheoutortogettothesongs, weneedtofirstjoinedbands, toealbumsandthenalbumstwosongssoitwillsay, Joinsjoinsonthealbum's tableandwe'lldoitwheretheband's Di d equalsthealbumsthatband I D.
Andthenweneedtodotheexactsamethingbutjoinedthealbumstothesongssojoiningsongsonalbumsthat I d equalsongsdotalbum I D.
Andnowthisislinkingfrombands, T albumsandalbums.
Twosongsandthenwehave a bunchofdifferentsongsnow, andwewanttogroupallofthesebasedontheband I D.
Sowe'lljustsaygroupbuyalbumsdotband I D.
Andnowallofthesongsforallofthealbumsaregoingtobegroupedbasedontheband I D.
I hopebywalkingthroughthesedifferentsolutionsthat I'm abletohelpyouguysthroughanyofthesolutionsthatyou'restuckonandweren't abletofigureoutjustbylookingattheactualsolution.