Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8891817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:50:14+00:00 2026-06-14T22:50:14+00:00

I have to write a LEFT JOIN query of find the records that do

  • 0

I have to write a LEFT JOIN query of find the records that do exist in one table and exists in other table (standard use for left join). There is only one table in this one for the join.

Table name: products

Join condition column: prdname

Two where clause conditions: subdivision='abc', subdivision='xyz'

Objecttive

With these two where clauses I will get two results containing prdname of two sets, I have to find prdname which exits in one but does not in another.


When I wrote the standard LEFT JOIN (case 2 below), it does not give any results that were incorrect so I wrote a subquery to check the results. It was 8 records so I decided to modify the query to case1 down here, and it worked.

My doubt is that both of them are pretty much the same so why second is incorrect?

Can any one help me?

Also, abc and xyz are random values so bother about them much.

Case in which It is working:

SELECT a.prdname
FROM   products a
       LEFT OUTER JOIN (SELECT prdname
                        FROM   products
                        WHERE  subdivision = 'xyz') b
                    ON a.prdname = b.prdname
WHERE  a.subdivision = 'abc'
       AND b.prdname IS NULL 

Case in which it is not working

SELECT DISTINCT( a.prdname )
FROM   products a
       LEFT OUTER JOIN products b
                    ON a.prdname = b.prdname
WHERE  a.subdivision = 'abc'
       AND b.subdivision = 'xyz'
       AND b.prdname IS NULL 

Subquery Query to test

SELECT DISTINCT( prdname )
FROM   products
WHERE  subdivision = 'abc'
       AND prdname NOT IN (SELECT DISTINCT prdname
                           FROM   products
                           WHERE  subdivision = 'xyz') 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:50:15+00:00Added an answer on June 14, 2026 at 10:50 pm

    By adding a WHERE condition on columns of the “outer” table you are essentially turning the outer join into an inner join because those rows that do not satisfy the join criteria will have a NULL value in those columns. And the comparison with NULL removes those rows from the result.

    You need to move that condition into the JOIN clause:

    SELECT a.prdname
    FROM   products a
      LEFT JOIN products b
             ON a.prdname = b.prdname
            AND b.subdivision = 'xyz'
    WHERE  a.subdivision = 'abc'
       AND b.prdname IS NULL;
    

    To make things a bit clearer, here is a simplified example.

    Assuming the following tables:

    Person

    id | firstname | lastname  
    ---+-----------+-----------
    1  | Arthur    | Dent      
    4  | Mary      | Moviestar 
    2  | Zaphod    | Beeblebrox
    3  | Tricia    | McMillian 
    

    Vehicle

    id | person_id | vehicle_name
    ---+-----------+-------------
    1  | 1         | Arthur's car
    2  | 2         | Zaphod's car
    

    Now fetch all people and their vehicles:

    select p.id,
           p.firstname, 
           v.vehicle_name
    from person p 
       left outer join vehicle v on p.id = v.person_id;
    

    This returns the following:

    id | firstname | vehicle_name
    ---+-----------+-------------
    1  | Arthur    | Arthur's car
    2  | Zaphod    | Zaphod's car
    3  | Tricia    | (null)      
    4  | Mary      | (null)      
    

    Now think about what happens when you add

    and v.vehicle_name = 'Arthur''s car' 
    

    to the query. The two rows where vehicle_car is null won’t satisfy that condition and thus are removed from the result.


    Now to your real question:

    To find products with subdivision ‘abc’ which do not exist in subdivision ‘xyz’ you can use the except operator:

    select prdname
    from products
    where subdivision = 'abc'
    except
    select prdname
    from products
    where subdivision = 'xyz';
    

    (For Oracle you must replace except with minus)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i'm struggling to write a particular MySQL Join Query. I have a table
I'm trying to write a query for a database that will left join a
i have this query that works fine SELECT t.username FROM users t LEFT JOIN
I have a query that i am trying to write to return the amount
I need to write an SQL-Query for a csv-export. I have a table stores,
I have a pretty long SQL query that I use to, for all of
I have some problem with php left join php write me a error You
It is bothering me that for a simple query, I have to write out
I have the following query sql query: select * from Articles ar left join
I'm trying to write a query that would extract products that have identical option

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.