Exercițiul 1. Afișați numele și prenumele plus numărul icq pentru persoanele care au un număr icq
Exercițiul 2. Afișați numele și prenumele plus numărul ICQ plus numărul de linie fixă pentru persoanele care au un număr ICQ ȘI o linie fixă. Trebuie să adăugaâi la tabelul contact de două ori.
Exercițiul 3. Afișați numele și prenumele plus (dacă există) numărul ICQ pentru TOATE persoanele
Exercițiul 4. Creați o listă care conține TOATE hobby-urile plus persoanele potrivite (dacă există)
Exercițiul 5. Este posibil ca una dintre cele trei join exterioare să conţină mai puţine rânduri decât join interioară corespunzătoare?
SELECT p.id, p.firstname, p.lastname, c.contact_value
FROM person p
JOIN contact c ON p.id = c.person_id
WHERE c.contact_type = 'icq';
Rezolvare 2
SELECT p.id, p.firstname, p.lastname,
c1.contact_value as icq,
c2.contact_value as "fixed line" -- looks like previous, but is different
FROM person p
JOIN contact c1 ON p.id = c1.person_id
JOIN contact c2 ON p.id = c2.person_id -- it's a second (virtual) incarnation of contact table
WHERE c1.contact_type = 'icq' -- from first incarnation
AND c2.contact_type = 'fixed line'; -- from second incarnation
-- In this example of an INNER JOIN we can convert the WHERE part to an additional JOIN criterion.
-- This may clarify the intention of the command. But be careful: This shifting in combination with
-- one of the OUTER JOINs may lead to different results.
SELECT p.id, p.firstname, p.lastname, c1.contact_value as icq, c2.contact_value as "fixed line"
FROM person p
JOIN contact c1 ON p.id = c1.person_id AND c1.contact_type = 'icq'
JOIN contact c2 ON p.id = c2.person_id AND c2.contact_type = 'fixed line';
Rezolvare 3
-- To retrieve ALL persons, it's necessary to use a LEFT join.
-- But the first approach is not what we expect! In this example, the LEFT JOIN is evaluated first
-- and creates an intermediate table with null-values in contact_type (eliminate the
-- WHERE clause to see this intermediate result). These rows and all other except the
-- one with 'ICQ' are then thrown away by evaluating the WHERE clause.
SELECT p.id, p.firstname, p.lastname, c.contact_value
FROM person p
LEFT JOIN contact c ON p.id = c.person_id
WHERE c.contact_type = 'icq';
-- It's necessary to formulate the search criterion as part of the JOIN. Unlike with
-- the INNER JOIN in the previous example with (LEFT/FULL/RIGHT) OUTER JOINs it is not possible
-- to shift it to the WHERE clause.
SELECT p.id, p.firstname, p.lastname, c.contact_value
FROM person p
LEFT JOIN contact c ON p.id = c.person_id AND c.contact_type = 'icq';
Rezolvare 4
SELECT p.id p_id, p.firstname, p.lastname, h.hobbyname, h.id h_id
FROM person p
RIGHT JOIN person_hobby ph ON p.id = ph.person_id
RIGHT JOIN hobby h ON ph.hobby_id = h.id
ORDER BY h.hobbyname, p.lastname, p.firstname;
Rezolvare 5
Nu. Toate cele patru tipuri de join conțin aceleași rânduri cu valori de potrivire a coloanelor. În plus, join exterioare conțin rânduri în care valorile coloanei nu se potrivesc - dacă există o astfel de situație.
(Include texte din Wikibooks traduse și adaptate de Nicolae Sfetcu)
Descoperă mai multe la MultiMedia
Abonează-te ca să primești ultimele articole prin email.



Lasă un răspuns