First public contribution.
3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
11 # This file implements regression tests for SQLite library.
13 # This file implements tests for joins, including outer joins.
15 # $Id: join.test,v 1.25 2008/08/14 00:19:49 drh Exp $
17 set testdir [file dirname $argv0]
18 source $testdir/tester.tcl
22 CREATE TABLE t1(a,b,c);
23 INSERT INTO t1 VALUES(1,2,3);
24 INSERT INTO t1 VALUES(2,3,4);
25 INSERT INTO t1 VALUES(3,4,5);
31 CREATE TABLE t2(b,c,d);
32 INSERT INTO t2 VALUES(1,2,3);
33 INSERT INTO t2 VALUES(2,3,4);
34 INSERT INTO t2 VALUES(3,4,5);
41 SELECT * FROM t1 NATURAL JOIN t2;
43 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
46 SELECT * FROM t2 NATURAL JOIN t1;
48 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
51 SELECT * FROM t2 AS x NATURAL JOIN t1;
53 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
56 SELECT * FROM t2 NATURAL JOIN t1 AS y;
58 } {b 2 c 3 d 4 a 1 b 3 c 4 d 5 a 2}
61 SELECT b FROM t1 NATURAL JOIN t2;
66 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
68 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
71 SELECT * FROM t1 AS x INNER JOIN t2 USING(b,c);
73 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
76 SELECT * FROM t1 INNER JOIN t2 AS y USING(b,c);
78 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
81 SELECT * FROM t1 AS x INNER JOIN t2 AS y USING(b,c);
83 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
86 SELECT b FROM t1 JOIN t2 USING(b);
91 SELECT * FROM t1 INNER JOIN t2 USING(b);
93 } {a 1 b 2 c 3 c 3 d 4 a 2 b 3 c 4 c 4 d 5}
96 SELECT * FROM t1 INNER JOIN t2 USING(c);
98 } {a 1 b 2 c 3 b 2 d 4 a 2 b 3 c 4 b 3 d 5}
101 SELECT * FROM t1 INNER JOIN t2 USING(c,b);
103 } {a 1 b 2 c 3 d 4 a 2 b 3 c 4 d 5}
107 SELECT * FROM t1 NATURAL CROSS JOIN t2;
112 SELECT * FROM t1 CROSS JOIN t2 USING(b,c);
117 SELECT * FROM t1 NATURAL INNER JOIN t2;
122 SELECT * FROM t1 INNER JOIN t2 USING(b,c);
127 SELECT * FROM t1 natural inner join t2;
134 SELECT * FROM t1 NATURAL JOIN
135 (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as t3
137 } {a 1 b 2 c 3 d 4 e 5}
140 SELECT * FROM (SELECT b as 'c', c as 'd', d as 'e' FROM t2) as 'tx'
143 } {c 3 d 4 e 5 a 1 b 2}
148 CREATE TABLE t3(c,d,e);
149 INSERT INTO t3 VALUES(2,3,4);
150 INSERT INTO t3 VALUES(3,4,5);
151 INSERT INTO t3 VALUES(4,5,6);
154 } {2 3 4 3 4 5 4 5 6}
157 SELECT * FROM t1 natural join t2 natural join t3;
159 } {1 2 3 4 5 2 3 4 5 6}
162 SELECT * FROM t1 natural join t2 natural join t3;
164 } {a 1 b 2 c 3 d 4 e 5 a 2 b 3 c 4 d 5 e 6}
167 CREATE TABLE t4(d,e,f);
168 INSERT INTO t4 VALUES(2,3,4);
169 INSERT INTO t4 VALUES(3,4,5);
170 INSERT INTO t4 VALUES(4,5,6);
173 } {2 3 4 3 4 5 4 5 6}
174 do_test join-1.19.1 {
176 SELECT * FROM t1 natural join t2 natural join t4;
179 do_test join-1.19.2 {
181 SELECT * FROM t1 natural join t2 natural join t4;
183 } {a 1 b 2 c 3 d 4 e 5 f 6}
186 SELECT * FROM t1 natural join t2 natural join t3 WHERE t1.a=1
192 SELECT * FROM t1 NATURAL LEFT JOIN t2;
194 } {1 2 3 4 2 3 4 5 3 4 5 {}}
197 SELECT * FROM t2 NATURAL LEFT OUTER JOIN t1;
199 } {1 2 3 {} 2 3 4 1 3 4 5 2}
202 SELECT * FROM t1 NATURAL RIGHT OUTER JOIN t2;
204 } {1 {RIGHT and FULL OUTER JOINs are not currently supported}}
207 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d
209 } {1 2 3 {} {} {} 2 3 4 {} {} {} 3 4 5 1 2 3}
212 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t1.a>1
214 } {2 3 4 {} {} {} 3 4 5 1 2 3}
217 SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.d WHERE t2.b IS NULL OR t2.b>1
219 } {1 2 3 {} {} {} 2 3 4 {} {} {}}
223 SELECT * FROM t1 NATURAL JOIN t2 ON t1.a=t2.b;
225 } {1 {a NATURAL join may not have an ON or USING clause}}
228 SELECT * FROM t1 NATURAL JOIN t2 USING(b);
230 } {1 {a NATURAL join may not have an ON or USING clause}}
233 SELECT * FROM t1 JOIN t2 ON t1.a=t2.b USING(b);
235 } {1 {cannot have both ON and USING clauses in the same join}}
238 SELECT * FROM t1 JOIN t2 USING(a);
240 } {1 {cannot join using column a - column not present in both tables}}
243 SELECT * FROM t1 JOIN t2 USING(d);
245 } {1 {cannot join using column d - column not present in both tables}}
248 SELECT * FROM t1 USING(a);
250 } {0 {1 2 3 2 3 4 3 4 5}}
253 SELECT * FROM t1 JOIN t2 ON t3.a=t2.b;
255 } {1 {no such column: t3.a}}
258 SELECT * FROM t1 INNER OUTER JOIN t2;
260 } {1 {unknown or unsupported join type: INNER OUTER}}
263 SELECT * FROM t1 INNER OUTER CROSS JOIN t2;
265 } {1 {unknown or unsupported join type: INNER OUTER CROSS}}
268 SELECT * FROM t1 OUTER NATURAL INNER JOIN t2;
270 } {1 {unknown or unsupported join type: OUTER NATURAL INNER}}
273 SELECT * FROM t1 LEFT BOGUS JOIN t2;
275 } {1 {unknown or unsupported join type: LEFT BOGUS}}
278 SELECT * FROM t1 INNER BOGUS CROSS JOIN t2;
280 } {1 {unknown or unsupported join type: INNER BOGUS CROSS}}
283 SELECT * FROM t1 NATURAL AWK SED JOIN t2;
285 } {1 {unknown or unsupported join type: NATURAL AWK SED}}
290 CREATE TABLE t5(a INTEGER PRIMARY KEY);
291 CREATE TABLE t6(a INTEGER);
292 INSERT INTO t6 VALUES(NULL);
293 INSERT INTO t6 VALUES(NULL);
294 INSERT INTO t6 SELECT * FROM t6;
295 INSERT INTO t6 SELECT * FROM t6;
296 INSERT INTO t6 SELECT * FROM t6;
297 INSERT INTO t6 SELECT * FROM t6;
298 INSERT INTO t6 SELECT * FROM t6;
299 INSERT INTO t6 SELECT * FROM t6;
303 SELECT * FROM t6 NATURAL JOIN t5;
308 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
313 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
318 UPDATE t6 SET a='xyz';
319 SELECT * FROM t6 NATURAL JOIN t5;
324 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
329 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
335 SELECT * FROM t6 NATURAL JOIN t5;
340 SELECT * FROM t6, t5 WHERE t6.a<t5.a;
345 SELECT * FROM t6, t5 WHERE t6.a>t5.a;
352 create table centros (id integer primary key, centro);
353 INSERT INTO centros VALUES(1,'xxx');
354 create table usuarios (id integer primary key, nombre, apellidos,
356 INSERT INTO usuarios VALUES(1,'a','aa',1);
357 INSERT INTO usuarios VALUES(2,'b','bb',1);
358 INSERT INTO usuarios VALUES(3,'c','cc',NULL);
359 create index idcentro on usuarios (idcentro);
361 select usuarios.id, usuarios.nombre, centros.centro from
362 usuarios left outer join centros on usuarios.idcentro = centros.id;
364 } {1 a xxx 2 b xxx 3 c {}}
366 # A test for ticket #247.
370 CREATE TABLE t7 (x, y);
371 INSERT INTO t7 VALUES ("pa1", 1);
372 INSERT INTO t7 VALUES ("pa2", NULL);
373 INSERT INTO t7 VALUES ("pa3", NULL);
374 INSERT INTO t7 VALUES ("pa4", 2);
375 INSERT INTO t7 VALUES ("pa30", 131);
376 INSERT INTO t7 VALUES ("pa31", 130);
377 INSERT INTO t7 VALUES ("pa28", NULL);
379 CREATE TABLE t8 (a integer primary key, b);
380 INSERT INTO t8 VALUES (1, "pa1");
381 INSERT INTO t8 VALUES (2, "pa4");
382 INSERT INTO t8 VALUES (3, NULL);
383 INSERT INTO t8 VALUES (4, NULL);
384 INSERT INTO t8 VALUES (130, "pa31");
385 INSERT INTO t8 VALUES (131, "pa30");
387 SELECT coalesce(t8.a,999) from t7 LEFT JOIN t8 on y=a;
389 } {1 999 999 2 131 130 999}
391 # Make sure a left join where the right table is really a view that
392 # is itself a join works right. Ticket #306.
398 CREATE TABLE t9(a INTEGER PRIMARY KEY, b);
399 INSERT INTO t9 VALUES(1,11);
400 INSERT INTO t9 VALUES(2,22);
401 CREATE TABLE t10(x INTEGER PRIMARY KEY, y);
402 INSERT INTO t10 VALUES(1,2);
403 INSERT INTO t10 VALUES(3,3);
404 CREATE TABLE t11(p INTEGER PRIMARY KEY, q);
405 INSERT INTO t11 VALUES(2,111);
406 INSERT INTO t11 VALUES(3,333);
407 CREATE VIEW v10_11 AS SELECT x, q FROM t10, t11 WHERE t10.y=t11.p;
409 SELECT * FROM t9 LEFT JOIN v10_11 ON( a=x );
411 } {1 11 1 111 2 22 {} {}}
415 SELECT * FROM t9 LEFT JOIN (SELECT x, q FROM t10, t11 WHERE t10.y=t11.p)
418 } {1 11 1 111 2 22 {} {}}
422 SELECT * FROM v10_11 LEFT JOIN t9 ON( a=x );
424 } {1 111 1 11 3 333 {} {}}
426 # Constant expressions in a subquery that is the right element of a
427 # LEFT JOIN evaluate to NULL for rows where the LEFT JOIN does not
428 # match. Ticket #3300
431 SELECT * FROM t9 LEFT JOIN (SELECT 44, p, q FROM t11) AS sub1 ON p=a
433 } {1 11 {} {} {} 2 22 44 2 111}
437 # Ticket #350 describes a scenario where LEFT OUTER JOIN does not
438 # function correctly if the right table in the join is really
441 # To test the problem, we generate the same LEFT OUTER JOIN in two
442 # separate selects but with on using a subquery and the other calling
443 # the table directly. Then connect the two SELECTs using an EXCEPT.
444 # Both queries should generate the same results so the answer should
451 CREATE TABLE t12(a,b);
452 INSERT INTO t12 VALUES(1,11);
453 INSERT INTO t12 VALUES(2,22);
454 CREATE TABLE t13(b,c);
455 INSERT INTO t13 VALUES(22,222);
463 SELECT * FROM t12 NATURAL LEFT JOIN t13
465 SELECT * FROM t12 NATURAL LEFT JOIN (SELECT * FROM t13 WHERE b>0);
472 CREATE VIEW v13 AS SELECT * FROM t13 WHERE b>0;
473 SELECT * FROM t12 NATURAL LEFT JOIN t13
475 SELECT * FROM t12 NATURAL LEFT JOIN v13;
479 } ;# ifcapable compound
482 # Ticket #1697: Left Join WHERE clause terms that contain an
483 # aggregate subquery.
487 CREATE TABLE t21(a,b,c);
488 CREATE TABLE t22(p,q);
489 CREATE INDEX i22 ON t22(q);
490 SELECT a FROM t21 LEFT JOIN t22 ON b=p WHERE q=
491 (SELECT max(m.q) FROM t22 m JOIN t21 n ON n.b=m.p WHERE n.c=1);
495 # Test a LEFT JOIN when the right-hand side of hte join is an empty
496 # sub-query. Seems fine.
500 CREATE TABLE t23(a, b, c);
501 CREATE TABLE t24(a, b, c);
502 INSERT INTO t23 VALUES(1, 2, 3);
505 SELECT * FROM t23 LEFT JOIN t24;
510 SELECT * FROM t23 LEFT JOIN (SELECT * FROM t24);
514 } ;# ifcapable subquery