Find supplier which supplies a product that others don't - sql

I'm trying to write an SQL query which selects the supplier based on the fact that it can supply a product other suppliers cannot.
I have 2 columns:
Supplier and Product
How would I select all the suppliers which supply at least 1 product which other suppliers do not supply?
I currently have:
SELECT incart.product, incart.supplier
FROM incart
WHERE incart.product
HAVING count(incart.supplier)=1
;

Try this:
SELECT
i1.supplier
FROM incart i1
WHERE i1.product NOT IN(SELECT product
FROM incart i2
WHERE i1.supplier <> i2.supplier);
For example, for the following sample data:
| PRODUCT | SUPPLIER |
|---------|----------|
| 1 | a |
| 2 | b |
| 3 | b |
| 2 | c |
| 3 | c |
| 4 | c |
It will select suppliers a and c, because supplier a supplies product 1 which others don't, and supplier c supplies product 4 which others don't.
SQL Fiddle Demo

Related

Select buyers and sellers data from customer

I have two tables Customer and Market
Select * from Customer :
customer_id | f_name | l_name
-------------------------------
1 | Sam | Brow
2 | Alex | Fore
3 | Marc | Lor
4 | Fab | Sow
Select * from Market
Orderid | Product | SellerID | BuyerID
-----------------------------------------
5 | Apple | 1 | 2
6 | Juice | 3 | 4
When doing this SELECT to have Sellers and buyers data, I have data of all customers.
SELECT c.f_name, c.l_name ,m.Orderid
FROM Customer c
INNER JOIN Market m ON m.BuyerID = c.customer_id OR m.SellerID = c.customer_id
Instead, I need to separate the data of buyers on their own and sellers on their own. I'd expect something like this :
Orderid | Seller_f_name | Buyer_f_name
----------------------------------------
5 | Sam | Alex
6 | Marc | Fab
Any idea please ?
You need to join the market table twice with customer table -
SELECT Orderid, C1.f_name Seller_f_name, C2.f_name Buyer_f_name
FROM Market M
LEFT JOIN Customer C1 ON M.SellerID = C1.customer_id
LEFT JOIN Customer C2 ON M.BuyerID = C2.customer_id;

SQL proble with join condition

I have a data base like here
I need to create a request to list the number of product available by product category.
However i should not list the product category without products.
We want at the end only column CATEGORY_NAME (product_category.name) and PRODUCT_COUNT
Example of result :
---------------------------------
| CATEGORY_NAME | PRODUCT_COUNT |
---------------------------------
| Books | 3 |
| Automotive | 2 |
| High-tech | 8 |
--------------------------------
I try this request but it's not good:
SELECT name AS CATEGORY_NAME, COUNT(available_stock) AS PRODUCT_COUNT
FROM product
GROUP BY name
WHERE available_stock IS NOT NULL

Getting an empty result with 'AND' operation and wrong result with 'OR' operation in SQL

I have two tables and I want to find out the customer_id and customer_name of all customers who bought product A and B both.
Customers table:
+-------------+---------------+
| customer_id | customer_name |
+-------------+---------------+
| 1 | Daniel |
| 2 | Diana |
| 3 | Elizabeth |
| 4 | Jhon |
+-------------+---------------+
Orders table:
+------------+--------------+---------------+
| order_id | customer_id | product_name |
+------------+--------------+---------------+
| 10 | 1 | A |
| 20 | 1 | B |
| 30 | 1 | D |
| 40 | 1 | C |
| 50 | 2 | A |
| 60 | 3 | A |
| 70 | 3 | B |
| 80 | 3 | D |
| 90 | 4 | C |
+------------+--------------+---------------+
In this example only the customers with id 1 and 3 have bought both the product A and B.
To find that i wrote this code -
SELECT distinct c.customer_id,
c.customer_name
from customers c inner join orders o
on c.customer_id = o.customer_id
where o.product_name = 'A' and o.product_name = 'B'
When I am doing this I am getting an empty result.
So tried to use OR -
SELECT distinct c.customer_id,
c.customer_name
from customers c inner join orders o
on c.customer_id = o.customer_id
where o.product_name = 'A' or o.product_name = 'B'
output -
customer_name customer_id
Daniel 1
Diana 2
Elizabeth 3
Based on OR it is working right but I am still not getting the result I am trying to find. Because customer with id 2 only bought A and not Product B. And Using AND bringing me an empty result.
I always feel confused with AND and OR operations. can someone help?
If you want both use aggregation:
select c.customer_id, c.customer_name
from customers c inner join
orders o
on c.customer_id = o.customer_id
where o.product_name in ('A', 'B')
group by c.customer_id, c.customer_name
having count(distinct product_name) = 2;
Note: This assumes that the data could have multiple rows for a customer and product. If that is not possible, just use count(*) = 2 for performance reasons.

