union.out 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. --
  2. -- UNION (also INTERSECT, EXCEPT)
  3. --
  4. -- Simple UNION constructs
  5. SELECT 1 AS two UNION SELECT 2 ORDER BY 1;
  6. two
  7. -----
  8. 1
  9. 2
  10. (2 rows)
  11. SELECT 1 AS one UNION SELECT 1 ORDER BY 1;
  12. one
  13. -----
  14. 1
  15. (1 row)
  16. SELECT 1 AS two UNION ALL SELECT 2;
  17. two
  18. -----
  19. 1
  20. 2
  21. (2 rows)
  22. SELECT 1 AS two UNION ALL SELECT 1;
  23. two
  24. -----
  25. 1
  26. 1
  27. (2 rows)
  28. SELECT 1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
  29. three
  30. -------
  31. 1
  32. 2
  33. 3
  34. (3 rows)
  35. SELECT 1 AS two UNION SELECT 2 UNION SELECT 2 ORDER BY 1;
  36. two
  37. -----
  38. 1
  39. 2
  40. (2 rows)
  41. SELECT 1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
  42. three
  43. -------
  44. 1
  45. 2
  46. 2
  47. (3 rows)
  48. SELECT 1.1 AS two UNION SELECT 2.2 ORDER BY 1;
  49. two
  50. -----
  51. 1.1
  52. 2.2
  53. (2 rows)
  54. -- Mixed types
  55. SELECT 1.1 AS two UNION SELECT 2 ORDER BY 1;
  56. two
  57. -----
  58. 1.1
  59. 2
  60. (2 rows)
  61. SELECT 1 AS two UNION SELECT 2.2 ORDER BY 1;
  62. two
  63. -----
  64. 1
  65. 2.2
  66. (2 rows)
  67. SELECT 1 AS one UNION SELECT 1.0::float8 ORDER BY 1;
  68. one
  69. -----
  70. 1
  71. (1 row)
  72. SELECT 1.1 AS two UNION ALL SELECT 2 ORDER BY 1;
  73. two
  74. -----
  75. 1.1
  76. 2
  77. (2 rows)
  78. SELECT 1.0::float8 AS two UNION ALL SELECT 1 ORDER BY 1;
  79. two
  80. -----
  81. 1
  82. 1
  83. (2 rows)
  84. SELECT 1.1 AS three UNION SELECT 2 UNION SELECT 3 ORDER BY 1;
  85. three
  86. -------
  87. 1.1
  88. 2
  89. 3
  90. (3 rows)
  91. SELECT 1.1::float8 AS two UNION SELECT 2 UNION SELECT 2.0::float8 ORDER BY 1;
  92. two
  93. -----
  94. 1.1
  95. 2
  96. (2 rows)
  97. SELECT 1.1 AS three UNION SELECT 2 UNION ALL SELECT 2 ORDER BY 1;
  98. three
  99. -------
  100. 1.1
  101. 2
  102. 2
  103. (3 rows)
  104. SELECT 1.1 AS two UNION (SELECT 2 UNION ALL SELECT 2) ORDER BY 1;
  105. two
  106. -----
  107. 1.1
  108. 2
  109. (2 rows)
  110. SELECT f1 AS three FROM VARCHAR_TBL
  111. UNION
  112. SELECT CAST(f1 AS varchar) FROM CHAR_TBL
  113. ORDER BY 1;
  114. three
  115. -------
  116. a
  117. ab
  118. abcd
  119. (3 rows)
  120. SELECT f1 AS eight FROM VARCHAR_TBL
  121. UNION ALL
  122. SELECT f1 FROM CHAR_TBL;
  123. eight
  124. -------
  125. a
  126. ab
  127. abcd
  128. abcd
  129. a
  130. ab
  131. abcd
  132. abcd
  133. (8 rows)
  134. SELECT f1 AS five FROM TEXT_TBL
  135. UNION
  136. SELECT f1 FROM VARCHAR_TBL
  137. UNION
  138. SELECT TRIM(TRAILING FROM f1) FROM CHAR_TBL
  139. ORDER BY 1;
  140. five
  141. -------------------
  142. a
  143. ab
  144. abcd
  145. doh!
  146. hi de ho neighbor
  147. (5 rows)
  148. --
  149. -- INTERSECT and EXCEPT
  150. --
  151. SELECT q2 FROM int8_tbl INTERSECT SELECT q1 FROM int8_tbl ORDER BY 1;
  152. q2
  153. ------------------
  154. 123
  155. 4567890123456789
  156. (2 rows)
  157. SELECT q2 FROM int8_tbl INTERSECT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
  158. q2
  159. ------------------
  160. 123
  161. 4567890123456789
  162. 4567890123456789
  163. (3 rows)
  164. SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1;
  165. q2
  166. -------------------
  167. -4567890123456789
  168. 456
  169. (2 rows)
  170. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT q1 FROM int8_tbl ORDER BY 1;
  171. q2
  172. -------------------
  173. -4567890123456789
  174. 456
  175. (2 rows)
  176. SELECT q2 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q1 FROM int8_tbl ORDER BY 1;
  177. q2
  178. -------------------
  179. -4567890123456789
  180. 456
  181. 4567890123456789
  182. (3 rows)
  183. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT q2 FROM int8_tbl ORDER BY 1;
  184. q1
  185. ------------------
  186. 123
  187. 4567890123456789
  188. (2 rows)
  189. SELECT q1 FROM int8_tbl EXCEPT ALL SELECT DISTINCT q2 FROM int8_tbl ORDER BY 1;
  190. q1
  191. ------------------
  192. 123
  193. 4567890123456789
  194. 4567890123456789
  195. (3 rows)
  196. -- nested cases
  197. (SELECT 1,2,3 UNION SELECT 4,5,6) INTERSECT SELECT 4,5,6;
  198. ?column? | ?column? | ?column?
  199. ----------+----------+----------
  200. 4 | 5 | 6
  201. (1 row)
  202. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) INTERSECT SELECT 4,5,6;
  203. ?column? | ?column? | ?column?
  204. ----------+----------+----------
  205. 4 | 5 | 6
  206. (1 row)
  207. (SELECT 1,2,3 UNION SELECT 4,5,6) EXCEPT SELECT 4,5,6;
  208. ?column? | ?column? | ?column?
  209. ----------+----------+----------
  210. 1 | 2 | 3
  211. (1 row)
  212. (SELECT 1,2,3 UNION SELECT 4,5,6 ORDER BY 1,2) EXCEPT SELECT 4,5,6;
  213. ?column? | ?column? | ?column?
  214. ----------+----------+----------
  215. 1 | 2 | 3
  216. (1 row)
  217. select count(*) from
  218. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  219. count
  220. -------
  221. 10000
  222. (1 row)
  223. select count(*) from
  224. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  225. count
  226. -------
  227. 5000
  228. (1 row)
  229. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  230. unique1
  231. ---------
  232. 10
  233. (1 row)
  234. select count(*) from
  235. ( select unique1 from tenk1 union select fivethous from tenk1 ) ss;
  236. count
  237. -------
  238. 10000
  239. (1 row)
  240. select count(*) from
  241. ( select unique1 from tenk1 intersect select fivethous from tenk1 ) ss;
  242. count
  243. -------
  244. 5000
  245. (1 row)
  246. select unique1 from tenk1 except select unique2 from tenk1 where unique2 != 10;
  247. unique1
  248. ---------
  249. 10
  250. (1 row)
  251. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  252. x
  253. -------
  254. {1,2}
  255. (1 row)
  256. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  257. x
  258. -------
  259. {1,3}
  260. (1 row)
  261. select x from (values (array[1, 2]), (array[1, 3])) _(x) intersect select x from (values (array[1, 2]), (array[1, 4])) _(x);
  262. x
  263. -------
  264. {1,2}
  265. (1 row)
  266. select x from (values (array[1, 2]), (array[1, 3])) _(x) except select x from (values (array[1, 2]), (array[1, 4])) _(x);
  267. x
  268. -------
  269. {1,3}
  270. (1 row)
  271. --
  272. -- Operator precedence and (((((extra))))) parentheses
  273. --
  274. SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl ORDER BY 1;
  275. q1
  276. -------------------
  277. -4567890123456789
  278. 123
  279. 123
  280. 456
  281. 4567890123456789
  282. 4567890123456789
  283. 4567890123456789
  284. (7 rows)
  285. SELECT q1 FROM int8_tbl INTERSECT (((SELECT q2 FROM int8_tbl UNION ALL SELECT q2 FROM int8_tbl))) ORDER BY 1;
  286. q1
  287. ------------------
  288. 123
  289. 4567890123456789
  290. (2 rows)
  291. (((SELECT q1 FROM int8_tbl INTERSECT SELECT q2 FROM int8_tbl ORDER BY 1))) UNION ALL SELECT q2 FROM int8_tbl;
  292. q1
  293. -------------------
  294. 123
  295. 4567890123456789
  296. 456
  297. 4567890123456789
  298. 123
  299. 4567890123456789
  300. -4567890123456789
  301. (7 rows)
  302. SELECT q1 FROM int8_tbl UNION ALL (((SELECT q2 FROM int8_tbl EXCEPT SELECT q1 FROM int8_tbl ORDER BY 1)));
  303. q1
  304. -------------------
  305. 123
  306. 123
  307. 4567890123456789
  308. 4567890123456789
  309. 4567890123456789
  310. -4567890123456789
  311. 456
  312. (7 rows)
  313. --
  314. -- Subqueries with ORDER BY & LIMIT clauses
  315. --
  316. -- In this syntax, ORDER BY/LIMIT apply to the result of the EXCEPT
  317. SELECT q1,q2 FROM int8_tbl EXCEPT SELECT q2,q1 FROM int8_tbl
  318. ORDER BY q2,q1;
  319. q1 | q2
  320. ------------------+-------------------
  321. 4567890123456789 | -4567890123456789
  322. 123 | 456
  323. (2 rows)
  324. --
  325. -- New syntaxes (7.1) permit new tests
  326. --
  327. (((((select * from int8_tbl)))));
  328. q1 | q2
  329. ------------------+-------------------
  330. 123 | 456
  331. 123 | 4567890123456789
  332. 4567890123456789 | 123
  333. 4567890123456789 | 4567890123456789
  334. 4567890123456789 | -4567890123456789
  335. (5 rows)
  336. --
  337. -- Check handling of a case with unknown constants. We don't guarantee
  338. -- an undecorated constant will work in all cases, but historically this
  339. -- usage has worked, so test we don't break it.
  340. --
  341. SELECT a.f1 FROM (SELECT 'test' AS f1 FROM varchar_tbl) a
  342. UNION
  343. SELECT b.f1 FROM (SELECT f1 FROM varchar_tbl) b
  344. ORDER BY 1;
  345. f1
  346. ------
  347. a
  348. ab
  349. abcd
  350. test
  351. (4 rows)
  352. -- This should fail, but it should produce an error cursor
  353. SELECT '3.4'::numeric UNION SELECT 'foo';
  354. ERROR: invalid input syntax for type numeric: "foo"
  355. LINE 1: SELECT '3.4'::numeric UNION SELECT 'foo';
  356. ^
  357. --
  358. -- Test that expression-index constraints can be pushed down through
  359. -- UNION or UNION ALL
  360. --
  361. CREATE TEMP TABLE t1 (a text, b text);
  362. CREATE TEMP TABLE t2 (ab text primary key);
  363. INSERT INTO t1 VALUES ('a', 'b'), ('x', 'y');
  364. INSERT INTO t2 VALUES ('ab'), ('xy');
  365. --
  366. -- Test that ORDER BY for UNION ALL can be pushed down to inheritance
  367. -- children.
  368. --
  369. CREATE TEMP TABLE t1c (b text, a text);
  370. INSERT INTO t1c VALUES ('v', 'w'), ('c', 'd'), ('m', 'n'), ('e', 'f');
  371. -- This simpler variant of the above test has been observed to fail differently
  372. create table events (event_id int primary key);
  373. create table other_events (event_id int primary key);
  374. drop table events_child, events, other_events;
  375. SELECT * FROM
  376. (SELECT 1 AS t, 2 AS x
  377. UNION
  378. SELECT 2 AS t, 4 AS x) ss
  379. WHERE x < 4
  380. ORDER BY x;
  381. t | x
  382. ---+---
  383. 1 | 2
  384. (1 row)
  385. SELECT * FROM
  386. (SELECT 1 AS t, (random()*3)::int AS x
  387. UNION
  388. SELECT 2 AS t, 4 AS x) ss
  389. WHERE x > 3
  390. ORDER BY x;
  391. t | x
  392. ---+---
  393. 2 | 4
  394. (1 row)
  395. select distinct q1 from
  396. (select distinct * from int8_tbl i81
  397. union all
  398. select distinct * from int8_tbl i82) ss
  399. where q2 = q2;
  400. q1
  401. ------------------
  402. 123
  403. 4567890123456789
  404. (2 rows)
  405. select distinct q1 from
  406. (select distinct * from int8_tbl i81
  407. union all
  408. select distinct * from int8_tbl i82) ss
  409. where -q1 = q2;
  410. q1
  411. ------------------
  412. 4567890123456789
  413. (1 row)