Difference between revisions of "Self-Joins"

From rbachwiki
Jump to navigation Jump to search
 
Line 3: Line 3:
  FROM customers c, customers r
  FROM customers c, customers r
  WHERE  c.referred = r.customer#;
  WHERE  c.referred = r.customer#;
=== Self Joins using the JOIN Method ===
=== Self Joins using the JOIN Method ===
  SELECT r.firstname, r.lastname, c.referred, c.lastname "Referred"
  SELECT r.firstname, r.lastname, c.referred, c.lastname "Referred"
  FROM customers c JOIN customers r
  FROM customers c JOIN customers r
   ON c.referred = r.customer#;
   ON c.referred = r.customer#;
[[#Select Command|Back To Top]]-[[Main_Page| Home]] - [[Oracle_SQL|Category]]
[[#Select Command|Back To Top]]-[[Main_Page| Home]] - [[Oracle_SQL|Category]]

Latest revision as of 00:45, 24 October 2017

Self Joins are used when a table must be joined to itself to retrieve the data you need. Table aliases are required in the FROM clause to perform self-join

SELECT r.firstname, r.lastname, r.referred, c.lastname "Referred"
FROM customers c, customers r
WHERE  c.referred = r.customer#;

Self Joins using the JOIN Method

SELECT r.firstname, r.lastname, c.referred, c.lastname "Referred"
FROM customers c JOIN customers r
 ON c.referred = r.customer#;

Back To Top- Home - Category