Select based on multiple ID's in the same column

I have two SQL tables already built (not by me) :
One table that has all the ingredients with ID's assigned to them :
IngredientID | IngredientName
1 | Milk
2 | Eggs
3 | Flower
and the second table that has finished products that have reference to ingredientID in the ProductIngredients column but no the product name and they are built like this :
ProductID | ProductName | ProductIngredients
1 | Tomato Soup | NULL
2 | Pancakes | ;1;2;3
3 | Omlette | ;1;2
If they don't have any reference then its written NULL, and if they have, then they start with a ";" symbol and every ingredient is divided by a ";" symbol.
My goal is to join those two tables and make a select statement that returns me, instead of the ID's of the other column, the actual names of the used ingredients like this :
ProductID | ProductName | ProductIngredients
1 | Tomato Soup |
2 | Pancakes | Milk, Eggs, Flower
3 | Omlette | Milk, Eggs
Can anyone help out with this?
You need a left join of Products to ProductIngredients and group by product:
select p.ProductID, p.ProductName,
group_concat(i.IngredientName order by i.IngredientID) ProductIngredients
from Products p left join Ingredients i
on concat(p.ProductIngredients, ';') like concat('%;', i.IngredientID,';%')
group by p.ProductID, p.ProductName
The function group_concat() works in MySql but you can find similar functions to other databases.
See the demo.
Results:
| ProductID | ProductName | ProductIngredients |
| --------- | ----------- | ------------------ |
| 1 | Tomato Soup | |
| 2 | Pancakes | Milk,Eggs,Flower |
| 3 | Omlette | Milk,Eggs |

How to output parent child relationships within tables in sql?

I have Table1 which shows the child and parent relationships between products. How to I get the output table with sql? Basically, Input table shows that B is a child of A and B1 and B2 are child of B. The output would need to show that as well, and also that B1 and B2 are child of A.
I believe what you need is hierarchical queries.
As an example, you could do:
SELECT childproduct, parentproduct
FROM product_table
START WITH parentproduct = 'Product A'
CONNECT BY PRIOR childproduct = parentproduct;
Note: This assumed that you know the root (top level) element is 'Product A'. Although it may work without it too.
The answer to your problem is shown in this sqlfiddle: http://sqlfiddle.com/#!4/87657/61
You do need to use a Hierarchical Query as shown below:
SELECT ChildProduct, CONNECT_BY_ROOT ParentProduct
FROM table_name
WHERE LEVEL >= 1
CONNECT BY PRIOR ChildProduct = ParentProduct
| CHILD | CONNECT_BY_ROOTPARENT |
|------------|-----------------------|
| Product B | Product A |
| Product B1 | Product A |
| Product B2 | Product A |
| Product C | Product A |
| Product D | Product A |
| Product D1 | Product A |
| Product E1 | Product A |
| Product E2 | Product A |
| Product B1 | Product B |
| Product B2 | Product B |
| Product D1 | Product D |
| Product E1 | Product D |
| Product E2 | Product D |
| Product E1 | Product D1 |
| Product E2 | Product D1 |