w3resource

SQL Exercises: Customer lives in a city other than the salesman's

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

Code Explanation:

The said query in SQL that joins the 'salesman', 'customer', and 'orders' tables. The result set includes the order number (ord_no), customer name (cust_name), customer ID (customer_id), and salesman ID (salesman_id). The WHERE clause specifies multiple conditions for the join.
There is a first condition that must be fulfilled in order for the city column in the customer table to not be the same as the city column in the salesman table.
The second and third conditions specify the join conditions between the orders table and the customer and salesman tables, respectively, which is that the customer_id and salesman_id columns of the orders table must be equal to the corresponding columns in the customer and salesman tables.

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

Visual 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 SQL Exercise: Customers along with the salesmen who works for them.
Next SQL Exercise: Find out customers who made the order.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

SQL: Tips of the Day

Grouped LIMIT in PostgreSQL: Show the first N rows for each group?

db=# SELECT * FROM xxx;
 id | section_id | name
----+------------+------
  1 |          1 | A
  2 |          1 | B
  3 |          1 | C
  4 |          1 | D
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
  8 |          2 | H
(8 rows)

I need the first 2 rows (ordered by name) for each section_id, i.e. a result similar to:

id | section_id | name
----+------------+------
  1 |          1 | A
  2 |          1 | B
  5 |          2 | E
  6 |          2 | F
  7 |          3 | G
(5 rows)

PostgreSQL v9.3 you can do a lateral join

select distinct t_outer.section_id, t_top.id, t_top.name from t t_outer
join lateral (
    select * from t t_inner
    where t_inner.section_id = t_outer.section_id
    order by t_inner.name
    limit 2
) t_top on true
order by t_outer.section_id;

Database: PostgreSQL

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

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook