Solved

SQL INTERSECT in same table

Posted on 2012-03-10
3
536 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
Comment Utility
SELECT column A
  FROM table
 
INTERSECT

SELECT column B
  FROM table
0
 
LVL 2

Expert Comment

by:gaurang04
Comment Utility
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 31

Expert Comment

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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

771 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

12 Experts available now in Live!

Get 1:1 Help Now