Solved

SQL INTERSECT in same table

Posted on 2012-03-10
3
626 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:
leoahmad earned 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

809 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