-4

This is the challeng for my class. Using INNER JOIN retrieve the following information:

  1. Using the customers and orders table find all the orders that were created by an owner of a company.

I came up with this but when I enter it it says empty set>>>

SELECT id FROM orders INNER JOIN customers USING (id);

These are the columns from the customers table. CUSTOMERS

 | id   | company | last_name | first_name  | email_address | job_title       
 | business_phone  | home_phone | mobile_phone | fax_number | address 
 | city | state_province  |  zip_postal_code |  country_region  | web_page    
 | notes | attachments |.

These are the columns from the orders table. ORDERS

| id  | employee_id | customer_id | order_date | shipped_date | shipper_id           
| ship_name | ship_address | ship_city | ship_state_province  
| ship_zip_postal_code  | ship_country_region  | shipping_fee | taxes                
| payment_type | paid_date | notes | tax_rate | tax_status_id | status_id
ahmet_y
  • 112
  • 8
  • No one is going to read that. Format properly. https://stackoverflow.com/help/how-to-ask – Eric Dec 18 '18 at 22:54
  • 1
    You seem to be matching order ID to customer ID.. – Vasan Dec 18 '18 at 23:06
  • See https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query, and don't include plainly irrelevant columns – Strawberry Dec 19 '18 at 06:59

2 Answers2

1

When you write:

SELECT id FROM orders INNER JOIN customers USING (id);

you are requesting:

SELECT o.ID              -- or c.ID
  FROM Orders AS o
  JOIN Customers AS c
    ON O.ID = C.ID;

You're asking "which customers have an ID that is the same as an order ID?", which is probably not what you meant.

Most likely, you want to use some variant of:

SELECT o.ID AS Order_ID, c.ID AS Customer_ID
  FROM Orders AS o
  JOIN Customers AS c
    ON O.Customer_ID = C.ID;

You get to choose what information you get in the select list.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
0

You can find examples of inner join syntax here. Here is one example of how to do this:

select o.* 
from orders o
inner join customers c on o.customer_id = c.id
/* not sure how "owner of company" is represented in the data, but I'm assuming it's some value for job_title */
where c.job_title = 'owner' 
Ryan Cogswell
  • 75,046
  • 9
  • 218
  • 198
  • What do the o and the c refer to? – user10788762 Dec 19 '18 at 01:03
  • They are table aliases. It just makes it easier to reference the tables in the join criteria so you don’t need to put `orders.customer_id = customers.id`. – Ryan Cogswell Dec 19 '18 at 01:08
  • @ryanc true, but it's not *just* for that – Strawberry Dec 19 '18 at 06:58
  • @Strawberry Yes, my “just” simply referred to the limits of their purpose in my example. I don’t think it would have been helpful to this user at this stage in their learning to describe joining to the same table multiple times or other similar purposes. – Ryan Cogswell Dec 19 '18 at 12:15