Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL INTERSECT in same table

Posted on 2012-03-10
3
Medium Priority
?
1,000 Views
Last Modified: 2012-04-17
im trying to do an intersect command between two columns of the same table..


so basically i have column A and column B..

I want to return a column that if i look at an item in Column B, and that same item appears in column A, but it in column C (or return column A modified to only contain overlaps with B

how can i do that?

thanks
0
Comment
Question by:ambush276
3 Comments
 
LVL 14

Accepted Solution

by:
Muhammad Ahmad Imran earned 2000 total points
ID: 37705233
SELECT column A
  FROM table
 
INTERSECT

SELECT column B
  FROM table
0
 
LVL 2

Expert Comment

by:gaurang04
ID: 37705408
INTERSECT operator allows you to combine two table expressions into one and return a result set which consists of rows that appear in the results of both table expressions. INTERSECT operator, like UNION operator, removes all duplicated row from the result sets. Unlike the UNION operator, INTERSECT operator operates as AND operator on tables expression. It means data row appears in both table expression will be combined in the result set while UNION operator operates as OR operator (data row appear in one table expression or both will be combined into the result set). The syntax of using INTERSECT operator is like UNION as follows:
1      table_expression1
2      INTERSECT
3      table_expression2

Table expression can be any select statement which has to be union compatible and ORDER BY can be specified only behind the last table expression.

Be note that several RDBMS, including MySQL (version < 5.x), does not support INTERSECT operator.

This is an example of using INTERSECT operator. Here is the employees sample table
1      employee_id  name      department_id  job_id  salary
2      -----------  --------  -------------  ------  -------
3                1  jack                  1       1  3000.00
4                2  mary                  2       2  2500.00
5                3  newcomer         (NULL)       0  2000.00
6                4  anna                  1       1  2800.00
7                5  Tom                   2       2  2700.00
8                6  foo                   3       3  4700.00

We can find employee who work in department id 1 and two and have salary greater than 2500$ by using INTERSECT operator. This example using the sample table in both table expressions, you can test it on two different tables.)
1      SELECT *
2      FROM employees
3      WHERE department_id in (1,2)
4      INTERSECT
5      SELECT *
6      FROM employees
7      WHERE salary > 2500
1      employee_id  name    department_id  job_id  salary
2      -----------  ------  -------------  ------  -------
3                1  jack                1       1  3000.00
4                4  anna                1       1  2800.00
5                5  Tom                 2       2  2700.00
0
 
LVL 32

Expert Comment

by:awking00
ID: 37709588
Can you provide some examples and your expected result? Also, what dbms are you using?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

885 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