Search code examples
mysqlsqladminer

Show the list of all product's names ordered along with first and last names of the customers


I was doing an exercise and I am stuck on it. There are 2 tables:

  • Customers(id, firstname, lastname, address)
  • Orders (id, product_name, product_price, date_order DATE, id_customer, amount)

The query:

SELECT Orders.product_name, Customers.firstname, Customers.lastname
FROM Orders INNER JOIN
     Customers
     ON Orders.id_customer=Customers.id
ORDER BY Orders.id;

Expected result:

Show the list of all products' names ordered along with first and last names of the customers.

Include in the result only those customers who have no address in a database and sort the data by Orders.id.


Solution

  • Looks like you're pretty close. You just need a WHERE clause to match this requirement:

    Include to the result only those customers who has no address in a database

    Try this:

    SELECT Orders.product_name, Customers.firstname, Customers.lastname
    FROM Orders INNER JOIN Customers ON Orders.id_customer = Customers.id
    WHERE address IS NULL OR address = ''
    ORDER BY Orders.id;