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
49
votes
2 answers

Combine two sql select queries (in postgres) with LIMIT statement

I've got a table and I want a query that returns the last 10 records created plus the record who's id is x. I'm trying to do - SELECT * FROM catalog_productimage ORDER BY date_modified LIMIT 10 UNION SELECT * FROM catalog_productimage WHERE…
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
46
votes
4 answers

Python set Union and set Intersection operate differently?

I'm doing some set operations in Python, and I noticed something odd.. >> set([1,2,3]) | set([2,3,4]) set([1, 2, 3, 4]) >> set().union(*[[1,2,3], [2,3,4]]) set([1, 2, 3, 4]) That's good, expected behaviour - but with intersection: >> set([1,2,3]) &…
Bilal Akil
  • 4,716
  • 5
  • 32
  • 52
41
votes
6 answers

SQL Server UNION - What is the default ORDER BY Behaviour

If I have a few UNION Statements as a contrived example: SELECT * FROM xxx WHERE z = 1 UNION SELECT * FROM xxx WHERE z = 2 UNION SELECT * FROM xxx WHERE z = 3 What is the default order by behaviour? The test data I'm seeing essentially does not…
Ray Booysen
  • 28,894
  • 13
  • 84
  • 111
39
votes
2 answers

Select from union in SQL Server

Is it possible to select from the result of a union? For example I'm trying to do something like: SELECT A FROM ( SELECT A, B FROM TableA UNION SELECT A, B FROM TableB ) WHERE B > 'some value' Am I missing anything or making an…
BlargleMonster
  • 1,602
  • 2
  • 18
  • 33
36
votes
10 answers

How to execute UNION without sorting? (SQL)

UNION joins two results and remove duplicates, while UNION ALL does not remove duplicates. UNION also sort the final output. What I want is the UNION ALL without duplicates and without the sort. Is that possible? The reason for this is that I want…
hightow
  • 769
  • 2
  • 9
  • 16
36
votes
3 answers

SQL returns less results when using UNION?

I have a SQL Server stored procedure that doesn't give me all the results when I add in the union and the second half. The first half will give me all 6 results, using the union, I only get 5. There are 3 patients with their own [chart number],…
dave k
  • 1,329
  • 4
  • 22
  • 44
36
votes
3 answers

MySQL: FULL OUTER JOIN - How do I merge one column?

I have a question regarding a FULL OUTER JOIN in MySQL. I have two (or more tables): table1 table2 id value id value2 1 a 1 b 2 c 3 d 3 e 4 f I have used this query to get my join: SELECT * FROM table1 LEFT…
Mig Cervantez
  • 363
  • 1
  • 3
  • 4
35
votes
4 answers

How to use group by with union in T-SQL

How can I using group by with union in T-SQL? I want to group by the first column of a result of union, I wrote the following SQL but it doesn't work. I just don't know how to reference the specified column (in this case is 1) of the union…
Just a learner
  • 26,690
  • 50
  • 155
  • 234
35
votes
10 answers

What's a good, generic algorithm for collapsing a set of potentially-overlapping ranges?

I have a method that gets a number of objects of this class class Range { public T Start; public T End; } In my case T is DateTime, but lets use int for simplicity. I would like a method that collapses those ranges into ones that cover…
Svish
  • 152,914
  • 173
  • 462
  • 620
33
votes
3 answers

Shallow copy of a hashset

Whats the best way of doing it? var set2 = new HashSet(); Traverse the set with a foreach like this. foreach (var n in set) set2.Add(n); Or use something like union like this. set2 = set.UnionWith(set); // all the elements
alan2here
  • 3,223
  • 6
  • 37
  • 62
33
votes
3 answers

MYSQL UNION DISTINCT

I have two selects that I'm currently running as a UNION successfully. (SELECT a.user_id, a.updatecontents AS city, b.country FROM userprofiletemp AS a LEFT JOIN userattributes AS b ON a.user_id=b.user_id WHERE…
Adam
  • 19,932
  • 36
  • 124
  • 207
33
votes
5 answers

how to convert sql union to linq

I have the following Transact SQL query using a union. I need some pointers as to how this would look in LINQ i.e some examples wouldbe nice or if anyone can recommend a good tutorial on UNIONS in linq. select top 10 Barcode,…
lowlyintern
  • 331
  • 1
  • 3
  • 3
32
votes
1 answer

How to properly union with set

I understand that any python set union with empty set would result in itself. But some strange behave I detect when union is inside of a for loop. looks good num= set([2,3,4]) emp= set() print num|emp >>>set([2, 3, 4]) confused s = set() inp =…
lcs
  • 432
  • 1
  • 4
  • 10
32
votes
10 answers

UNION query with codeigniter's active record pattern

How to do UNION query with PHP CodeIgniter framework's active record query format?
strike_noir
  • 4,080
  • 11
  • 57
  • 100
32
votes
4 answers

Select distinct values from multiple columns in same table

I am trying to construct a single SQL statement that returns unique, non-null values from multiple columns all located in the same table. SELECT distinct tbl_data.code_1 FROM tbl_data WHERE tbl_data.code_1 is not null UNION SELECT…
regulus
  • 939
  • 3
  • 13
  • 21