Display the names of customers who ordered the same set of products as customers from Brazil

383 Views Asked by At

Microsoft Sql Server 2017, database Northwind.

Hello everyone, I'm still trying to do these question in sql server, but I stacked and I don't know how to do this. Please give me some tip, how to bite it :P

select *     
from

(select companyname, products.productid, productname,[Order Details].OrderID
    from [order details], orders, customers, products
    where [order details].orderid=orders.orderid AND
          orders.customerid=customers.customerid AND
          [order details].productid=products.productid) c1
    inner join
    (select companyname, products.productid, productname, [Order Details].OrderID
    from [order details], orders, customers, products
    where [order details].orderid=orders.orderid AND
          orders.customerid=customers.customerid AND
          [order details].productid=products.productid  AND
          Customers.Country='Brazil') c2
          on c1.productId=c2.productId and c1.productname=c2.productname
1

There are 1 best solutions below

0
On

I'm unsure whether or not customers in Brazil should be excluded from the resulting data, but to point you in the right direction, consider the following query:

select c.*
from
    (
        select distinct o.productid
        from [order details] o inner join customers c
        on o.customerid = c.customerid
        where c.country = 'Brazil'
    ) p
    inner join [order details] o on p.productid = o.productid
    inner join customers c on o.customerid = c.customerid

Here, the nested selection obtains a distinct set of products purchased by customers in Brazil. The result of this selection is then joined to your order details table to obtain all customers who have purchased such products (this will include the Brazilians).