1999-02-23 08:27:13 +01:00
|
|
|
--
|
2000-01-06 07:41:55 +01:00
|
|
|
-- JOIN
|
2000-09-12 23:07:18 +02:00
|
|
|
-- Test JOIN clauses
|
1999-02-23 08:27:13 +01:00
|
|
|
--
|
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
CREATE TABLE J1_TBL (
|
1999-02-23 08:27:13 +01:00
|
|
|
i integer,
|
|
|
|
j integer,
|
2000-01-05 07:07:58 +01:00
|
|
|
t text
|
1999-02-23 08:27:13 +01:00
|
|
|
);
|
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
CREATE TABLE J2_TBL (
|
1999-02-23 08:27:13 +01:00
|
|
|
i integer,
|
|
|
|
k integer
|
|
|
|
);
|
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
|
2000-11-06 17:03:47 +01:00
|
|
|
INSERT INTO J1_TBL VALUES (1, 4, 'one');
|
|
|
|
INSERT INTO J1_TBL VALUES (2, 3, 'two');
|
|
|
|
INSERT INTO J1_TBL VALUES (3, 2, 'three');
|
|
|
|
INSERT INTO J1_TBL VALUES (4, 1, 'four');
|
|
|
|
INSERT INTO J1_TBL VALUES (5, 0, 'five');
|
|
|
|
INSERT INTO J1_TBL VALUES (6, 6, 'six');
|
|
|
|
INSERT INTO J1_TBL VALUES (7, 7, 'seven');
|
|
|
|
INSERT INTO J1_TBL VALUES (8, 8, 'eight');
|
|
|
|
INSERT INTO J1_TBL VALUES (0, NULL, 'zero');
|
|
|
|
INSERT INTO J1_TBL VALUES (NULL, NULL, 'null');
|
|
|
|
INSERT INTO J1_TBL VALUES (NULL, 0, 'zero');
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
INSERT INTO J2_TBL VALUES (1, -1);
|
|
|
|
INSERT INTO J2_TBL VALUES (2, 2);
|
|
|
|
INSERT INTO J2_TBL VALUES (3, -3);
|
|
|
|
INSERT INTO J2_TBL VALUES (2, 4);
|
2000-09-12 23:07:18 +02:00
|
|
|
INSERT INTO J2_TBL VALUES (5, -5);
|
2000-11-06 17:03:47 +01:00
|
|
|
INSERT INTO J2_TBL VALUES (5, -5);
|
|
|
|
INSERT INTO J2_TBL VALUES (0, NULL);
|
|
|
|
INSERT INTO J2_TBL VALUES (NULL, NULL);
|
|
|
|
INSERT INTO J2_TBL VALUES (NULL, 0);
|
2000-02-15 04:31:33 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- CORRELATION NAMES
|
|
|
|
-- Make sure that table/column aliases are supported
|
|
|
|
-- before diving into more complex join syntax.
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL AS tx;
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL tx;
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL AS t1 (a, b, c);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL t1 (a, b, c);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", t1.a, t2.e
|
|
|
|
FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
|
|
|
|
WHERE t1.a = t2.d;
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
--
|
|
|
|
-- CROSS JOIN
|
|
|
|
-- Qualifications are not allowed on cross joins,
|
|
|
|
-- which degenerate into a standard unqualified inner join.
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL CROSS JOIN J2_TBL;
|
2000-01-05 07:07:58 +01:00
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
-- ambiguous column
|
2000-01-05 07:07:58 +01:00
|
|
|
SELECT '' AS "xxx", i, k, t
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL CROSS JOIN J2_TBL;
|
|
|
|
|
|
|
|
-- resolve previous ambiguity by specifying the table name
|
|
|
|
SELECT '' AS "xxx", t1.i, k, t
|
|
|
|
FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
|
2000-01-05 07:07:58 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", ii, tt, kk
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM (J1_TBL CROSS JOIN J2_TBL)
|
|
|
|
AS tx (ii, jj, tt, ii2, kk);
|
2000-01-05 07:07:58 +01:00
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
SELECT '' AS "xxx", tx.ii, tx.jj, tx.kk
|
|
|
|
FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
|
|
|
|
AS tx (ii, jj, tt, ii2, kk);
|
2000-01-05 07:07:58 +01:00
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
|
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
|
|
|
|
--
|
1999-02-23 08:27:13 +01:00
|
|
|
--
|
|
|
|
-- Inner joins (equi-joins)
|
|
|
|
--
|
2000-01-05 07:07:58 +01:00
|
|
|
--
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Inner joins (equi-joins) with USING clause
|
|
|
|
-- The USING syntax changes the shape of the resulting table
|
|
|
|
-- by including a column in the USING clause only once in the result.
|
|
|
|
--
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
-- Inner equi-join on specified column
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL INNER JOIN J2_TBL USING (i);
|
|
|
|
|
|
|
|
-- Same as above, slightly different syntax
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL JOIN J2_TBL USING (i);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b);
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- NATURAL JOIN
|
2000-01-05 07:07:58 +01:00
|
|
|
-- Inner equi-join on all columns with the same name
|
2000-02-15 04:31:33 +01:00
|
|
|
--
|
|
|
|
|
1999-02-23 08:27:13 +01:00
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL NATURAL JOIN J2_TBL;
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
|
|
|
|
|
|
|
|
-- mismatch number of columns
|
|
|
|
-- currently, Postgres will fill in with underlying names
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
|
2000-01-05 07:07:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Inner joins (equi-joins)
|
|
|
|
--
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Non-equi-joins
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Outer joins
|
2000-11-06 17:03:47 +01:00
|
|
|
-- Note that OUTER is a noise word
|
1999-02-23 08:27:13 +01:00
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-11-06 17:03:47 +01:00
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL LEFT JOIN J2_TBL USING (i);
|
|
|
|
|
1999-02-23 08:27:13 +01:00
|
|
|
SELECT '' AS "xxx", *
|
2000-02-15 04:31:33 +01:00
|
|
|
FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-11-06 17:03:47 +01:00
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i);
|
|
|
|
|
1999-02-23 08:27:13 +01:00
|
|
|
SELECT '' AS "xxx", *
|
2000-09-12 23:07:18 +02:00
|
|
|
FROM J1_TBL FULL JOIN J2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-11-06 17:03:47 +01:00
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
|
|
|
|
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
--
|
|
|
|
-- More complicated constructs
|
|
|
|
--
|
|
|
|
|
2000-09-12 23:07:18 +02:00
|
|
|
-- UNION JOIN isn't implemented yet
|
|
|
|
SELECT '' AS "xxx", *
|
|
|
|
FROM J1_TBL UNION JOIN J2_TBL;
|
|
|
|
|
1999-02-23 08:27:13 +01:00
|
|
|
--
|
|
|
|
-- Clean up
|
|
|
|
--
|
|
|
|
|
2000-02-15 04:31:33 +01:00
|
|
|
DROP TABLE J1_TBL;
|
|
|
|
DROP TABLE J2_TBL;
|
1999-02-23 08:27:13 +01:00
|
|
|
|