2 #pragma ident "%Z%%M% %I% %E% SMI"
6 # The author disclaims copyright to this source code. In place of
7 # a legal notice, here is a blessing:
9 # May you do good and not evil.
10 # May you find forgiveness for yourself and forgive others.
11 # May you share freely, never taking more than you give.
13 #***********************************************************************
14 # This file implements regression tests for SQLite library.
16 # This file implements tests for joins, including outer joins.
18 # $Id: join.test,v 1.11 2003/09/27 13:39:40 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
25 CREATE TABLE t1(a,b,c);
26 INSERT INTO t1 VALUES(1,2,3);
27 INSERT INTO t1 VALUES(2,3,4);
28 INSERT INTO t1 VALUES(3,4,5);
34 CREATE TABLE t2(b,c,d);
35 INSERT INTO t2 VALUES(1,2,3);
36 INSERT INTO t2 VALUES(2,3,4);
37 INSERT INTO t2 VALUES(3,4,5);
44 SELECT * FROM t1 NATURAL JOIN t2;
46 } {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
49 SELECT * FROM t2 NATURAL JOIN t1;
51 } {t2.b 2 t2.c 3 t2.d 4 t1.a 1 t2.b 3 t2.c 4 t2.d 5 t1.a 2}
54 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
56 } {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
59 SELECT * FROM t1 INNER JOIN t2 USING(b);
61 } {t1.a 1 t1.b 2 t1.c 3 t2.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.c 4 t2.d 5}
64 SELECT * FROM t1 INNER JOIN t2 USING(c);
66 } {t1.a 1 t1.b 2 t1.c 3 t2.b 2 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.b 3 t2.d 5}
69 SELECT * FROM t1 INNER JOIN t2 USING(c,b);
71 } {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t1.a 2 t1.b 3 t1.c 4 t2.d 5}
75 SELECT * FROM t1 NATURAL CROSS JOIN t2;
80 SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
85 SELECT * FROM t1 NATURAL INNER JOIN t2;
90 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
95 SELECT * FROM t1 natural inner join t2;
100 SELECT * FROM t1 NATURAL JOIN
101 (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
103 } {t1.a 1 t1.b 2 t1.c 3 t3.d 4 t3.e 5}
106 SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
109 } {tx.c 3 tx.d 4 tx.e 5 t1.a 1 t1.b 2}
113 CREATE TABLE t3(c,d,e);
114 INSERT INTO t3 VALUES(2,3,4);
115 INSERT INTO t3 VALUES(3,4,5);
116 INSERT INTO t3 VALUES(4,5,6);
119 } {2 3 4 3 4 5 4 5 6}
122 SELECT * FROM t1 natural join t2 natural join t3;
124 } {1 2 3 4 5 2 3 4 5 6}
127 SELECT * FROM t1 natural join t2 natural join t3;
129 } {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t3.e 5 t1.a 2 t1.b 3 t1.c 4 t2.d 5 t3.e 6}
132 CREATE TABLE t4(d,e,f);
133 INSERT INTO t4 VALUES(2,3,4);
134 INSERT INTO t4 VALUES(3,4,5);
135 INSERT INTO t4 VALUES(4,5,6);
138 } {2 3 4 3 4 5 4 5 6}
141 SELECT * FROM t1 natural join t2 natural join t4;
146 SELECT * FROM t1 natural join t2 natural join t4;
148 } {t1.a 1 t1.b 2 t1.c 3 t2.d 4 t4.e 5 t4.f 6}
151 SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
157 SELECT * FROM t1 NATURAL LEFT JOIN t2;
159 } {1 2 3 4 2 3 4 5 3 4 5 {}}
162 SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
164 } {1 2 3 {} 2 3 4 1 3 4 5 2}
167 SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
169 } {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
172 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
174 } {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
177 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
179 } {2 3 4 {} {} {} 3 4 5 1 2 3}
182 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
184 } {1 2 3 {} {} {} 2 3 4 {} {} {}}
188 SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
190 } {1 {a NATURAL join may not have an ON or USING clause}}
193 SELECT * FROM t1 NATURAL JOIN t2 USING(b);
195 } {1 {a NATURAL join may not have an ON or USING clause}}
198 SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
200 } {1 {cannot have both ON and USING clauses in the same join}}
203 SELECT * FROM t1 JOIN t2 USING(a);
205 } {1 {cannot join using column a - column not present in both tables}}
208 SELECT * FROM t1 USING(a);
210 } {0 {1 2 3 2 3 4 3 4 5}}
213 SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
215 } {1 {no such column: t3.a}}
218 SELECT * FROM t1 INNER OUTER JOIN t2;
220 } {1 {unknown or unsupported join type: INNER OUTER}}
223 SELECT * FROM t1 LEFT BOGUS JOIN t2;
225 } {1 {unknown or unsupported join type: LEFT BOGUS}}
230 CREATE TABLE t5(a INTEGER PRIMARY KEY);
231 CREATE TABLE t6(a INTEGER);
232 INSERT INTO t6 VALUES(NULL);
233 INSERT INTO t6 VALUES(NULL);
234 INSERT INTO t6 SELECT * FROM t6;
235 INSERT INTO t6 SELECT * FROM t6;
236 INSERT INTO t6 SELECT * FROM t6;
237 INSERT INTO t6 SELECT * FROM t6;
238 INSERT INTO t6 SELECT * FROM t6;
239 INSERT INTO t6 SELECT * FROM t6;
243 SELECT * FROM t6 NATURAL JOIN t5;
248 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
253 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
258 UPDATE t6 SET a='xyz';
259 SELECT * FROM t6 NATURAL JOIN t5;
264 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
269 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
275 SELECT * FROM t6 NATURAL JOIN t5;
280 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
285 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
292 create table centros (id integer primary key, centro);
293 INSERT INTO centros VALUES(1,'xxx');
294 create table usuarios (id integer primary key, nombre, apellidos,
296 INSERT INTO usuarios VALUES(1,'a','aa',1);
297 INSERT INTO usuarios VALUES(2,'b','bb',1);
298 INSERT INTO usuarios VALUES(3,'c','cc',NULL);
299 create index idcentro on usuarios (idcentro);
301 select usuarios.id, usuarios.nombre, centros.centro from
302 usuarios left outer join centros on usuarios.idcentro = centros.id;
304 } {1 a xxx 2 b xxx 3 c {}}
306 # A test for ticket #247.
310 CREATE TABLE t7 (x, y);
311 INSERT INTO t7 VALUES ("pa1", 1);
312 INSERT INTO t7 VALUES ("pa2", NULL);
313 INSERT INTO t7 VALUES ("pa3", NULL);
314 INSERT INTO t7 VALUES ("pa4", 2);
315 INSERT INTO t7 VALUES ("pa30", 131);
316 INSERT INTO t7 VALUES ("pa31", 130);
317 INSERT INTO t7 VALUES ("pa28", NULL);
319 CREATE TABLE t8 (a integer primary key, b);
320 INSERT INTO t8 VALUES (1, "pa1");
321 INSERT INTO t8 VALUES (2, "pa4");
322 INSERT INTO t8 VALUES (3, NULL);
323 INSERT INTO t8 VALUES (4, NULL);
324 INSERT INTO t8 VALUES (130, "pa31");
325 INSERT INTO t8 VALUES (131, "pa30");
327 SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
329 } {1 999 999 2 131 130 999}
331 # Make sure a left join where the right table is really a view that
332 # is itself a join works right. Ticket #306.
337 CREATE TABLE t9(a INTEGER PRIMARY KEY, b);
338 INSERT INTO t9 VALUES(1,11);
339 INSERT INTO t9 VALUES(2,22);
340 CREATE TABLE t10(x INTEGER PRIMARY KEY, y);
341 INSERT INTO t10 VALUES(1,2);
342 INSERT INTO t10 VALUES(3,3);
343 CREATE TABLE t11(p INTEGER PRIMARY KEY, q);
344 INSERT INTO t11 VALUES(2,111);
345 INSERT INTO t11 VALUES(3,333);
346 CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
348 SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
350 } {1 11 1 111 2 22 {} {}}
353 SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
356 } {1 11 1 111 2 22 {} {}}
359 SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
361 } {1 111 1 11 3 333 {} {}}
363 # Ticket #350 describes a scenario where LEFT OUTER JOIN does not
364 # function correctly if the right table in the join is really
367 # To test the problem, we generate the same LEFT OUTER JOIN in two
368 # separate selects but with on using a subquery and the other calling
369 # the table directly. Then connect the two SELECTs using an EXCEPT.
370 # Both queries should generate the same results so the answer should
376 CREATE TABLE t12(a,b);
377 INSERT INTO t12 VALUES(1,11);
378 INSERT INTO t12 VALUES(2,22);
379 CREATE TABLE t13(b,c);
380 INSERT INTO t13 VALUES(22,222);
382 SELECT * FROM t12 NATURAL LEFT JOIN t13
384 SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
389 CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
390 SELECT * FROM t12 NATURAL LEFT JOIN t13
392 SELECT * FROM t12 NATURAL LEFT JOIN v13;