Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

SQL Exercises: Orders by customers not located in the same cities where their sales persons live

SQL Query on Multiple Tables: Exercise-3 with Solution

From the following tables, write a SQL query to find those salespeople who generated orders for their customers but are not located in the same city. Return ord_no, cust_name, customer_id (orders table), salesman_id (orders table).

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

Relational Algebra Expression:

Relational Algebra Expression: Orders by the customers not located in the same cities where their salesmen live.

Relational Algebra Tree:

Relational Algebra Tree: Orders by the customers not located in the same cities where their salesmen live.

Explanation:

Syntax of orders by the customers not located in the same cities where their salesmen lives

Pictorial presentation :

Result of orders by the customers not located in the same cities where their salesmen lives

Practice Online


Query Visualization:

Duration:

Query visualization of Orders by the customers not located in the same cities where their salesmen live - Duration

Rows:

Query visualization of Orders by the customers not located in the same cities where their salesmen live - Rows

Cost:

Query visualization of Orders by the customers not located in the same cities where their salesmen live - Cost

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: From the following tables, write a SQL query to find all the customers along with the salesperson who works for them. Return customer name, and salesperson name.
Next: From the following tables, write a SQL query to find those orders made by customers. Return order number, customer name.

Test your Programming skills with w3resource's quiz.

What is the difficulty level of this exercise?



SQL: Tips of the Day

SQL Server SELECT into existing table.

INSERT INTO dbo.TABLETWO
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

This assumes there's only two columns in dbo.TABLETWO - you need to specify the columns otherwise:

INSERT INTO dbo.TABLETWO
  (col1, col2)
SELECT col1, col2
  FROM dbo.TABLEONE
 WHERE col3 LIKE @search_key

Database: SQL Server

Ref: https://bit.ly/3y6tpA3