18 INSERT INTO J1_TBL VALUES (1, 4, 'one');
19 INSERT INTO J1_TBL VALUES (2, 3, 'two');
20 INSERT INTO J1_TBL VALUES (3, 2, 'three');
21 INSERT INTO J1_TBL VALUES (4, 1, 'four');
22 INSERT INTO J1_TBL VALUES (5, 0, 'five');
23 INSERT INTO J1_TBL VALUES (6, 6, 'six');
24 INSERT INTO J1_TBL VALUES (7, 7, 'seven');
25 INSERT INTO J1_TBL VALUES (8, 8, 'eight');
26 INSERT INTO J1_TBL VALUES (0, NULL, 'zero');
27 INSERT INTO J1_TBL VALUES (NULL, NULL, 'null');
28 INSERT INTO J1_TBL VALUES (NULL, 0, 'zero');
30 INSERT INTO J2_TBL VALUES (1, -1);
31 INSERT INTO J2_TBL VALUES (2, 2);
32 INSERT INTO J2_TBL VALUES (3, -3);
33 INSERT INTO J2_TBL VALUES (2, 4);
34 INSERT INTO J2_TBL VALUES (5, -5);
35 INSERT INTO J2_TBL VALUES (5, -5);
36 INSERT INTO J2_TBL VALUES (0, NULL);
37 INSERT INTO J2_TBL VALUES (NULL, NULL);
38 INSERT INTO J2_TBL VALUES (NULL, 0);
42 -- Make sure that table/column aliases are supported
43 -- before diving into more complex join syntax.
53 FROM J1_TBL AS t1 (a, b, c);
56 FROM J1_TBL t1 (a, b, c);
59 FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
61 SELECT '' AS "xxx", t1.a, t2.e
62 FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
68 -- Qualifications are not allowed on cross joins,
69 -- which degenerate into a standard unqualified inner join.
73 FROM J1_TBL CROSS JOIN J2_TBL;
76 SELECT '' AS "xxx", i, k, t
77 FROM J1_TBL CROSS JOIN J2_TBL;
79 -- resolve previous ambiguity by specifying the table name
80 SELECT '' AS "xxx", t1.i, k, t
81 FROM J1_TBL t1 CROSS JOIN J2_TBL t2;
83 SELECT '' AS "xxx", ii, tt, kk
84 FROM (J1_TBL CROSS JOIN J2_TBL)
85 AS tx (ii, jj, tt, ii2, kk);
87 SELECT '' AS "xxx", tx.ii, tx.jj, tx.kk
88 FROM (J1_TBL t1 (a, b, c) CROSS JOIN J2_TBL t2 (d, e))
89 AS tx (ii, jj, tt, ii2, kk);
92 FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
97 -- Inner joins (equi-joins)
102 -- Inner joins (equi-joins) with USING clause
103 -- The USING syntax changes the shape of the resulting table
104 -- by including a column in the USING clause only once in the result.
107 -- Inner equi-join on specified column
108 SELECT '' AS "xxx", *
109 FROM J1_TBL INNER JOIN J2_TBL USING (i);
111 -- Same as above, slightly different syntax
112 SELECT '' AS "xxx", *
113 FROM J1_TBL JOIN J2_TBL USING (i);
115 SELECT '' AS "xxx", *
116 FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
119 SELECT '' AS "xxx", *
120 FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b)
126 -- Inner equi-join on all columns with the same name
129 SELECT '' AS "xxx", *
130 FROM J1_TBL NATURAL JOIN J2_TBL;
132 SELECT '' AS "xxx", *
133 FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
135 SELECT '' AS "xxx", *
136 FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
138 -- mismatch number of columns
139 -- currently, Postgres will fill in with underlying names
140 SELECT '' AS "xxx", *
141 FROM J1_TBL t1 (a, b) NATURAL JOIN J2_TBL t2 (a);
145 -- Inner joins (equi-joins)
148 SELECT '' AS "xxx", *
149 FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
151 SELECT '' AS "xxx", *
152 FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
159 SELECT '' AS "xxx", *
160 FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
165 -- Note that OUTER is a noise word
168 SELECT '' AS "xxx", *
169 FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i)
172 SELECT '' AS "xxx", *
173 FROM J1_TBL LEFT JOIN J2_TBL USING (i)
176 SELECT '' AS "xxx", *
177 FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
179 SELECT '' AS "xxx", *
180 FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
182 SELECT '' AS "xxx", *
183 FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i)
186 SELECT '' AS "xxx", *
187 FROM J1_TBL FULL JOIN J2_TBL USING (i)
190 SELECT '' AS "xxx", *
191 FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
193 SELECT '' AS "xxx", *
194 FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
198 -- More complicated constructs
202 -- Multiway full join
205 CREATE TABLE t1 (name TEXT, n INTEGER);
206 CREATE TABLE t2 (name TEXT, n INTEGER);
207 CREATE TABLE t3 (name TEXT, n INTEGER);
209 INSERT INTO t1 VALUES ( 'aa', 11 );
210 INSERT INTO t2 VALUES ( 'aa', 12 );
211 INSERT INTO t2 VALUES ( 'bb', 22 );
212 INSERT INTO t2 VALUES ( 'dd', 42 );
213 INSERT INTO t3 VALUES ( 'aa', 13 );
214 INSERT INTO t3 VALUES ( 'bb', 23 );
215 INSERT INTO t3 VALUES ( 'cc', 33 );
217 SELECT * FROM t1 FULL JOIN t2 USING (name) FULL JOIN t3 USING (name);
220 -- Test interactions of join syntax and subqueries
223 -- Basic cases (we expect planner to pull up the subquery here)
225 (SELECT * FROM t2) as s2
227 (SELECT * FROM t3) s3
231 (SELECT * FROM t2) as s2
233 (SELECT * FROM t3) s3
237 (SELECT * FROM t2) as s2
239 (SELECT * FROM t3) s3
242 -- Cases with non-nullable expressions in subquery results;
243 -- make sure these go to null as expected
245 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
247 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
250 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
252 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
255 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
257 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
260 (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
262 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
264 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
267 (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
269 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
271 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
274 (SELECT name, n as s1_n FROM t1) as s1
277 (SELECT name, n as s2_n FROM t2) as s2
279 (SELECT name, n as s3_n FROM t3) as s3
283 (SELECT name, n as s1_n FROM t1) as s1
286 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
288 (SELECT name, n as s3_n FROM t3) as s3
292 -- Test for propagation of nullability constraints into sub-joins
294 create temp table x (x1 int, x2 int);
295 insert into x values (1,11);
296 insert into x values (2,22);
297 insert into x values (3,null);
298 insert into x values (4,44);
299 insert into x values (5,null);
301 create temp table y (y1 int, y2 int);
302 insert into y values (1,111);
303 insert into y values (2,222);
304 insert into y values (3,333);
305 insert into y values (4,null);
310 select * from x left join y on (x1 = y1 and x2 is not null);
311 select * from x left join y on (x1 = y1 and y2 is not null);
313 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
315 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
316 on (x1 = xx1 and x2 is not null);
317 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
318 on (x1 = xx1 and y2 is not null);
319 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
320 on (x1 = xx1 and xx2 is not null);
321 -- these should NOT give the same answers as above
322 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
323 on (x1 = xx1) where (x2 is not null);
324 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
325 on (x1 = xx1) where (y2 is not null);
326 select * from (x left join y on (x1 = y1)) left join x xx(xx1,xx2)
327 on (x1 = xx1) where (xx2 is not null);
330 -- regression test: check for bug with propagation of implied equality
333 select count(*) from tenk1 a where unique1 in
334 (select unique1 from tenk1 b join tenk1 c using (unique1)
335 where b.unique2 = 42);
338 -- regression test: check for failure to generate a plan with multiple
339 -- degenerate IN clauses
341 select count(*) from tenk1 x where
342 x.unique1 in (select a.f1 from int4_tbl a,float8_tbl b where a.f1=b.f1) and
344 x.unique1 in (select aa.f1 from int4_tbl aa,float8_tbl bb where aa.f1=bb.f1);
358 -- Both DELETE and UPDATE allow the specification of additional tables
359 -- to "join" against to determine which rows should be modified.
361 CREATE TEMP TABLE t1 (a int, b int);
362 CREATE TEMP TABLE t2 (a int, b int);
363 CREATE TEMP TABLE t3 (x int, y int);
365 INSERT INTO t1 VALUES (5, 10);
366 INSERT INTO t1 VALUES (15, 20);
367 INSERT INTO t1 VALUES (100, 100);
368 INSERT INTO t1 VALUES (200, 1000);
369 INSERT INTO t2 VALUES (200, 2000);
370 INSERT INTO t3 VALUES (5, 20);
371 INSERT INTO t3 VALUES (6, 7);
372 INSERT INTO t3 VALUES (7, 8);
373 INSERT INTO t3 VALUES (500, 100);
375 DELETE FROM t3 USING t1 table1 WHERE t3.x = table1.a;
377 DELETE FROM t3 USING t1 JOIN t2 USING (a) WHERE t3.x > t1.a;
379 DELETE FROM t3 USING t3 t3_other WHERE t3.x = t3_other.x AND t3.y = t3_other.y;
383 -- regression test for 8.1 merge right join bug
386 CREATE TEMP TABLE tt1 ( tt1_id int4, joincol int4 );
387 INSERT INTO tt1 VALUES (1, 11);
388 INSERT INTO tt1 VALUES (2, NULL);
390 CREATE TEMP TABLE tt2 ( tt2_id int4, joincol int4 );
391 INSERT INTO tt2 VALUES (21, 11);
392 INSERT INTO tt2 VALUES (22, 11);
394 set enable_hashjoin to off;
395 set enable_nestloop to off;
397 -- these should give the same results
399 select tt1.*, tt2.* from tt1 left join tt2 on tt1.joincol = tt2.joincol;
401 select tt1.*, tt2.* from tt2 right join tt1 on tt1.joincol = tt2.joincol;
403 reset enable_hashjoin;
404 reset enable_nestloop;
407 -- regression test for 8.2 bug with improper re-ordering of left joins
410 create temp table tt3(f1 int, f2 text);
411 insert into tt3 select x, repeat('xyzzy', 100) from generate_series(1,10000) x;
412 create index tt3i on tt3(f1);
415 create temp table tt4(f1 int);
416 insert into tt4 values (0),(1),(9999);
423 FROM tt3 b LEFT JOIN tt3 c ON (b.f1 = c.f1)
425 ) AS d ON (a.f1 = d.f1)
429 -- regression test for problems of the sort depicted in bug #3494
432 create temp table tt5(f1 int, f2 int);
433 create temp table tt6(f1 int, f2 int);
435 insert into tt5 values(1, 10);
436 insert into tt5 values(1, 11);
438 insert into tt6 values(1, 9);
439 insert into tt6 values(1, 2);
440 insert into tt6 values(2, 9);
442 select * from tt5,tt6 where tt5.f1 = tt6.f1 and tt5.f1 = tt5.f2 - tt6.f2;
445 -- regression test for problems of the sort depicted in bug #3588
448 create temp table xx (pkxx int);
449 create temp table yy (pkyy int, pkxx int);
451 insert into xx values (1);
452 insert into xx values (2);
453 insert into xx values (3);
455 insert into yy values (101, 1);
456 insert into yy values (201, 2);
457 insert into yy values (301, NULL);
459 select yy.pkyy as yy_pkyy, yy.pkxx as yy_pkxx, yya.pkyy as yya_pkyy,
460 xxa.pkxx as xxa_pkxx, xxb.pkxx as xxb_pkxx
462 left join (SELECT * FROM yy where pkyy = 101) as yya ON yy.pkyy = yya.pkyy
463 left join xx xxa on yya.pkxx = xxa.pkxx
464 left join xx xxb on coalesce (xxa.pkxx, 1) = xxb.pkxx;
467 -- regression test for improper pushing of constants across outer-join clauses
468 -- (as seen in early 8.2.x releases)
471 create temp table zt1 (f1 int primary key);
472 create temp table zt2 (f2 int primary key);
473 create temp table zt3 (f3 int primary key);
474 insert into zt1 values(53);
475 insert into zt2 values(53);
478 zt2 left join zt3 on (f2 = f3)
479 left join zt1 on (f3 = f1)
482 create temp view zv1 as select *,'dummy'::text AS junk from zt1;
485 zt2 left join zt3 on (f2 = f3)
486 left join zv1 on (f3 = f1)
490 -- regression test for improper extraction of OR indexqual conditions
491 -- (as seen in early 8.3.x releases)
494 select a.unique2, a.ten, b.tenthous, b.unique2, b.hundred
495 from tenk1 a left join tenk1 b on a.unique2 = b.tenthous
496 where a.unique1 = 42 and
497 ((b.unique2 is null and a.ten = 2) or b.hundred = 3);