]> granicus.if.org Git - postgresql/blob - src/test/regress/sql/join.sql
Clean up header for uniform appearance throughout tests.
[postgresql] / src / test / regress / sql / join.sql
1 --
2 -- JOIN
3 -- Test join clauses
4 --
5
6 CREATE TABLE JOIN1_TBL (
7   i integer,
8   j integer,
9   t text
10 );
11
12 CREATE TABLE JOIN2_TBL (
13   i integer,
14   k integer
15 );
16
17 CREATE TABLE JOIN3_TBL (
18   i integer,
19   j integer,
20   y integer
21 );
22
23 CREATE TABLE JOIN4_TBL (
24   k integer,
25   z integer
26 );
27
28 INSERT INTO JOIN1_TBL VALUES (1, 3, 'one');
29 INSERT INTO JOIN1_TBL VALUES (2, 2, 'two');
30 INSERT INTO JOIN1_TBL VALUES (3, 1, 'three');
31 INSERT INTO JOIN1_TBL VALUES (4, 0, 'four');
32
33 INSERT INTO JOIN2_TBL VALUES (1, -1);
34 INSERT INTO JOIN2_TBL VALUES (2, 2);
35 INSERT INTO JOIN2_TBL VALUES (3, -3);
36 INSERT INTO JOIN2_TBL VALUES (2, 4);
37
38
39 --
40 -- CROSS JOIN
41 -- Qualifications are not allowed on cross joins,
42 -- which degenerate into a standard unqualified inner join.
43 --
44
45 SELECT '' AS "xxx", *
46   FROM JOIN1_TBL CROSS JOIN JOIN2_TBL;
47
48 SELECT '' AS "xxx", i, k, t
49   FROM JOIN1_TBL CROSS JOIN JOIN2_TBL;
50
51 SELECT '' AS "xxx", ii, tt, kk
52   FROM JOIN1_TBL CROSS JOIN JOIN2_TBL AS JT (ii, jj, tt, ii2, kk);
53
54 SELECT '' AS "xxx", jt.ii, jt.jj, jt.kk
55   FROM JOIN1_TBL CROSS JOIN JOIN2_TBL AS JT (ii, jj, tt, ii2, kk);
56
57
58 --
59 --
60 -- Inner joins (equi-joins)
61 --
62 --
63
64 --
65 -- Inner joins (equi-joins) with USING clause
66 -- The USING syntax changes the shape of the resulting table
67 -- by including a column in the USING clause only once in the result.
68 --
69
70 -- Inner equi-join on all columns with the same name
71 SELECT '' AS "xxx", *
72   FROM JOIN1_TBL NATURAL JOIN JOIN2_TBL;
73
74 -- Inner equi-join on specified column
75 SELECT '' AS "xxx", *
76   FROM JOIN1_TBL INNER JOIN JOIN2_TBL USING (i);
77
78 -- Same as above, slightly different syntax
79 SELECT '' AS "xxx", *
80   FROM JOIN1_TBL JOIN JOIN2_TBL USING (i);
81
82
83 --
84 -- Inner joins (equi-joins)
85 --
86
87 SELECT '' AS "xxx", *
88   FROM JOIN1_TBL JOIN JOIN2_TBL ON (JOIN1_TBL.i = JOIN2_TBL.i);
89
90 SELECT '' AS "xxx", *
91   FROM JOIN1_TBL JOIN JOIN2_TBL ON (JOIN1_TBL.i = JOIN2_TBL.k);
92
93 SELECT '' AS "xxx", *
94   FROM JOIN1_TBL CROSS JOIN JOIN2_TBL;
95
96
97 --
98 -- Non-equi-joins
99 --
100
101 SELECT '' AS "xxx", *
102   FROM JOIN1_TBL JOIN JOIN2_TBL ON (JOIN1_TBL.i <= JOIN2_TBL.k);
103
104
105 --
106 -- Outer joins
107 --
108
109 SELECT '' AS "xxx", *
110   FROM JOIN1_TBL OUTER JOIN JOIN2_TBL USING (i);
111
112 SELECT '' AS "xxx", *
113   FROM JOIN1_TBL LEFT OUTER JOIN JOIN2_TBL USING (i);
114
115 SELECT '' AS "xxx", *
116   FROM JOIN1_TBL RIGHT OUTER JOIN JOIN2_TBL USING (i);
117
118 SELECT '' AS "xxx", *
119   FROM JOIN1_TBL FULL OUTER JOIN JOIN2_TBL USING (i);
120
121
122 --
123 -- More complicated constructs
124 --
125
126 --
127 -- Clean up
128 --
129
130 DROP TABLE JOIN1_TBL;
131 DROP TABLE JOIN2_TBL;
132