]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/join.sql
Second try at fixing join alias variables. Instead of attaching miscellaneous
[postgresql] / src / test / regress / sql / join.sql
1 --
2 -- JOIN
3 -- Test JOIN clauses
4 --
5
6 CREATE TABLE J1_TBL (
7   i integer,
8   j integer,
9   t text
10 );
11
12 CREATE TABLE J2_TBL (
13   i integer,
14   k integer
15 );
16
17
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');
29
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);
39
40 --
41 -- CORRELATION NAMES
42 -- Make sure that table/column aliases are supported
43 -- before diving into more complex join syntax.
44 --
45
46 SELECT '' AS "xxx", *
47   FROM J1_TBL AS tx;
48
49 SELECT '' AS "xxx", *
50   FROM J1_TBL tx;
51
52 SELECT '' AS "xxx", *
53   FROM J1_TBL AS t1 (a, b, c);
54
55 SELECT '' AS "xxx", *
56   FROM J1_TBL t1 (a, b, c);
57
58 SELECT '' AS "xxx", *
59   FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e);
60
61 SELECT '' AS "xxx", t1.a, t2.e
62   FROM J1_TBL t1 (a, b, c), J2_TBL t2 (d, e)
63   WHERE t1.a = t2.d;
64
65
66 --
67 -- CROSS JOIN
68 -- Qualifications are not allowed on cross joins,
69 -- which degenerate into a standard unqualified inner join.
70 --
71
72 SELECT '' AS "xxx", *
73   FROM J1_TBL CROSS JOIN J2_TBL;
74
75 -- ambiguous column
76 SELECT '' AS "xxx", i, k, t
77   FROM J1_TBL CROSS JOIN J2_TBL;
78
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;
82
83 SELECT '' AS "xxx", ii, tt, kk
84   FROM (J1_TBL CROSS JOIN J2_TBL)
85     AS tx (ii, jj, tt, ii2, kk);
86
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);
90
91 SELECT '' AS "xxx", *
92   FROM J1_TBL CROSS JOIN J2_TBL a CROSS JOIN J2_TBL b;
93
94
95 --
96 --
97 -- Inner joins (equi-joins)
98 --
99 --
100
101 --
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.
105 --
106
107 -- Inner equi-join on specified column
108 SELECT '' AS "xxx", *
109   FROM J1_TBL INNER JOIN J2_TBL USING (i);
110
111 -- Same as above, slightly different syntax
112 SELECT '' AS "xxx", *
113   FROM J1_TBL JOIN J2_TBL USING (i);
114
115 SELECT '' AS "xxx", *
116   FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, d) USING (a)
117   ORDER BY a, d;
118
119 SELECT '' AS "xxx", *
120   FROM J1_TBL t1 (a, b, c) JOIN J2_TBL t2 (a, b) USING (b)
121   ORDER BY b, t1.a;
122
123
124 --
125 -- NATURAL JOIN
126 -- Inner equi-join on all columns with the same name
127 --
128
129 SELECT '' AS "xxx", *
130   FROM J1_TBL NATURAL JOIN J2_TBL;
131
132 SELECT '' AS "xxx", *
133   FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (a, d);
134
135 SELECT '' AS "xxx", *
136   FROM J1_TBL t1 (a, b, c) NATURAL JOIN J2_TBL t2 (d, a);
137
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);
142
143
144 --
145 -- Inner joins (equi-joins)
146 --
147
148 SELECT '' AS "xxx", *
149   FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.i);
150
151 SELECT '' AS "xxx", *
152   FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i = J2_TBL.k);
153
154
155 --
156 -- Non-equi-joins
157 --
158
159 SELECT '' AS "xxx", *
160   FROM J1_TBL JOIN J2_TBL ON (J1_TBL.i <= J2_TBL.k);
161
162
163 --
164 -- Outer joins
165 -- Note that OUTER is a noise word
166 --
167
168 SELECT '' AS "xxx", *
169   FROM J1_TBL LEFT OUTER JOIN J2_TBL USING (i);
170
171 SELECT '' AS "xxx", *
172   FROM J1_TBL LEFT JOIN J2_TBL USING (i);
173
174 SELECT '' AS "xxx", *
175   FROM J1_TBL RIGHT OUTER JOIN J2_TBL USING (i);
176
177 SELECT '' AS "xxx", *
178   FROM J1_TBL RIGHT JOIN J2_TBL USING (i);
179
180 SELECT '' AS "xxx", *
181   FROM J1_TBL FULL OUTER JOIN J2_TBL USING (i);
182
183 SELECT '' AS "xxx", *
184   FROM J1_TBL FULL JOIN J2_TBL USING (i);
185
186 SELECT '' AS "xxx", *
187   FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (k = 1);
188
189 SELECT '' AS "xxx", *
190   FROM J1_TBL LEFT JOIN J2_TBL USING (i) WHERE (i = 1);
191
192
193 --
194 -- More complicated constructs
195 --
196
197 -- UNION JOIN isn't implemented yet
198 SELECT '' AS "xxx", *
199   FROM J1_TBL UNION JOIN J2_TBL;
200
201 --
202 -- Multiway full join
203 --
204
205 CREATE TABLE t1 (name TEXT, n INTEGER);
206 CREATE TABLE t2 (name TEXT, n INTEGER);
207 CREATE TABLE t3 (name TEXT, n INTEGER);
208
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 );
216
217 SELECT * FROM t1 FULL JOIN t2 USING (name) FULL JOIN t3 USING (name);
218
219 --
220 -- Test interactions of join syntax and subqueries
221 --
222
223 -- Basic cases (we expect planner to pull up the subquery here)
224 SELECT * FROM
225 (SELECT * FROM t2) as s2
226 INNER JOIN
227 (SELECT * FROM t3) s3
228 USING (name);
229
230 SELECT * FROM
231 (SELECT * FROM t2) as s2
232 LEFT JOIN
233 (SELECT * FROM t3) s3
234 USING (name);
235
236 SELECT * FROM
237 (SELECT * FROM t2) as s2
238 FULL JOIN
239 (SELECT * FROM t3) s3
240 USING (name);
241
242 -- Cases with non-nullable expressions in subquery results;
243 -- make sure these go to null as expected
244 SELECT * FROM
245 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
246 NATURAL INNER JOIN
247 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
248
249 SELECT * FROM
250 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
251 NATURAL LEFT JOIN
252 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
253
254 SELECT * FROM
255 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
256 NATURAL FULL JOIN
257 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
258
259 SELECT * FROM
260 (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
261 NATURAL INNER JOIN
262 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
263 NATURAL INNER JOIN
264 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
265
266 SELECT * FROM
267 (SELECT name, n as s1_n, 1 as s1_1 FROM t1) as s1
268 NATURAL FULL JOIN
269 (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
270 NATURAL FULL JOIN
271 (SELECT name, n as s3_n, 3 as s3_2 FROM t3) s3;
272
273 SELECT * FROM
274 (SELECT name, n as s1_n FROM t1) as s1
275 NATURAL FULL JOIN
276   (SELECT * FROM
277     (SELECT name, n as s2_n FROM t2) as s2
278     NATURAL FULL JOIN
279     (SELECT name, n as s3_n FROM t3) as s3
280   ) ss2;
281
282 SELECT * FROM
283 (SELECT name, n as s1_n FROM t1) as s1
284 NATURAL FULL JOIN
285   (SELECT * FROM
286     (SELECT name, n as s2_n, 2 as s2_2 FROM t2) as s2
287     NATURAL FULL JOIN
288     (SELECT name, n as s3_n FROM t3) as s3
289   ) ss2;
290
291 --
292 -- Clean up
293 --
294
295 DROP TABLE t1;
296 DROP TABLE t2;
297 DROP TABLE t3;
298
299 DROP TABLE J1_TBL;
300 DROP TABLE J2_TBL;