Auf den folgenden Seiten habe ich mal diverse SQL Anfragen mit ihren Ergebnissen auf einer Beispielsdatenbank zusammengestellt, die auf verschiedene Möglichkeiten SQL benutzen. Dieses hilft einfache SQL Queries zu lernen und zu verstehen sowie eben mal schnell die Schreibweise von häufigen Abfragen nachzusehen. EIn SQL Anfänger bekommt dadurch einen guten Überblick über die Möglichkeiten von SQL. Es werden Themen wie select, count, union, where, having, order, limit, views, NULL, like, subselects und joins behandelt.
Für jeden Befehl gibt es eine kurze Beschreibung was er tut, danach der eigentliche Befehl und dann das Ergebnis der Abfrage. Die SQL um die Beispielsdatenbank und -daten zu erzeugen können hier heruntergeladen werden. Alle SQL Beispiele befinden sich hier. Die Beispieldatenbank ist eine Modellierung einer kleinen Datenbank die ähnliche Relationen wie Facebook hat: Personen, Freunde, Geschwister, Städte, Adressen und Meldungen.
Die Installation von mySQL wird hier beschrieben und
mysql <frampsSQLByExampleData.sql
mysql --column-names --table <frampsSQLByExample.sql
installiert die BeispielDaten und führt dieBeispielQueries aus.
Sample database model
-- *******************
-- Simple queries to show the contents of the sample database
-- Einfache Queries um den Inhalt der Beispieldatenbank zu zeigen.
-- *******************
-- Abfrage nach allen Personen
select * from Person;
+----+----------+-----------+-----------+-----------+------------+---------+--------+
| id | LastName | FirstName | Father_Id | Mother_Id | Address_Id | Gender | Height |
+----+----------+-----------+-----------+-----------+------------+---------+--------+
| 1 | Turner | Peter | 6 | 5 | 1 | M | 180 |
| 2 | Turner | Angie | NULL | 8 | 1 | F | 160 |
| 3 | Turner | Marvin | 1 | 2 | 2 | M | 185 |
| 4 | Turner | Roxanne | 1 | 2 | 1 | F | 155 |
| 5 | Turner | Wendy | 8 | NULL | 3 | F | 167 |
| 6 | Turner | Richard | NULL | NULL | 3 | M | 162 |
| 8 | Wilson | Hilary | NULL | NULL | 4 | F | 169 |
| 7 | Wilson | Woodrow | NULL | NULL | 4 | M | 174 |
| 10 | Chen | Walter | NULL | NULL | NULL | M | 156 |
| 9 | Robinson | Rick | NULL | NULL | NULL | M | 189 |
+----+----------+-----------+-----------+-----------+------------+---------+--------+
-- Abfrage nach Geschwistern
select * from Sibling;
+----+---------+-------+
| id | from_Id | to_Id |
+----+---------+-------+
| 1 | 3 | 4 |
| 2 | 4 | 3 |
+----+---------+-------+
-- Abfrage nach Stadt
select * from City;
+----+-----------+-------+
| id | Name | ZIP |
+----+-----------+-------+
| 1 | Rochester | 87656 |
| 2 | Paris | 1234 |
| 3 | Wien | 52637 |
| 4 | Berlin | 12987 |
| 5 | New York | 65432 |
+----+-----------+-------+
-- Abfrage nach Adresse
select * from Address;
+----+---------+----------------+------+
| id | City_Id | Street | No |
+----+---------+----------------+------+
| 1 | 1 | Bondstreet | 13 |
| 2 | 2 | PlaceDeLetoile | 45 |
| 3 | 3 | BarneysStreet | 5 |
| 4 | 4 | ShiningStreet | 10 |
+----+---------+----------------+------+
-- Abfrage nach Messages
select * from Message;
+----+---------+-------+------------------------------------------+
| id | from_Id | to_ID | message |
+----+---------+-------+------------------------------------------+
| 1 | 1 | 2 | Nice to meet you |
| 2 | 2 | 1 | I also enjoy to meet you |
| 3 | 3 | 1 | My system crashed. Can you help me? |
| 4 | 3 | 2 | I'm hungry |
| 5 | 2 | 3 | Just visit MC'Donalds |
| 6 | 1 | 3 | Restart your system |
| 7 | 3 | 1 | Could you help me with GMAT? |
| 8 | 3 | 1 | Don't forget me and buy a big mac for me |
+----+---------+-------+------------------------------------------+
-- Abfrage nach allen Freunden
select * from Friend;
+----+---------+-------+
| id | from_Id | to_Id |
+----+---------+-------+
| 1 | 3 | 9 |
| 2 | 9 | 3 |
| 3 | 4 | 10 |
| 4 | 10 | 4 |
| 5 | 3 | 9 |
| 6 | 9 | 3 |
+----+---------+-------+
-- Anzeige von bestimmten Spalten alle Personen
select LastName, Firstname from Person;
+----------+-----------+
| LastName | Firstname |
+----------+-----------+
| Turner | Peter |
| Turner | Angie |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
| Turner | Richard |
| Wilson | Hilary |
| Wilson | Woodrow |
| Chen | Walter |
| Robinson | Rick |
+----------+-----------+
-- Abfrage aller Personen sortiert nach Vorname
select LastName, FirstName from Person order by FirstName;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Angie |
| Wilson | Hilary |
| Turner | Marvin |
| Turner | Peter |
| Turner | Richard |
| Robinson | Rick |
| Turner | Roxanne |
| Chen | Walter |
| Turner | Wendy |
| Wilson | Woodrow |
+----------+-----------+
-- Abfrage aller Personen sortiert nach Nachnamen und Vornamen
select LastName, FirstName from Person order by LastName, FirstName;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Chen | Walter |
| Robinson | Rick |
| Turner | Angie |
| Turner | Marvin |
| Turner | Peter |
| Turner | Richard |
| Turner | Roxanne |
| Turner | Wendy |
| Wilson | Hilary |
| Wilson | Woodrow |
+----------+-----------+
-- Abfrage aller Personen sortiert nach Nachnamen abfallend und Vornamen abfallend
select LastName, FirstName from Person order by LastName desc, FirstName asc;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Wilson | Hilary |
| Wilson | Woodrow |
| Turner | Angie |
| Turner | Marvin |
| Turner | Peter |
| Turner | Richard |
| Turner | Roxanne |
| Turner | Wendy |
| Robinson | Rick |
| Chen | Walter |
+----------+-----------+
-- Abfrage nach Personen die einen bestimmten Nachnamen haben
select LastName, Firstname from Person
where LastName = 'Turner';
+----------+-----------+
| LastName | Firstname |
+----------+-----------+
| Turner | Peter |
| Turner | Angie |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
| Turner | Richard |
+----------+-----------+
-- Abfrage der Personen wobei bestimmte Spalten deutsche Überschriften bekommen
select LastName as Nachname, Firstname as Vorname from Person;
+----------+---------+
| Nachname | Vorname |
+----------+---------+
| Turner | Peter |
| Turner | Angie |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
| Turner | Richard |
| Wilson | Hilary |
| Wilson | Woodrow |
| Chen | Walter |
| Robinson | Rick |
+----------+---------+
-- Simple queries to show the contents of the sample database
-- Einfache Queries um den Inhalt der Beispieldatenbank zu zeigen.
-- *******************
-- Abfrage nach allen Personen
select * from Person;
+----+----------+-----------+-----------+-----------+------------+---------+--------+
| id | LastName | FirstName | Father_Id | Mother_Id | Address_Id | Gender | Height |
+----+----------+-----------+-----------+-----------+------------+---------+--------+
| 1 | Turner | Peter | 6 | 5 | 1 | M | 180 |
| 2 | Turner | Angie | NULL | 8 | 1 | F | 160 |
| 3 | Turner | Marvin | 1 | 2 | 2 | M | 185 |
| 4 | Turner | Roxanne | 1 | 2 | 1 | F | 155 |
| 5 | Turner | Wendy | 8 | NULL | 3 | F | 167 |
| 6 | Turner | Richard | NULL | NULL | 3 | M | 162 |
| 8 | Wilson | Hilary | NULL | NULL | 4 | F | 169 |
| 7 | Wilson | Woodrow | NULL | NULL | 4 | M | 174 |
| 10 | Chen | Walter | NULL | NULL | NULL | M | 156 |
| 9 | Robinson | Rick | NULL | NULL | NULL | M | 189 |
+----+----------+-----------+-----------+-----------+------------+---------+--------+
-- Abfrage nach Geschwistern
select * from Sibling;
+----+---------+-------+
| id | from_Id | to_Id |
+----+---------+-------+
| 1 | 3 | 4 |
| 2 | 4 | 3 |
+----+---------+-------+
-- Abfrage nach Stadt
select * from City;
+----+-----------+-------+
| id | Name | ZIP |
+----+-----------+-------+
| 1 | Rochester | 87656 |
| 2 | Paris | 1234 |
| 3 | Wien | 52637 |
| 4 | Berlin | 12987 |
| 5 | New York | 65432 |
+----+-----------+-------+
-- Abfrage nach Adresse
select * from Address;
+----+---------+----------------+------+
| id | City_Id | Street | No |
+----+---------+----------------+------+
| 1 | 1 | Bondstreet | 13 |
| 2 | 2 | PlaceDeLetoile | 45 |
| 3 | 3 | BarneysStreet | 5 |
| 4 | 4 | ShiningStreet | 10 |
+----+---------+----------------+------+
-- Abfrage nach Messages
select * from Message;
+----+---------+-------+------------------------------------------+
| id | from_Id | to_ID | message |
+----+---------+-------+------------------------------------------+
| 1 | 1 | 2 | Nice to meet you |
| 2 | 2 | 1 | I also enjoy to meet you |
| 3 | 3 | 1 | My system crashed. Can you help me? |
| 4 | 3 | 2 | I'm hungry |
| 5 | 2 | 3 | Just visit MC'Donalds |
| 6 | 1 | 3 | Restart your system |
| 7 | 3 | 1 | Could you help me with GMAT? |
| 8 | 3 | 1 | Don't forget me and buy a big mac for me |
+----+---------+-------+------------------------------------------+
-- Abfrage nach allen Freunden
select * from Friend;
+----+---------+-------+
| id | from_Id | to_Id |
+----+---------+-------+
| 1 | 3 | 9 |
| 2 | 9 | 3 |
| 3 | 4 | 10 |
| 4 | 10 | 4 |
| 5 | 3 | 9 |
| 6 | 9 | 3 |
+----+---------+-------+
-- Anzeige von bestimmten Spalten alle Personen
select LastName, Firstname from Person;
+----------+-----------+
| LastName | Firstname |
+----------+-----------+
| Turner | Peter |
| Turner | Angie |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
| Turner | Richard |
| Wilson | Hilary |
| Wilson | Woodrow |
| Chen | Walter |
| Robinson | Rick |
+----------+-----------+
-- Abfrage aller Personen sortiert nach Vorname
select LastName, FirstName from Person order by FirstName;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Angie |
| Wilson | Hilary |
| Turner | Marvin |
| Turner | Peter |
| Turner | Richard |
| Robinson | Rick |
| Turner | Roxanne |
| Chen | Walter |
| Turner | Wendy |
| Wilson | Woodrow |
+----------+-----------+
-- Abfrage aller Personen sortiert nach Nachnamen und Vornamen
select LastName, FirstName from Person order by LastName, FirstName;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Chen | Walter |
| Robinson | Rick |
| Turner | Angie |
| Turner | Marvin |
| Turner | Peter |
| Turner | Richard |
| Turner | Roxanne |
| Turner | Wendy |
| Wilson | Hilary |
| Wilson | Woodrow |
+----------+-----------+
-- Abfrage aller Personen sortiert nach Nachnamen abfallend und Vornamen abfallend
select LastName, FirstName from Person order by LastName desc, FirstName asc;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Wilson | Hilary |
| Wilson | Woodrow |
| Turner | Angie |
| Turner | Marvin |
| Turner | Peter |
| Turner | Richard |
| Turner | Roxanne |
| Turner | Wendy |
| Robinson | Rick |
| Chen | Walter |
+----------+-----------+
-- Abfrage nach Personen die einen bestimmten Nachnamen haben
select LastName, Firstname from Person
where LastName = 'Turner';
+----------+-----------+
| LastName | Firstname |
+----------+-----------+
| Turner | Peter |
| Turner | Angie |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
| Turner | Richard |
+----------+-----------+
-- Abfrage der Personen wobei bestimmte Spalten deutsche Überschriften bekommen
select LastName as Nachname, Firstname as Vorname from Person;
+----------+---------+
| Nachname | Vorname |
+----------+---------+
| Turner | Peter |
| Turner | Angie |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
| Turner | Richard |
| Wilson | Hilary |
| Wilson | Woodrow |
| Chen | Walter |
| Robinson | Rick |
+----------+---------+
-- *******************
-- NULL values
-- NULL Werte
-- *******************
-- NULL Werte sind NICHT gleich
select NULL = NULL;
+-------------+
| NULL = NULL |
+-------------+
| NULL |
+-------------+
-- NULL Werte sind gleich wenn der spezielle Operator <=> benutzt wird
select NULL <=> NULL;
+---------------+
| NULL <=> NULL |
+---------------+
| 1 |
+---------------+
-- Tests auf NULL Gleichheit
select NULL is null,NULL is not null;
+--------------+------------------+
| NULL is null | NULL is not null |
+--------------+------------------+
| 1 | 0 |
+--------------+------------------+
-- count(*) zählt alle Zeilen einer Tabelle während count(SpaltenName) nur Zeilen zählt, die kein NULL in der Spalte haben
select count(*) as 'Total # of Persons', count(Father_Id) as 'Person has father' from Person;
+--------------------+-------------------+
| Total # of Persons | Person has father |
+--------------------+-------------------+
| 10 | 4 |
+--------------------+-------------------+
-- Auswahl der Personen, für die kein Vater bekannt ist
select LastName, FirstName from Person
where Father_Id is null;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Angie |
| Turner | Richard |
| Wilson | Hilary |
| Wilson | Woodrow |
| Chen | Walter |
| Robinson | Rick |
+----------+-----------+
-- Auswahl der Personen, die ihren Vater kennen
select LastName, FirstName from Person
where Father_Id is not null;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Peter |
| Turner | Marvin |
| Turner | Roxanne |
| Turner | Wendy |
+----------+-----------+
-- Auswahl der Personen, die nicht beide Eltern kennen
select LastName, FirstName from Person
where (Father_Id is not null
and Mother_Id is null)
or
(Father_Id is null
and Mother_Id is not null);
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Angie |
| Turner | Wendy |
+----------+-----------+
-- NULL wird niemals bei einem like Vergleich gefunden
select NULL like '%', NULL not like '%';
select 'art' like 'a%', 'art' not like '%';
+---------------+-------------------+
| NULL like '%' | NULL not like '%' |
+---------------+-------------------+
| NULL | NULL |
+---------------+-------------------+
+-----------------+--------------------+
| 'art' like 'a%' | 'art' not like '%' |
+-----------------+--------------------+
| 1 | 0 |
+-----------------+--------------------+
-- *******************
-- Ansichten um Abfragen zu vereinfachen
-- *******************
-- Erstellung einer view die nur Personen liefert, die nur einen Vater haben
create or replace view motherOnly as
select LastName, FirstName from Person
where (Father_Id is not null
and Mother_Id is null);
-- Erstellung einer view die nur Personen liefert, die nur eine Mutter haben
create or replace view fatherOnly as
select LastName, FirstName from Person
where (Father_Id is null
and Mother_Id is not null);
-- Select persons which either know father or mother
-- Auswahl der Personen, die entweder den Vater oder die Mutter kennen
select * from fatherOnly
union
select * from motherOnly;
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Angie |
| Turner | Wendy |
+----------+-----------+
-- *******************
-- Gruppen Abfragen
-- *******************
-- Abfrage der Anzahl von Frauen und Männern mit demselben Nachnamen
select LastName, Gender, count(*) as Count from Person
group by LastName,Gender;
+----------+---------+-------+
| LastName | Gender | Count |
+----------+---------+-------+
| Chen | M | 1 |
| Robinson | M | 1 |
| Turner | F | 3 |
| Turner | M | 3 |
| Wilson | F | 1 |
| Wilson | M | 1 |
+----------+---------+-------+
-- Abfrage der Anzahl von Leuten mit einem bestimmten Nachnamen
select count(*) as 'Number of people' from Person
where LastName = 'Turner';
+------------------+
| Number of people |
+------------------+
| 6 |
+------------------+
-- Abfrage der Anzahl von Frauen und Männern
select Gender,count(*) as 'Number of people' from Person
group by Gender;
+---------+------------------+
| Gender | Number of people |
+---------+------------------+
| F | 4 |
| M | 6 |
+---------+------------------+
| Gender | Number of people |
+---------+------------------+
| F | 4 |
| M | 6 |
+---------+------------------+
-- Abfrage der Anzahl von Frauen und Männern und Ändern der Spaltenüberschriften
select If(Gender = 'M',"Men","Woman") as Gender ,count(*) as 'Number of people' from Person
group by Gender;
+---------+------------------+
| Gender | Number of people |
+---------+------------------+
| F | 4 |
| M | 6 |
+---------+------------------+
-- Abfrage der Anzahl von Frauen und Männern mit demselben Nachnamen sofern es mehr als 2 sind
select LastName, Gender, count(*) as Count from Person
group by LastName,Gender
select If(Gender = 'M',"Men","Woman") as Gender ,count(*) as 'Number of people' from Person
group by Gender;
+---------+------------------+
| Gender | Number of people |
+---------+------------------+
| F | 4 |
| M | 6 |
+---------+------------------+
-- Abfrage der Anzahl von Frauen und Männern mit demselben Nachnamen sofern es mehr als 2 sind
select LastName, Gender, count(*) as Count from Person
group by LastName,Gender
having count(*) > 2;
+----------+---------+-------+
| LastName | Gender | Count |
+----------+---------+-------+
| Turner | F | 3 |
| Turner | M | 3 |
+----------+---------+-------+
+----------+---------+-------+
| LastName | Gender | Count |
+----------+---------+-------+
| Turner | F | 3 |
| Turner | M | 3 |
+----------+---------+-------+
-- *******************
-- Berechnungen
-- *******************
-- Abfrage der Größe der Personen
select LastName, FirstName, height as Avg from Person
order by height;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Turner | Roxanne | 155 |
| Chen | Walter | 156 |
| Turner | Angie | 160 |
| Turner | Richard | 162 |
| Turner | Wendy | 167 |
| Wilson | Hilary | 169 |
| Wilson | Woodrow | 174 |
| Turner | Peter | 180 |
| Turner | Marvin | 185 |
| Robinson | Rick | 189 |
+----------+-----------+------+
-- Abfrage der kleinsten Person
select LastName, FirstName, height as Avg from Person
order by height
limit 1;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Turner | Roxanne | 155 |
+----------+-----------+------+
-- Abfrage der größten Person
select LastName, FirstName, height as Avg from Person
order by height desc
limit 1;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Robinson | Rick | 189 |
+----------+-----------+------+
-- Abfrage der minimalen, maximalen und mittleren Größe der Personen
select Min(height) as Min, Max(height) as Max, Avg(height) as Avg from Person;
+------+------+----------+
| Min | Max | Avg |
+------+------+----------+
| 155 | 189 | 169.7000 |
+------+------+----------+
-- Abfrage der kleinsten und größten Person
select LastName, FirstName, Height from Person
where height = (select max(height) from Person)
or height = (select min(height) from Person);
+----------+-----------+--------+
| LastName | FirstName | Height |
+----------+-----------+--------+
| Turner | Roxanne | 155 |
| Robinson | Rick | 189 |
+----------+-----------+--------+
-- Abfrage der Personen, die Größer als das Mittelmass sind
set @avg = (select Avg(height) from Person);
select LastName, FirstName, Height as Avg from Person
where Height > @avg
order by height desc;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Robinson | Rick | 189 |
| Turner | Marvin | 185 |
| Turner | Peter | 180 |
| Wilson | Woodrow | 174 |
+----------+-----------+------+
-- oder
select LastName, FirstName, Height as Avg from Person
where Height > (select avg(height) from Person)
order by height desc;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Robinson | Rick | 189 |
| Turner | Marvin | 185 |
| Turner | Peter | 180 |
| Wilson | Woodrow | 174 |
+----------+-----------+------+
-- Berechnungen
-- *******************
-- Abfrage der Größe der Personen
select LastName, FirstName, height as Avg from Person
order by height;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Turner | Roxanne | 155 |
| Chen | Walter | 156 |
| Turner | Angie | 160 |
| Turner | Richard | 162 |
| Turner | Wendy | 167 |
| Wilson | Hilary | 169 |
| Wilson | Woodrow | 174 |
| Turner | Peter | 180 |
| Turner | Marvin | 185 |
| Robinson | Rick | 189 |
+----------+-----------+------+
-- Abfrage der kleinsten Person
select LastName, FirstName, height as Avg from Person
order by height
limit 1;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Turner | Roxanne | 155 |
+----------+-----------+------+
-- Abfrage der größten Person
select LastName, FirstName, height as Avg from Person
order by height desc
limit 1;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Robinson | Rick | 189 |
+----------+-----------+------+
-- Abfrage der minimalen, maximalen und mittleren Größe der Personen
select Min(height) as Min, Max(height) as Max, Avg(height) as Avg from Person;
+------+------+----------+
| Min | Max | Avg |
+------+------+----------+
| 155 | 189 | 169.7000 |
+------+------+----------+
-- Abfrage der kleinsten und größten Person
select LastName, FirstName, Height from Person
where height = (select max(height) from Person)
or height = (select min(height) from Person);
+----------+-----------+--------+
| LastName | FirstName | Height |
+----------+-----------+--------+
| Turner | Roxanne | 155 |
| Robinson | Rick | 189 |
+----------+-----------+--------+
-- Abfrage der Personen, die Größer als das Mittelmass sind
set @avg = (select Avg(height) from Person);
select LastName, FirstName, Height as Avg from Person
where Height > @avg
order by height desc;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Robinson | Rick | 189 |
| Turner | Marvin | 185 |
| Turner | Peter | 180 |
| Wilson | Woodrow | 174 |
+----------+-----------+------+
-- oder
select LastName, FirstName, Height as Avg from Person
where Height > (select avg(height) from Person)
order by height desc;
+----------+-----------+------+
| LastName | FirstName | Avg |
+----------+-----------+------+
| Robinson | Rick | 189 |
| Turner | Marvin | 185 |
| Turner | Peter | 180 |
| Wilson | Woodrow | 174 |
+----------+-----------+------+
-- *******************
-- Joins
-- Zusammenfassungen
-- *******************
-- Abfrage der Eltern von Personen
select p1.FirstName,p2.LastName as ParentLastName,
p2.FirstName as ParentFirstName
from Person as p1
inner join Person as p2
on (p1.Father_Id = p2.id or p1.Mother_Id = p2.id);
+-----------+----------------+-----------------+
| FirstName | ParentLastName | ParentFirstName |
+-----------+----------------+-----------------+
| Peter | Turner | Wendy |
| Peter | Turner | Richard |
| Angie | Wilson | Hilary |
| Marvin | Turner | Peter |
| Marvin | Turner | Angie |
| Roxanne | Turner | Peter |
| Roxanne | Turner | Angie |
| Wendy | Wilson | Hilary |
+-----------+----------------+-----------------+
-- Abfrage welche Personen Eltern sind und Entfernung von doppelten Zeilen
select distinct p2.LastName as ParentLastName,
p2.FirstName as ParentFirstName
from Person as p1
inner join Person as p2
on (p1.Father_Id = p2.id or p1.Mother_Id = p2.id);
+----------------+-----------------+
| ParentLastName | ParentFirstName |
+----------------+-----------------+
| Turner | Wendy |
| Turner | Richard |
| Wilson | Hilary |
| Turner | Peter |
| Turner | Angie |
+----------------+-----------------+
-- Abfrage der Geschwister einer Person
select p1.LastName, p1.FirstName
from Person as p1
inner join Sibling as s
inner join Person as p2
on p1.id = s.from_Id and s.to_Id = p2.id
and p1.FirstName != 'Marvin';
+----------+-----------+
| LastName | FirstName |
+----------+-----------+
| Turner | Roxanne |
+----------+-----------+
-- Abfrage der Meldungen, die von einer Person geschickt wurden
select p1.FirstName as 'From',
p2.FirstName as 'To',
m.message
from Person as p1
inner join Message as m
inner join Person as p2
on p1.id = m.from_Id
and m.to_Id = p2.id
where (select id from Person where FirstName = 'Marvin');
+--------+--------+------------------------------------------+
| From | To | message |
+--------+--------+------------------------------------------+
| Peter | Angie | Nice to meet you |
| Angie | Peter | I also enjoy to meet you |
| Marvin | Peter | My system crashed. Can you help me? |
| Marvin | Angie | I'm hungry |
| Angie | Marvin | Just visit MC'Donalds |
| Peter | Marvin | Restart your system |
| Marvin | Peter | Could you help me with GMAT? |
| Marvin | Peter | Don't forget me and buy a big mac for me |
+--------+--------+------------------------------------------+
-- Abfrage der Meldungen, die an eine Person geschickt wurden
select p2.FirstName as 'From',
p1.FirstName as 'To',
m.message
from Person as p1
inner join Message as m
inner join Person as p2
on p1.id = m.to_Id
and m.from_Id = p2.id
where (select id from Person where FirstName = 'Marvin');
+--------+--------+------------------------------------------+
| From | To | message |
+--------+--------+------------------------------------------+
| Peter | Angie | Nice to meet you |
| Angie | Peter | I also enjoy to meet you |
| Marvin | Peter | My system crashed. Can you help me? |
| Marvin | Angie | I'm hungry |
| Angie | Marvin | Just visit MC'Donalds |
| Peter | Marvin | Restart your system |
| Marvin | Peter | Could you help me with GMAT? |
| Marvin | Peter | Don't forget me and buy a big mac for me |
+--------+--------+------------------------------------------+
| From | To | message |
+--------+--------+------------------------------------------+
| Peter | Angie | Nice to meet you |
| Angie | Peter | I also enjoy to meet you |
| Marvin | Peter | My system crashed. Can you help me? |
| Marvin | Angie | I'm hungry |
| Angie | Marvin | Just visit MC'Donalds |
| Peter | Marvin | Restart your system |
| Marvin | Peter | Could you help me with GMAT? |
| Marvin | Peter | Don't forget me and buy a big mac for me |
+--------+--------+------------------------------------------+
-- Abfrage der Meldungen, die von einer Person geschickt und gesendet wurden
select p1.FirstName,
If(m.from_Id = p1.id,'Sent to','Received from') 'S/R',
p2.Firstname,
m.message
from Person as p1
inner join Message as m
inner join Person as p2
on p1.id = m.to_Id
and m.from_Id = p2.id
or p2.id = m.to_Id
and m.from_Id = p1.id
where (select id from Person where FirstName = 'Marvin')
order by p1.LastName, p1.FirstName;
+-----------+---------------+-----------+------------------------------------------+
| FirstName | S/R | Firstname | message |
+-----------+---------------+-----------+------------------------------------------+
| Angie | Received from | Peter | Nice to meet you |
| Angie | Sent to | Marvin | Just visit MC'Donalds |
| Angie | Received from | Marvin | I'm hungry |
| Angie | Sent to | Peter | I also enjoy to meet you |
| Marvin | Received from | Peter | Restart your system |
| Marvin | Sent to | Peter | Don't forget me and buy a big mac for me |
| Marvin | Sent to | Peter | My system crashed. Can you help me? |
| Marvin | Received from | Angie | Just visit MC'Donalds |
| Marvin | Sent to | Peter | Could you help me with GMAT? |
| Marvin | Sent to | Angie | I'm hungry |
| Peter | Received from | Marvin | Could you help me with GMAT? |
| Peter | Received from | Angie | I also enjoy to meet you |
| Peter | Sent to | Marvin | Restart your system |
| Peter | Sent to | Angie | Nice to meet you |
| Peter | Received from | Marvin | Don't forget me and buy a big mac for me |
| Peter | Received from | Marvin | My system crashed. Can you help me? |
+-----------+---------------+-----------+------------------------------------------+
-- Abfrage der Anzahl von Meldungen, die von allen Personen gesendet oder empfangen wurden
select p1.FirstName,
If(m.from_Id = p1.id,'Sent','Received') 'S/R',
count(*) as 'Messages'
from Person as p1
inner join Message as m
inner join Person as p2
on p1.id = m.to_Id
and m.from_Id = p2.id
or p2.id = m.to_Id
and m.from_Id = p1.id
group by p1.FirstName,p1.LastName,m.from_Id = p1.id
order by p1.LastName, p1.FirstName;
+-----------+----------+----------+
| FirstName | S/R | Messages |
+-----------+----------+----------+
| Angie | Sent | 2 |
| Angie | Received | 2 |
| Marvin | Received | 2 |
| Marvin | Sent | 4 |
| Peter | Received | 4 |
| Peter | Sent | 2 |
+-----------+----------+----------+
-- Abfrage der Personen mit ihrer vollständigen Adresse
select LastName, FirstName, c.Name, c.ZIP, a.Street, a.No
from Person as p
inner join City as c
inner join Address as a
on p.Address_Id = a.id and a.City_Id = c.id
order by LastName, FirstName;
+----------+-----------+-----------+-------+----------------+------+
| LastName | FirstName | Name | ZIP | Street | No |
+----------+-----------+-----------+-------+----------------+------+
| Turner | Angie | Rochester | 87656 | Bondstreet | 13 |
| Turner | Marvin | Paris | 1234 | PlaceDeLetoile | 45 |
| Turner | Peter | Rochester | 87656 | Bondstreet | 13 |
| Turner | Richard | Wien | 52637 | BarneysStreet | 5 |
| Turner | Roxanne | Rochester | 87656 | Bondstreet | 13 |
| Turner | Wendy | Wien | 52637 | BarneysStreet | 5 |
| Wilson | Hilary | Berlin | 12987 | ShiningStreet | 10 |
| Wilson | Woodrow | Berlin | 12987 | ShiningStreet | 10 |
+----------+-----------+-----------+-------+----------------+------+
-- Abfrage welche Geschwister dieselben Freunde haben
select concat(L1,' ',F1) as person,concat(L2,' ',F2) as sibling, concat(p.LastName,' ',p.FirstName) as 'common friend' from Friend as f
inner join ( /* subselect to select all siblings */
select p1.Lastname as L1, p1.FirstName as F1, p2.LastName as L2, p2.FirstName as F2,s.*
from Person as p1
inner join Sibling as s
inner join Person as p2
on p1.id = s.from_Id
and p2.id = s.to_Id
and p1.id > p2.id) as s /* remove duplicates */
inner join Person as p /* join to get name of common friend */
where f.from_Id = s.from_Id and p.id = f.to_Id;
+----------------+---------------+---------------+
| person | sibling | common friend |
+----------------+---------------+---------------+
| Turner Roxanne | Turner Marvin | Chen Walter |
+----------------+---------------+---------------+
select p1.FirstName,
If(m.from_Id = p1.id,'Sent to','Received from') 'S/R',
p2.Firstname,
m.message
from Person as p1
inner join Message as m
inner join Person as p2
on p1.id = m.to_Id
and m.from_Id = p2.id
or p2.id = m.to_Id
and m.from_Id = p1.id
where (select id from Person where FirstName = 'Marvin')
order by p1.LastName, p1.FirstName;
+-----------+---------------+-----------+------------------------------------------+
| FirstName | S/R | Firstname | message |
+-----------+---------------+-----------+------------------------------------------+
| Angie | Received from | Peter | Nice to meet you |
| Angie | Sent to | Marvin | Just visit MC'Donalds |
| Angie | Received from | Marvin | I'm hungry |
| Angie | Sent to | Peter | I also enjoy to meet you |
| Marvin | Received from | Peter | Restart your system |
| Marvin | Sent to | Peter | Don't forget me and buy a big mac for me |
| Marvin | Sent to | Peter | My system crashed. Can you help me? |
| Marvin | Received from | Angie | Just visit MC'Donalds |
| Marvin | Sent to | Peter | Could you help me with GMAT? |
| Marvin | Sent to | Angie | I'm hungry |
| Peter | Received from | Marvin | Could you help me with GMAT? |
| Peter | Received from | Angie | I also enjoy to meet you |
| Peter | Sent to | Marvin | Restart your system |
| Peter | Sent to | Angie | Nice to meet you |
| Peter | Received from | Marvin | Don't forget me and buy a big mac for me |
| Peter | Received from | Marvin | My system crashed. Can you help me? |
+-----------+---------------+-----------+------------------------------------------+
-- Abfrage der Anzahl von Meldungen, die von allen Personen gesendet oder empfangen wurden
select p1.FirstName,
If(m.from_Id = p1.id,'Sent','Received') 'S/R',
count(*) as 'Messages'
from Person as p1
inner join Message as m
inner join Person as p2
on p1.id = m.to_Id
and m.from_Id = p2.id
or p2.id = m.to_Id
and m.from_Id = p1.id
group by p1.FirstName,p1.LastName,m.from_Id = p1.id
order by p1.LastName, p1.FirstName;
+-----------+----------+----------+
| FirstName | S/R | Messages |
+-----------+----------+----------+
| Angie | Sent | 2 |
| Angie | Received | 2 |
| Marvin | Received | 2 |
| Marvin | Sent | 4 |
| Peter | Received | 4 |
| Peter | Sent | 2 |
+-----------+----------+----------+
-- Abfrage der Personen mit ihrer vollständigen Adresse
select LastName, FirstName, c.Name, c.ZIP, a.Street, a.No
from Person as p
inner join City as c
inner join Address as a
on p.Address_Id = a.id and a.City_Id = c.id
order by LastName, FirstName;
+----------+-----------+-----------+-------+----------------+------+
| LastName | FirstName | Name | ZIP | Street | No |
+----------+-----------+-----------+-------+----------------+------+
| Turner | Angie | Rochester | 87656 | Bondstreet | 13 |
| Turner | Marvin | Paris | 1234 | PlaceDeLetoile | 45 |
| Turner | Peter | Rochester | 87656 | Bondstreet | 13 |
| Turner | Richard | Wien | 52637 | BarneysStreet | 5 |
| Turner | Roxanne | Rochester | 87656 | Bondstreet | 13 |
| Turner | Wendy | Wien | 52637 | BarneysStreet | 5 |
| Wilson | Hilary | Berlin | 12987 | ShiningStreet | 10 |
| Wilson | Woodrow | Berlin | 12987 | ShiningStreet | 10 |
+----------+-----------+-----------+-------+----------------+------+
-- Abfrage welche Geschwister dieselben Freunde haben
select concat(L1,' ',F1) as person,concat(L2,' ',F2) as sibling, concat(p.LastName,' ',p.FirstName) as 'common friend' from Friend as f
inner join ( /* subselect to select all siblings */
select p1.Lastname as L1, p1.FirstName as F1, p2.LastName as L2, p2.FirstName as F2,s.*
from Person as p1
inner join Sibling as s
inner join Person as p2
on p1.id = s.from_Id
and p2.id = s.to_Id
and p1.id > p2.id) as s /* remove duplicates */
inner join Person as p /* join to get name of common friend */
where f.from_Id = s.from_Id and p.id = f.to_Id;
+----------------+---------------+---------------+
| person | sibling | common friend |
+----------------+---------------+---------------+
| Turner Roxanne | Turner Marvin | Chen Walter |
+----------------+---------------+---------------+
*** Hinweis ***
Kommentare sind erwünscht. Aber um lästige Spamposts abweisen zu können gibt es ein paar Dinge die zu beachten sind: