1999-02-23 08:27:13 +01:00
|
|
|
--
|
2000-01-06 07:41:55 +01:00
|
|
|
-- JOIN
|
1999-02-23 08:27:13 +01:00
|
|
|
-- Test join clauses
|
|
|
|
--
|
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
CREATE TABLE JOIN1_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
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE JOIN2_TBL (
|
|
|
|
i integer,
|
|
|
|
k integer
|
|
|
|
);
|
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
CREATE TABLE JOIN3_TBL (
|
|
|
|
i integer,
|
|
|
|
j integer,
|
|
|
|
y integer
|
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TABLE JOIN4_TBL (
|
|
|
|
k integer,
|
|
|
|
z integer
|
|
|
|
);
|
|
|
|
|
|
|
|
INSERT INTO JOIN1_TBL VALUES (1, 3, 'one');
|
|
|
|
INSERT INTO JOIN1_TBL VALUES (2, 2, 'two');
|
|
|
|
INSERT INTO JOIN1_TBL VALUES (3, 1, 'three');
|
|
|
|
INSERT INTO JOIN1_TBL VALUES (4, 0, 'four');
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
INSERT INTO JOIN2_TBL VALUES (1, -1);
|
|
|
|
INSERT INTO JOIN2_TBL VALUES (2, 2);
|
|
|
|
INSERT INTO JOIN2_TBL VALUES (3, -3);
|
|
|
|
INSERT INTO JOIN2_TBL VALUES (2, 4);
|
|
|
|
|
|
|
|
|
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", *
|
|
|
|
FROM JOIN1_TBL CROSS JOIN JOIN2_TBL;
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", i, k, t
|
|
|
|
FROM JOIN1_TBL CROSS JOIN JOIN2_TBL;
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", ii, tt, kk
|
|
|
|
FROM JOIN1_TBL CROSS JOIN JOIN2_TBL AS JT (ii, jj, tt, ii2, kk);
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", jt.ii, jt.jj, jt.kk
|
|
|
|
FROM JOIN1_TBL CROSS JOIN JOIN2_TBL AS JT (ii, jj, tt, ii2, kk);
|
|
|
|
|
|
|
|
|
|
|
|
--
|
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-01-05 07:07:58 +01:00
|
|
|
-- Inner equi-join on all columns with the same name
|
1999-02-23 08:27:13 +01:00
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL NATURAL JOIN JOIN2_TBL;
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
-- Inner equi-join on specified column
|
1999-02-23 08:27:13 +01:00
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL INNER JOIN JOIN2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
-- Same as above, slightly different syntax
|
1999-02-23 08:27:13 +01:00
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL JOIN JOIN2_TBL USING (i);
|
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Inner joins (equi-joins)
|
|
|
|
--
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL JOIN JOIN2_TBL ON (JOIN1_TBL.i = JOIN2_TBL.i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL JOIN JOIN2_TBL ON (JOIN1_TBL.i = JOIN2_TBL.k);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL CROSS JOIN JOIN2_TBL;
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Non-equi-joins
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL JOIN JOIN2_TBL ON (JOIN1_TBL.i <= JOIN2_TBL.k);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Outer joins
|
|
|
|
--
|
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL OUTER JOIN JOIN2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL LEFT OUTER JOIN JOIN2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL RIGHT OUTER JOIN JOIN2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
SELECT '' AS "xxx", *
|
2000-01-05 07:07:58 +01:00
|
|
|
FROM JOIN1_TBL FULL OUTER JOIN JOIN2_TBL USING (i);
|
1999-02-23 08:27:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
--
|
|
|
|
-- More complicated constructs
|
|
|
|
--
|
|
|
|
|
|
|
|
--
|
|
|
|
-- Clean up
|
|
|
|
--
|
|
|
|
|
2000-01-05 07:07:58 +01:00
|
|
|
DROP TABLE JOIN1_TBL;
|
1999-02-23 08:27:13 +01:00
|
|
|
DROP TABLE JOIN2_TBL;
|
|
|
|
|