SQL Exercises: Orders by the customers not located in the same cities where their salesmen live
SQL Query on Multiple Tables: Exercise-3 with Solution
Write a SQL statement to display all those orders by the customers not located in the same cities where their salesmen live.
Sample table: salesman
Sample table: customer
Sample table: orders
Sample Solution :
SELECT ord_no, cust_name, orders.customer_id, orders.salesman_id FROM salesman, customer, orders WHERE customer.city <> salesman.city AND orders.customer_id = customer.customer_id AND orders.salesman_id = salesman.salesman_id;
Output of the query:
ord_no cust_name customer_id salesman_id 70004 Geoff Cameron 3009 5003 70003 Geoff Cameron 3009 5003 70011 Jozy Altidor 3003 5007 70001 Graham Zusi 3005 5002 70007 Graham Zusi 3005 5002 70012 Julian Green 3008 5002
Pictorial presentation :
Note: The pictorial represetation above is based on hypothetical table for the purpose of explanation only. Your answer may not match.
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a SQL statement to find the names of all customers along with the salesmen who works for them.
Next: Write a SQL statement that finds out each order number followed by the name of the customers who made the order.
What is the difficulty level of this exercise?