Solved

SQL INTERSECT in same table

Posted on 2012-03-10
3
558 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now