• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 862
  • Last Modified:

Simple distinct join of two tables in SQL

I know this has to be easy but honestly I dont have the time to figure it out.

I have two tables with a single column... LastName
Table A
Smith
Jones

Table B
Jones
Williams

How can I get a single result set... with distinct values... without using a temp table...

ie.
Smith
Jones
Williams

On a single table I would usually do "select distinct(LastName) from TableA"...

Thanks!
0
James Talvy
Asked:
James Talvy
  • 2
1 Solution
 
wilcoxonCommented:
I don't have a good answer in sql for this.  I'd use a temp table, pipe it through Unix commands (I use sqsh for accessing Sybase), or whip up a quick perl script.
0
 
James TalvyAuthor Commented:
Can't do that since I need to use this as an intermediary step within a stored procedure...
0
 
Simone BCommented:
Will this work?

select distinct LastName from TableA
union
select distinct LastName from TableB

Using DISTINCT eliminates duplicates within each table, while using UNION (instead of UNION ALL) should eliminate the duplicates between the two tables.
0
 
James TalvyAuthor Commented:
Indeed it does seem to work thanks.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now