union.sql 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. --
  2. -- UNION (also INTERSECT, EXCEPT)
  3. --
  4. -- Simple UNION constructs
  5. SELECT 1 AS two UNION SELECT 2 ORDER BY 1;
  6. SELECT 1 AS one UNION SELECT 1 ORDER BY 1;
  7. SELECT 1 AS two UNION ALL SELECT 2;
  8. SELECT 1 AS two UNION ALL SELECT 1;
  9. SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
  10. SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1;
  11. SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
  12. SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1;
  13. -- Mixed types
  14. SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1;
  15. SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1;
  16. SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1;
  17. SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1;
  18. SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1;
  19. SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
  20. SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1;
  21. SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
  22. SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1;
  23. SELECT f1 AS three FROM VARCHAR_TBL
  24. UNION
  25. SELECT CAST(f1 AS varchar) FROM CHAR_TBL
  26. ORDER BY 1;
  27. SELECT f1 AS eight FROM VARCHAR_TBL
  28. UNION ALL
  29. SELECT f1 FROM CHAR_TBL;
  30. SELECT f1 AS five FROM TEXT_TBL
  31. UNION
  32. SELECT f1 FROM VARCHAR_TBL
  33. UNION
  34. SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
  35. ORDER BY 1;
  36. --
  37. -- INTERSECT and EXCEPT
  38. --
  39. SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1;
  40. SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
  41. SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
  42. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
  43. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1;
  44. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1;
  45. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1;
  46. -- nested cases
  47. (SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6;
  48. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6;
  49. (SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6;
  50. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6;
  51. select count(*) from
  52. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  53. select count(*) from
  54. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  55. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  56. select count(*) from
  57. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  58. select count(*) from
  59. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  60. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  61. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  62. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  63. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  64. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  65. --
  66. -- Operator precedence and (((((extra))))) parentheses
  67. --
  68. SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl ORDER BY 1;
  69. SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1;
  70. (((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl;
  71. SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)));
  72. --
  73. -- Subqueries with ORDER BY & LIMIT clauses
  74. --
  75. -- In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
  76. SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
  77. ORDER BY q2,q1;
  78. --
  79. -- New syntaxes (7.1) permit new tests
  80. --
  81. (((((select * from int8_tbl)))));
  82. --
  83. -- Check handling of a case with unknown constants. We don't guarantee
  84. -- an undecorated constant will work in all cases, but historically this
  85. -- usage has worked, so test we don't break it.
  86. --
  87. SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
  88. UNION
  89. SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
  90. ORDER BY 1;
  91. -- This should fail, but it should produce an error cursor
  92. SELECT '3.4'::numeric UNION SELECT 'foo';
  93. --
  94. -- Test that expression-index constraints can be pushed down through
  95. -- UNION or UNION ALL
  96. --
  97. CREATE TEMP TABLE t1 (a text, b text);
  98. CREATE TEMP TABLE t2 (ab text primary key);
  99. INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
  100. INSERT INTO t2 VALUES ('ab'), ('xy');
  101. --
  102. -- Test that ORDER BY for UNION ALL can be pushed down to inheritance
  103. -- children.
  104. --
  105. CREATE TEMP TABLE t1c (b text, a text);
  106. INSERT INTO t1c VALUES ('v', 'w'), ('c', 'd'), ('m', 'n'), ('e', 'f');
  107. -- This simpler variant of the above test has been observed to fail differently
  108. create table events (event_id int primary key);
  109. create table other_events (event_id int primary key);
  110. drop table events_child, events, other_events;
  111. SELECT * FROM
  112. (SELECT 1 AS t, 2 AS x
  113. UNION
  114. SELECT 2 AS t, 4 AS x) ss
  115. WHERE x < 4
  116. ORDER BY x;
  117. SELECT * FROM
  118. (SELECT 1 AS t, (random()*3)::int AS x
  119. UNION
  120. SELECT 2 AS t, 4 AS x) ss
  121. WHERE x > 3
  122. ORDER BY x;
  123. select distinct q1 from
  124. (select distinct * from int8_tbl i81
  125. union all
  126. select distinct * from int8_tbl i82) ss
  127. where q2 = q2;
  128. select distinct q1 from
  129. (select distinct * from int8_tbl i81
  130. union all
  131. select distinct * from int8_tbl i82) ss
  132. where -q1 = q2;