Skip to content
Advertisement

List Customer And Product Without Sale

Need to show the following columns (3) using UNION to return:

  • All customers who do not have an invoice
  • All products that were not sold
  1. Category: Is this related to “customer” or “product”? Print “customer or “product”
  2. ID: Customer.id (category=”customer”) or product.id (category=”product”)
  3. Name: customer.customer_name (category=”customer”) or product.product_name (category=”product”)

Tables:

Customer

  • id
  • customer_name
  • city_id
  • customer_address
  • contact_person
  • email
  • phone

Product

  • id
  • sku
  • product_name
  • product_description
  • current_price
  • quantity_in_stock

Invoice

  • id
  • invoice_number
  • customer_id
  • user_account_id
  • total_price
  • time_issued
  • time_due
  • time_paid
  • time_canceled
  • time_refunded

Invoice_Item

  • id
  • invoice_id
  • product_id
  • quantity
  • price
  • line_total_price

So far have the following:

Open to any suggestions as I’m stuck trying to figure out how to show the category as either “product” or “customer”. Thanks in advance!

Advertisement

Answer

Considering your data model and requirements, you should try the below SQLs. You can easily perform UNION with both the SQLs.

First SQL returns this list –> All customers who do not have an invoice

Second SQL returns this list –> All products that were not sold

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement