Jump to content

Get supplier names for suppliers who do not supply part P2


Recommended Posts

image.png.74d9502c93b918ca7b7d42b2de7773b6.png

This is a sample database.

The answer(relational algebra query) is:(I am converting those queries to SQL)

image.thumb.png.5bc842d8c78911c7f47a767047f3e02f.png

So it is first finding all suppliers who supply P2 from shipments table

Then it finds all suppliers who do not supply p2.

Then it joins all those suppliers who do not supply p2 with suppliers table? What does this gives us? A join with those who do not supply part p2?

Now, finally, it subtracts that joined result-thosee who supply p2.

And that should bring the names of suppliers who do not supply p2.

I did not quite get how join helped here? Can anyone describe this?

Personally I would frame a query like this

select supplier_name from suppliers join
shipments
(select supplier_number from shipments
where part_number!='P2')
DIFFERENCE
(select supplier_number from shipments
where part_number='P2'
)

But obviously there are lots of mistakes here since I am not aware of what difference operator is in postgresql,how can I rename a table

 

To find records where there is no match in another table you use a LEFT JOIN. Data from the the second table where there is no match will contain NULL values.

SELECT s.name
FROM supplier s
     LEFT JOIN shipment sh
          ON s.suppid = sh.suppid
          AND sh.prodid = 'P2'
WHERE sh.suppid IS NULL

 

  • Great Answer 1

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.