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
1685
votes
19 answers

What is the difference between UNION and UNION ALL?

What is the difference between UNION and UNION ALL?
Brian G
  • 53,704
  • 58
  • 125
  • 140
326
votes
14 answers

What is the difference between JOIN and UNION?

What is the difference between JOIN and UNION? Can I have an example?
Gold
  • 60,526
  • 100
  • 215
  • 315
282
votes
12 answers

How to order by with union in SQL?

Is it possible to order when the data is come from many select and union it together? Such as Select id,name,age From Student Where age < 15 Union Select id,name,age From Student Where Name like "%a%" How can I order this query by name? I tried…
Guilgamos
  • 3,621
  • 4
  • 20
  • 11
197
votes
3 answers

How can I get the intersection, union, and subset of arrays in Ruby?

I want to create different methods for a class called Multiset. I have all the required methods, but I'm unsure of how to write intersection, union, and subset methods. For intersection and union, my code starts like this: def intersect(var) x =…
user487743
  • 2,145
  • 4
  • 16
  • 11
164
votes
24 answers

Intersection and union of ArrayLists in Java

Are there any methods to do so? I was looking but couldn't find any. Another question: I need these methods so I can filter files. Some are AND filters and some are OR filters (like in set theory), so I need to filter according to all files and the…
yotamoo
  • 5,334
  • 13
  • 49
  • 61
164
votes
14 answers

Using union and order by clause in mysql

I want to use order by with union in mysql query. I am fetching different types of record based on different criteria from a table based on distance for a search on my site. The first select query returns data related to the exact place search . The…
Aditya
  • 1,705
  • 2
  • 13
  • 16
129
votes
6 answers

How can I merge two maps in go?

I have a recursive function that creates objects representing file paths (the keys are paths and the values are info about the file). It's recursive as it's only meant to handle files, so if a directory is encountered, the function is recursively…
jeffknupp
  • 5,966
  • 3
  • 28
  • 29
107
votes
17 answers

ActiveRecord Query Union

I've written a couple of complex queries (at least to me) with Ruby on Rail's query interface: watched_news_posts = Post.joins(:news => :watched).where(:watched => {:user_id => id}) watched_topic_posts = Post.joins(:post_topic_relationships =>…
LandonSchropp
  • 10,084
  • 22
  • 86
  • 149
103
votes
3 answers

Combining UNION and LIMIT operations in MySQL query

I have a Jobs and a Companies table, and I want to extract 20 jobs that meet the following criteria: Jobs only from two (2) named companies There can at most be 10 jobs per company I have tried the following SELECT with UNION DISTINCT, but the…
user172517
95
votes
10 answers

How do I combine complex polygons?

Given two polygons: POLYGON((1 0, 1 8, 6 4, 1 0)) POLYGON((4 1, 3 5, 4 9, 9 5, 4 1),(4 5, 5 7, 6 7, 4 4, 4 5)) How can I calculate the union (combined polygon)? Dave's example uses SQL server to produce the union, but I need to accomplish the same…
grenade
  • 31,451
  • 23
  • 97
  • 126
90
votes
5 answers

Simplest way to form a union of two lists

What is the easiest way to compare the elements of two lists say A and B with one another, and add the elements which are present in B to A only if they are not present in A? To illustrate, Take list A = {1,2,3} list B = {3,4,5} So after the…
R.S.K
  • 2,114
  • 4
  • 26
  • 32
86
votes
16 answers

SQL Query - Using Order By in UNION

How can one programmatically sort a union query when pulling data from two tables? For example, SELECT table1.field1 FROM table1 ORDER BY table1.field1 UNION SELECT table2.field1 FROM table2 ORDER BY table2.field1 Throws an exception Note: this is…
Curtis Inderwiesche
  • 4,940
  • 19
  • 60
  • 82
85
votes
22 answers

How to perform union on two DataFrames with different amounts of columns in Spark?

I have 2 DataFrames: I need union like this: The unionAll function doesn't work because the number and the name of columns are different. How can I do this?
Allan Feliph
  • 862
  • 1
  • 8
  • 8
85
votes
6 answers

SQL Performance UNION vs OR

I just read part of an optimization article and segfaulted on the following statement: When using SQL replace statements using OR with a UNION: select username from users where company = ‘bbc’ or company = ‘itv’; to: select username from users…
Jason McCreary
  • 71,546
  • 23
  • 135
  • 174
77
votes
2 answers

How do you UNION with multiple CTEs?

How do you use UNION with multiple Common Table Expressions? I'm trying to put together some summary numbers but no matter where I put the ;, I always get an error SELECT COUNT(*) FROM dbo.Decision_Data UNION SELECT COUNT(DISTINCT…
SteveC
  • 15,808
  • 23
  • 102
  • 173
1
2 3
99 100