Aufgaben zu SQL-Abfragen über mehrere Tabellen hinweg

Aufgaben:

Laden Sie sich die Datenbank für die folgenden Aufgaben hier herunter.

Lösen Sie die Aufgaben, indem Sie die entsprechenden SQL-Codes erstellen:

  1. Liste alle Musiker aus Pop-Bands auf.
  2. In welchen Bands spielen Musiker, die vor 1970 geboren wurden?
  3. Welchen Stil (keine Mehrfachnennungen) haben die Bands, in denen Musiker spielen, die vor 1970 geboren wurden?
  4. Liste alle Lieder auf, die von Rock-Bands gespielt werden.
  5. Erzeuge eine Liste aller Daten (aber ohne Nummern). Sortiere sie alphabetisch nach Bandname und CDTitel.
  6. Auf welchen CDs wirkt Katja Biller mit?
  7. Welche Musiker (Vor- und Nachname) spielen auf der CD „Spain“?
  8. Wer singt in dem Lied mit dem Titel „Hot Temptation“?
  9. Welche Lieder, bei denen Jill Hutu mitwirkt, dauern länger als 3:30 Minuten?
  10. Berechne die Mitgliederzahlen aller Bands mit Hilfe der COUNT-Funktion.
  11. Berechne die Spieldauer jeder erfassten CD.
  12. Wie lange dauert das längste Lied der Band „Katzen“?
  13. Wie heißt das längste Lied der Band „Katzen“?

Beachten Sie für Ihre Lösungen die weiteren Beispiele für Bedingungen:

Die Anweisung Distinct bewirkt, dass Mehrfachnennungen im Ergebnis einer Abfrage ausgeschlossen sind.

Lösungen

1. SELECT tbl_person.Vorname, tbl_person.Nachname
FROM tbl_person, tbl_band, tbl_stil
WHERE tbl_person.band_nr = tbl_band.band_id and tbl_band.stil_nr =
tbl_stil.stil_id and tbl_stil.stilbezeichnung = „pop“;

2. SELECT DISTINCT tbl_band.bandname
FROM tbl_person, tbl_band
WHERE tbl_band.band_id = tbl_person.band_nr and geburtsjahr < 1970;

3. SELECT DISTINCT tbl_stil.stilbezeichnung
FROM tbl_stil, tbl_band, tbl_person
WHERE tbl_stil.stil_id = tbl_band.stil_nr and tbl_band.band_id =
tbl_person.band_nr and tbl_person.geburtsjahr < 1970;

4. SELECT liedtitel
FROM tbl_cd, tbl_stil, tbl_band, tbl_lied
WHERE tbl_lied.cd_nr = tbl_cd.cd_id and tbl_cd.band_nr =
tbl_band.band_id and tbl_band.stil_nr = tbl_stil.stil_id and
tbl_stil.stilbezeichnung = „rock“;

5. SELECT Vorname, Nachname, Geburtsjahr, Instrument,
tbl_band.Bandname, stilbezeichnung, tbl_cd.CDTitel, LiedTitel, Länge
FROM tbl_person, tbl_band, tbl_cd, tbl_lied, tbl_stil
WHERE tbl_person.Band_nr = tbl_band.band_id
AND tbl_person.band_nr = tbl_cd.band_nr
AND tbl_cd.cd_id= tbl_lied.cd_nr
and tbl_stil.stil_id = tbl_band.stil_nr
ORDER BY tbl_band.Bandname, tbl_cd.CDTitel;

6. SELECT CDTitel
FROM tbl_person, tbl_cd, tbl_band
WHERE tbl_person.band_nr = tbl_band.band_id
AND tbl_band.band_id = tbl_cd.band_nr
and Vorname = „Katja“
AND Nachname = „Biller“;

7. SELECT tbl_person.vorname, tbl_person.nachname
FROM tbl_person, tbl_band, tbl_cd
WHERE tbl_person.band_nr = tbl_band.band_id
and tbl_band.band_id = tbl_cd.band_nr
and tbl_cd.cdtitel = „Spain“;

8. SELECT Vorname, Nachname
FROM tbl_person, tbl_band, tbl_cd, tbl_lied
WHERE tbl_person.band_nr = tbl_band.band_id
and tbl_band.band_id = tbl_cd.band_nr
and tbl_cd.cd_id = tbl_lied.cd_nr
AND Instrument = „Stimme“
AND LiedTitel = „Hot Temptation“;

9. SELECT LiedTitel
FROM tbl_person, tbl_band, tbl_cd, tbl_lied
WHERE tbl_person.band_nr = tbl_band.band_id
and tbl_band.band_id = tbl_cd.band_nr
and tbl_cd.cd_id = tbl_lied.cd_nr
AND Vorname = „Jill“
AND Nachname = „Hutu“
AND Länge > #0:3:30#;

10.SELECT tbl_band.bandname, Count(*) AS Mitgliederzahl
FROM tbl_person, tbl_band
WHERE tbl_person.band_nr = tbl_band.band_id
GROUP BY bandname;

11.SELECT CDTitel, SUM(Länge)*24*60 AS Spieldauer
FROM tbl_lied
GROUP BY CDTitel;

12.SELECT MAX(Länge)
FROM tbl_lied, tbl_cd, tbl_band
WHERE tbl_lied.cd_nr = tbl_cd.cd_id
and tbl_cd.band_nr = tbl_band.band_id
AND tbl_band.Bandname = „Katzen“;

13.SELECT LiedTitel
FROM tbl_lied, tbl_cd, tbl_band
WHERE tbl_lied.cd_nr = tbl_cd.cd_id
and tbl_cd.band_nr = tbl_band.band_id
AND tbl_band.Bandname = „Katzen“
AND Länge = #00:04:00#;

alternativ:
SELECT LiedTitel
FROM tbl_lied, tbl_cd, tbl_band
WHERE tbl_lied.cd_nr = tbl_cd.cd_id
and tbl_cd.band_nr = tbl_band.band_id
AND tbl_band.Bandname = „Katzen“
AND Länge = (SELECT MAX(Länge)
FROM tbl_lied, tbl_cd, tbl_band
WHERE tbl_lied.cd_nr = tbl_cd.cd_id
and tbl_cd.band_nr = tbl_band.band_id
AND tbl_band.Bandname = „Katzen“);