1# 2# 2001 September 15 3# 4# The author disclaims copyright to this source code. In place of 5# a legal notice, here is a blessing: 6# 7# May you do good and not evil. 8# May you find forgiveness for yourself and forgive others. 9# May you share freely, never taking more than you give. 10# 11#*********************************************************************** 12# This file implements regression tests for SQLite library. The 13# focus of this file is testing SELECT statements that are part of 14# expressions. 15# 16# $Id: subselect.test,v 1.7 2002/07/15 18:55:26 drh Exp $ 17 18set testdir [file dirname $argv0] 19source $testdir/tester.tcl 20 21# Basic sanity checking. Try a simple subselect. 22# 23do_test subselect-1.1 { 24 execsql { 25 CREATE TABLE t1(a int, b int); 26 INSERT INTO t1 VALUES(1,2); 27 INSERT INTO t1 VALUES(3,4); 28 INSERT INTO t1 VALUES(5,6); 29 } 30 execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)} 31} {3 4} 32 33# Try a select with more than one result column. 34# 35do_test subselect-1.2 { 36 set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg] 37 lappend v $msg 38} {1 {only a single result allowed for a SELECT that is part of an expression}} 39 40# A subselect without an aggregate. 41# 42do_test subselect-1.3a { 43 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)} 44} {2} 45do_test subselect-1.3b { 46 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)} 47} {4} 48do_test subselect-1.3c { 49 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)} 50} {6} 51do_test subselect-1.3c { 52 execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)} 53} {} 54 55# What if the subselect doesn't return any value. We should get 56# NULL as the result. Check it out. 57# 58do_test subselect-1.4 { 59 execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)} 60} {2} 61 62# Try multiple subselects within a single expression. 63# 64do_test subselect-1.5 { 65 execsql { 66 CREATE TABLE t2(x int, y int); 67 INSERT INTO t2 VALUES(1,2); 68 INSERT INTO t2 VALUES(2,4); 69 INSERT INTO t2 VALUES(3,8); 70 INSERT INTO t2 VALUES(4,16); 71 } 72 execsql { 73 SELECT y from t2 74 WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1) 75 } 76} {8} 77 78# Try something useful. Delete every entry from t2 where the 79# x value is less than half of the maximum. 80# 81do_test subselect-1.6 { 82 execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)} 83 execsql {SELECT x FROM t2 ORDER BY x} 84} {2 3 4} 85 86# Make sure sorting works for SELECTs there used as a scalar expression. 87# 88do_test subselect-2.1 { 89 execsql { 90 SELECT (SELECT a FROM t1 ORDER BY a), (SELECT a FROM t1 ORDER BY a DESC) 91 } 92} {1 5} 93do_test subselect-2.2 { 94 execsql { 95 SELECT 1 IN (SELECT a FROM t1 ORDER BY a); 96 } 97} {1} 98do_test subselect-2.3 { 99 execsql { 100 SELECT 2 IN (SELECT a FROM t1 ORDER BY a DESC); 101 } 102} {0} 103 104# Verify that the ORDER BY clause is honored in a subquery. 105# 106do_test subselect-3.1 { 107 execsql { 108 CREATE TABLE t3(x int); 109 INSERT INTO t3 SELECT a FROM t1 UNION ALL SELECT b FROM t1; 110 SELECT * FROM t3 ORDER BY x; 111 } 112} {1 2 3 4 5 6} 113do_test subselect-3.2 { 114 execsql { 115 SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x LIMIT 2); 116 } 117} {3} 118do_test subselect-3.3 { 119 execsql { 120 SELECT sum(x) FROM (SELECT x FROM t3 ORDER BY x DESC LIMIT 2); 121 } 122} {11} 123do_test subselect-3.4 { 124 execsql { 125 SELECT (SELECT x FROM t3 ORDER BY x); 126 } 127} {1} 128do_test subselect-3.5 { 129 execsql { 130 SELECT (SELECT x FROM t3 ORDER BY x DESC); 131 } 132} {6} 133do_test subselect-3.6 { 134 execsql { 135 SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1); 136 } 137} {1} 138do_test subselect-3.7 { 139 execsql { 140 SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1); 141 } 142} {6} 143do_test subselect-3.8 { 144 execsql { 145 SELECT (SELECT x FROM t3 ORDER BY x LIMIT 1 OFFSET 2); 146 } 147} {3} 148do_test subselect-3.9 { 149 execsql { 150 SELECT (SELECT x FROM t3 ORDER BY x DESC LIMIT 1 OFFSET 2); 151 } 152} {4} 153 154 155 156finish_test 157