Questions tagged [union]

Use this tag only for questions about UNION, a keyword of the SQL language for combining the results of multiple SQL queries. Use [union-all] for the UNION-ALL keyword. Use the tag [unions] for unions in C, C++, and similar languages.

UNION is a keyword of SQL for combining the results of multiple SQL queries. The results are combined and duplicate rows are eliminated (similar to DISTINCT). If UNION ALL is used, the rows are combined but duplicates are not removed.

Use the tag for unions in C, C++, and similar languages. Use for the UNION-ALL keyword.

Reference

5266 questions
31
votes
3 answers

How can I insert all values of one HashSet into another HashSet?

I have two HashSets and I would like to implement a = a U b. If possible, I'd like to use HashSet::union rather than loops or other tweaks. I tried the following: use std::collections::HashSet; let mut a: HashSet = [1, 2,…
m.raynal
  • 2,983
  • 2
  • 21
  • 34
30
votes
1 answer

PostgreSQL nested CTE and UNION

I'm trying to learn SQL, using PostgreSQL 9.1.3. I would like to understand some behavior that strikes me as inconsistent. To wit: This works: WITH innermost AS (SELECT 2) SELECT * FROM innermost UNION SELECT 3; I get this: ?column? ---------- …
Adam Mackler
  • 1,980
  • 1
  • 18
  • 32
29
votes
4 answers

Is there a neater linq way to 'Union' a single item?

If I have two sequences and I want to process them both together, I can union them and away we go. Now lets say I have a single item I want to process between the two sequencs. I can get it in by creating an array with a single item, but is there a…
Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
28
votes
8 answers

SQL Server: How to use UNION with two queries that BOTH have a WHERE clause?

Given: Two queries that require filtering: select top 2 t1.ID, t1.ReceivedDate from Table t1 where t1.Type = 'TYPE_1' order by t1.ReceivedDate desc And: select top 2 t2.ID from Table t2 where t2.Type = 'TYPE_2' order by t2.ReceivedDate…
aarona
  • 35,986
  • 41
  • 138
  • 186
28
votes
6 answers

Performance of UNION versus UNION ALL in SQL Server

I have to run a SELECT statement across several tables. I am sure the tables return different records. I am anyway using UNION ALL. Is it better to use UNION or of UNION ALL in performance terms when I am sure the tables return different records?
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
27
votes
6 answers

Support UNION function in BigQuery SQL

BigQuery does not seem to have support for UNION yet: https://developers.google.com/bigquery/docs/query-reference (I don't mean unioning tables together for the source. It has that.) Is it coming soon?
mdahlman
  • 9,204
  • 4
  • 44
  • 72
26
votes
4 answers

UNION after ORDER BY and LIMIT

My goal is to execute two different queries and then combine them. My code is: SELECT * FROM some tables WHERE ... ORDER BY field1 LIMIT 0,1 UNION SELECT * FROM some tables WHERE ... I get the following error: #1221 - Incorrect usage of…
lvil
  • 4,326
  • 9
  • 48
  • 76
26
votes
7 answers

SQL: how to use UNION and order by a specific select?

I have two selects: SELECT id FROM a -- returns 1,4,2,3 UNION SELECT id FROM b -- returns 2,1 I'm receiving correct num of rows, like: 1,4,2,3. But I want b table results first: 2,1,4,3 or 2,1,3,4 How can I do this? (I'm using Oracle)
Topera
  • 12,223
  • 15
  • 67
  • 104
26
votes
3 answers

SQL Union All with order by and limit (Postgresql)

In the following query I get syntax error: SELECT , FROM ORDER BY LIMIT 1 UNION ALL SELECT , FROM WHERE ORDER BY LIMIT 1; syntax error at or…
michael
  • 3,835
  • 14
  • 53
  • 90
26
votes
5 answers

Union of multiple ranges

I have these ranges: 7,10 11,13 11,15 14,20 23,39 I need to perform a union of the overlapping ranges to give ranges that are not overlapping, so in the example: 7,20 23,39 I've done this in Ruby where I have pushed the start and end of the range…
bioinf80
  • 523
  • 3
  • 7
  • 14
25
votes
1 answer

The data type text cannot be used as an operand to the UNION, INTERSECT or EXCEPT operators because it is not comparable

I have a table Id (PK) Owner int DescriptionText text which is joined to another table Id (FK) Participant int The Owner can be a Participant, and if it is, the same reference (into user table) is in Owner and Participant. So I did: SELECT…
Alexander
  • 19,906
  • 19
  • 75
  • 162
24
votes
2 answers

MySQL: using UNION vs multiple queries

I'm interested to know if a UNION is better than running multiple queries. If so, are there times that multiple queries would be faster or preferred for other reasons?
Yehosef
  • 17,987
  • 7
  • 35
  • 56
24
votes
3 answers

How to have a custom sort order for a union query in Postgres

With a query like this (simplified for clarity): SELECT 'East' AS name, * FROM events WHERE event_timestamp BETWEEN '2015-06-14 06:15:00' AND '2015-06-21 06:15:00' UNION SELECT 'West' AS name, * FROM events WHERE event_timestamp BETWEEN…
Dave
  • 1,696
  • 4
  • 23
  • 47
24
votes
3 answers

MySQL - How to unpivot columns to rows?

ID | a | b | c 1 | a1 | b1 | c1 2 | a2 | b2 | c2 How do I reorganize the rows as ID, columntitle, value? 1 | a1 | a 1 | b1 | b 1 | c1 | c 2 | a2 | a 2 | b2 | b 2 | c2 | c
user2128539
  • 241
  • 1
  • 2
  • 5
23
votes
8 answers

geospatial queries in javascript

I'm looking for a library in javascript that would allow me to make geospatial queries. I know about OpenLayers and GoogleMaps, but this two do not support things like union intersection and so on. +----+ | | | +-+--+ +--+-+ | | | …
wolktm
  • 263
  • 3
  • 10