w3resource

SQL Exercises: Customer and salesmen who lives in the same city

SQL Query on Multiple Tables: Exercise-1 with Solution

From the following tables, write a SQL query to find the salespeople and customers who live in the same city. Return customer name, salesperson name and salesperson city.

Sample table: salesman

Sample table: salesman

 salesman_id |    name    |   city   | commission 
-------------+------------+----------+------------
        5001 | James Hoog | New York |       0.15
        5002 | Nail Knite | Paris    |       0.13
        5005 | Pit Alex   | London   |       0.11
        5006 | Mc Lyon    | Paris    |       0.14
        5007 | Paul Adam  | Rome     |       0.13
        5003 | Lauson Hen | San Jose |       0.12

Sample table: customer

 customer_id |   cust_name    |    city    | grade | salesman_id 
-------------+----------------+------------+-------+-------------
        3002 | Nick Rimando   | New York   |   100 |        5001
        3007 | Brad Davis     | New York   |   200 |        5001
        3005 | Graham Zusi    | California |   200 |        5002
        3008 | Julian Green   | London     |   300 |        5002
        3004 | Fabian Johnson | Paris      |   300 |        5006
        3009 | Geoff Cameron  | Berlin     |   100 |        5003
        3003 | Jozy Altidor   | Moscow     |   200 |        5007
        3001 | Brad Guzan     | London     |       |        5005

Sample Solution:

-- This query selects specific columns ('customer.cust_name', 'salesman.name', 'salesman.city') from the 'salesman' and 'customer' tables.
-- It retrieves data where the 'city' column in the 'salesman' table matches the 'city' column in the 'customer' table.
SELECT customer.cust_name, salesman.name, salesman.city
-- Specifies the tables from which to retrieve the data (in this case, 'salesman' and 'customer').
FROM salesman, customer
-- Specifies the condition for joining the 'salesman' and 'customer' tables based on the equality of the 'city' columns.
WHERE salesman.city = customer.city;

Output of the query:

cust_name	name		city
Nick Rimando	James Hoog	New York
Brad Davis	James Hoog	New York
Julian Green	Pit Alex	London
Fabian Johnson	Mc Lyon		Paris
Fabian Johnson	Nail Knite	Paris
Brad Guzan	Pit Alex	London

Code Explanation:

The said query in SQL that joins the 'salesman' and 'customer' tables based on the city column. The result set includes the customer name (cust_name), salesman name (name), and city from the salesman table. The WHERE clause specifies the join condition between the two tables, which is that the city column must be equal in both tables.

Relational Algebra Expression:

Relational Algebra Expression: Find the customer and salesmen who lives in same city.

Relational Algebra Tree:

Relational Algebra Tree: Find the customer and salesmen who lives in same city.

Explanation:

Syntax to find the customer and salesmen who lives in same city

Visual presentation:

Result to the customer and salesmen who lives in same city

Practice Online


Query Visualization:

Duration:

Query visualization of Find the customer and salesmen who lives in same city - Duration

Rows:

Query visualization of Find the customer and salesmen who lives in same city - Rows

Cost:

Query visualization of Find the customer and salesmen who lives in same city - Cost

 

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous SQL Exercise: SQL Query on Multiple Tables Exercises Home
Next SQL Exercise: Customers along with the salesmen who works for them.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/sql-exercises/sql-quering-on-multiple-table-exercise-1.php