subselect.test 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # 2001 September 15
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  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.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The
  12. # focus of this file is testing SELECT statements that are part of
  13. # expressions.
  14. #
  15. # $Id: subselect.test,v 1.5 2002/05/26 20:54:35 drh Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. # Basic sanity checking. Try a simple subselect.
  19. #
  20. do_test subselect-1.1 {
  21. execsql {
  22. CREATE TABLE t1(a int, b int);
  23. INSERT INTO t1 VALUES(1,2);
  24. INSERT INTO t1 VALUES(3,4);
  25. INSERT INTO t1 VALUES(5,6);
  26. }
  27. execsql {SELECT * FROM t1 WHERE a = (SELECT count(*) FROM t1)}
  28. } {3 4}
  29. # Try a select with more than one result column.
  30. #
  31. do_test subselect-1.2 {
  32. set v [catch {execsql {SELECT * FROM t1 WHERE a = (SELECT * FROM t1)}} msg]
  33. lappend v $msg
  34. } {1 {only a single result allowed for a SELECT that is part of an expression}}
  35. # A subselect without an aggregate.
  36. #
  37. do_test subselect-1.3a {
  38. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=2)}
  39. } {2}
  40. do_test subselect-1.3b {
  41. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=4)}
  42. } {4}
  43. do_test subselect-1.3c {
  44. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=6)}
  45. } {6}
  46. do_test subselect-1.3c {
  47. execsql {SELECT b from t1 where a = (SELECT a FROM t1 WHERE b=8)}
  48. } {}
  49. # What if the subselect doesn't return any value. We should get
  50. # NULL as the result. Check it out.
  51. #
  52. do_test subselect-1.4 {
  53. execsql {SELECT b from t1 where a = coalesce((SELECT a FROM t1 WHERE b=5),1)}
  54. } {2}
  55. # Try multiple subselects within a single expression.
  56. #
  57. do_test subselect-1.5 {
  58. execsql {
  59. CREATE TABLE t2(x int, y int);
  60. INSERT INTO t2 VALUES(1,2);
  61. INSERT INTO t2 VALUES(2,4);
  62. INSERT INTO t2 VALUES(3,8);
  63. INSERT INTO t2 VALUES(4,16);
  64. }
  65. execsql {
  66. SELECT y from t2
  67. WHERE x = (SELECT sum(b) FROM t1 where a notnull) - (SELECT sum(a) FROM t1)
  68. }
  69. } {8}
  70. # Try something useful. Delete every entry from t2 where the
  71. # x value is less than half of the maximum.
  72. #
  73. do_test subselect-1.6 {
  74. execsql {DELETE FROM t2 WHERE x < 0.5*(SELECT max(x) FROM t2)}
  75. execsql {SELECT x FROM t2 ORDER BY x}
  76. } {2 3 4}
  77. finish_test