Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL INTERSECT in same table

Posted on 2012-03-10
3
Medium Priority
?
892 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
[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
3 Comments
 
LVL 14

Accepted Solution

by:
leoahmad 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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
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. …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 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