2

Table 1

Product    product Description
ABC        Hardware
CBD        Software
BCQ        Component
DEF        License

Table 2

Product  Product Description Order ID
ABC      Hardware            1234
ABC      Hardware            2345
ABC      Hardware            5678
BCQ      Component           7896
BCQ      Component           9681

Here is the background table 1 has the product data, table 2 has the product data with order ids I want to know how many products are not ordered (in this scenario "CBD Software" and "DEF License" has not orders placed hence is missing in table 2). now i want to know how many products were never placed on an order.

1
  • Request you to please accept the answer as it motivate me to answer more questions. Commented Jan 11, 2019 at 19:08

4 Answers 4

3

You can try the below query

SELECT * FROM Table1
where ProductId not in (SELECT ProductId FROM ProductDescription)

Below is the actual example

Create table ProductDescription (ProdCd varchar(10), ProdDes Varchar(20))
insert into ProductDescription values ('ABC', 'Hardware'),
('CBD',     'Software'),
('BCQ',     'Component'),
('DEF',     'License')

Create table ProductOrders (ProdCd varchar(10), ProdDes Varchar(20), OrderId int)
insert into ProductOrders values ('ABC',      'Hardware',      1234),
('ABC',      'Hardware',      2345),
('ABC',      'Hardware',      5678),
('BCQ',      'Component',     7896),
('BCQ',      'Component',     9681)

SELECT * FROM ProductDescription
where ProdCd not in (SELECT ProdCd FROM ProductOrders)

The output is as shown below

ProdCd  ProdDes
---------------
CBD     Software
DEF     License

You can find the live demo here Live Demo

Sign up to request clarification or add additional context in comments.

Comments

2

You can also use LEFT JOIN and IS NULL in where condition

SELECT t1.*
FROM table1 t1
LEFT JOIN table2 t2 ON t2.product = t1.product
WHERE t2.product IS NULL

Comments

2

Do you just want not exists?

select p.*
from products p
where not exists (select 1 from orders o where o.product = p.product);

1 Comment

You forgot the orders alias o
1

Here's yet another way, with RIGHT JOIN and IS NULL:

declare @prod table
(
    Product varchar(100),
    [product Description] varchar(100)
)

insert into @prod
select 'ABC','Hardware' union
select 'CBD','Software' union
select 'BCQ','Component' union
select 'DEF','License'


declare @prodOrders table
(
    Product varchar(100),
    [product Description] varchar(100),
    [orderid] int
)
insert into @prodOrders
select 'ABC','Hardware',1234  union
select 'ABC','Hardware',2345  union
select 'ABC','Hardware',5678  union
select 'BCQ','Component',7896 union
select 'BCQ','Component',9681

SELECT t1.*
FROM @prodOrders t2 
right JOIN @prod t1 ON t2.product = t1.product
WHERE t2.product IS NULL

Live Demo

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.