?
Solved

Simple distinct join of two tables in SQL

Posted on 2013-01-14
4
Medium Priority
?
841 Views
Last Modified: 2013-01-14
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
Comment
Question by:James Talvy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Expert Comment

by:wilcoxon
ID: 38775932
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
 
LVL 1

Author Comment

by:James Talvy
ID: 38775942
Can't do that since I need to use this as an intermediary step within a stored procedure...
0
 
LVL 11

Accepted Solution

by:
Simone B earned 1600 total points
ID: 38776035
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
 
LVL 1

Author Closing Comment

by:James Talvy
ID: 38776144
Indeed it does seem to work thanks.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question