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. The
15 # focus of this file is testing SELECT statements that are part of
18 # $Id: subselect.test,v 1.7 2002/07/15 18:55:26 drh Exp $
20 set testdir [file dirname $argv0]
21 source $testdir/tester.tcl
23 # Basic sanity checking. Try a simple subselect.
25 do_test subselect-1.1 {
27 CREATE TABLE t1(a int, b int);
28 INSERT INTO t1 VALUES(1,2);
29 INSERT INTO t1 VALUES(3,4);
30 INSERT INTO t1 VALUES(5,6);
32 execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
35 # Try a select with more than one result column.
37 do_test subselect-1.2 {
38 set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
40 } {1 {only a single result allowed for a SELECT that is part of an expression}}
42 # A subselect without an aggregate.
44 do_test subselect-1.3a {
45 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
47 do_test subselect-1.3b {
48 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
50 do_test subselect-1.3c {
51 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
53 do_test subselect-1.3c {
54 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
57 # What if the subselect doesn't return any value. We should get
58 # NULL as the result. Check it out.
60 do_test subselect-1.4 {
61 execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
64 # Try multiple subselects within a single expression.
66 do_test subselect-1.5 {
68 CREATE TABLE t2(x int, y int);
69 INSERT INTO t2 VALUES(1,2);
70 INSERT INTO t2 VALUES(2,4);
71 INSERT INTO t2 VALUES(3,8);
72 INSERT INTO t2 VALUES(4,16);
76 WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
80 # Try something useful. Delete every entry from t2 where the
81 # x value is less than half of the maximum.
83 do_test subselect-1.6 {
84 execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
85 execsql {SELECT x FROM t2 ORDER BY x}
88 # Make sure sorting works for SELECTs there used as a scalar expression.
90 do_test subselect-2.1 {
92 SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC)
95 do_test subselect-2.2 {
97 SELECT 1 IN (SELECT a FROM t1 ORDER BY a);
100 do_test subselect-2.3 {
102 SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC);
106 # Verify that the ORDER BY clause is honored in a subquery.
108 do_test subselect-3.1 {
110 CREATE TABLE t3(x int);
111 INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1;
112 SELECT * FROM t3 ORDER BY x;
115 do_test subselect-3.2 {
117 SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2);
120 do_test subselect-3.3 {
122 SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2);
125 do_test subselect-3.4 {
127 SELECT (SELECT x FROM t3 ORDER BY x);
130 do_test subselect-3.5 {
132 SELECT (SELECT x FROM t3 ORDER BY x DESC);
135 do_test subselect-3.6 {
137 SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1);
140 do_test subselect-3.7 {
142 SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1);
145 do_test subselect-3.8 {
147 SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2);
150 do_test subselect-3.9 {
152 SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2);