17 CREATE TABLE JOIN3_TBL (
23 CREATE TABLE JOIN4_TBL (
28 INSERT INTO J1_TBL VALUES (1, 3, 'one');
29 INSERT INTO J1_TBL VALUES (2, 2, 'two');
30 INSERT INTO J1_TBL VALUES (3, 1, 'three');
31 INSERT INTO J1_TBL VALUES (4, 0, 'four');
33 INSERT INTO J2_TBL VALUES (1, -1);
34 INSERT INTO J2_TBL VALUES (2, 2);
35 INSERT INTO J2_TBL VALUES (3, -3);
36 INSERT INTO J2_TBL VALUES (2, 4);
40 -- Make sure that table/column aliases are supported
41 -- before diving into more complex join syntax.
51 FROM J1_TBL AS t1 (a, b, c);
54 FROM J1_TBL t1 (a, b, c);
57 FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
59 SELECT '' AS "xxx", t1.a, t2.e
60 FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
66 -- Qualifications are not allowed on cross joins,
67 -- which degenerate into a standard unqualified inner join.
71 FROM J1_TBL CROSS JOIN J2_TBL;
74 SELECT '' AS "xxx", i, k, t
75 FROM J1_TBL CROSS JOIN J2_TBL;
77 -- resolve previous ambiguity by specifying the table name
78 SELECT '' AS "xxx", t1.i, k, t
79 FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
81 SELECT '' AS "xxx", ii, tt, kk
82 FROM (J1_TBL CROSS JOIN J2_TBL)
83 AS tx (ii, jj, tt, ii2, kk);
85 SELECT '' AS "xxx", tx.ii, tx.jj, tx.kk
86 FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
87 AS tx (ii, jj, tt, ii2, kk);
92 -- Inner joins (equi-joins)
97 -- Inner joins (equi-joins) with USING clause
98 -- The USING syntax changes the shape of the resulting table
99 -- by including a column in the USING clause only once in the result.
102 -- Inner equi-join on specified column
103 SELECT '' AS "xxx", *
104 FROM J1_TBL INNER JOIN J2_TBL USING (i);
106 -- Same as above, slightly different syntax
107 SELECT '' AS "xxx", *
108 FROM J1_TBL JOIN J2_TBL USING (i);
110 SELECT '' AS "xxx", *
111 FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a);
113 SELECT '' AS "xxx", *
114 FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b);
119 -- Inner equi-join on all columns with the same name
122 SELECT '' AS "xxx", *
123 FROM J1_TBL NATURAL JOIN J2_TBL;
125 SELECT '' AS "xxx", *
126 FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
128 SELECT '' AS "xxx", *
129 FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
131 SELECT '' AS "xxx", *
132 FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
134 -- mismatch number of columns
135 -- currently, Postgres will fill in with underlying names
136 SELECT '' AS "xxx", *
137 FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
141 -- Inner joins (equi-joins)
144 SELECT '' AS "xxx", *
145 FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
147 SELECT '' AS "xxx", *
148 FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
150 SELECT '' AS "xxx", *
151 FROM J1_TBL CROSS JOIN J2_TBL;
158 SELECT '' AS "xxx", *
159 FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
166 SELECT '' AS "xxx", *
167 FROM J1_TBL OUTER JOIN J2_TBL USING (i);
169 SELECT '' AS "xxx", *
170 FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i);
172 SELECT '' AS "xxx", *
173 FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
175 SELECT '' AS "xxx", *
176 FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i);
180 -- More complicated constructs