w3resource

SQL Exercise: Salesmen works either for one or more customer or not

SQL JOINS: Exercise-12 with Solution

Write a SQL statement to generate a list in ascending order of salespersons who work either for one or more customers or have not yet joined any of the customers.

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 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 Solution:

-- Selecting specific columns and renaming them for clarity
SELECT a.cust_name, a.city, a.grade, 
       b.name AS "Salesman", b.city 
-- Specifying the tables to retrieve data from ('customer' as 'a' and 'salesman' as 'b')
FROM customer a 
-- Performing a right outer join based on the salesman_id, including unmatched rows from 'salesman'
RIGHT OUTER JOIN salesman b 
ON b.salesman_id = a.salesman_id 
-- Sorting the result set by salesman_id in ascending order
ORDER BY b.salesman_id; 

Output of the Query:

cust_name	city		grade	Salesman	city
Brad Davis	New York	200	James Hoog	New York
Nick Rimando	New York	100	James Hoog	New York
Graham Zusi	California	200	Nail Knite	Paris
Julian Green	London		300	Nail Knite	Paris
Geoff Cameron	Berlin		100	Lauson Hen	San Jose
Brad Guzan	London			Pit Alex	London
Fabian Johnson	Paris		300	Mc Lyon		Paris
Jozy Altidor	Moscow		200	Paul Adam	Rome

Explanation:

The said SQL query is performing a right outer join on the customer table alias a and the salesman table alias b on the 'salesman_id' column. It is then selecting the 'cust_name', 'city', and 'grade' columns from the customer table, and the 'name' and 'city' columns from the salesman table. The result is ordered by the 'salesman_id' column.
This query will select all the rows from the salesman table and any matching rows from the customer table and returning the results in the order of salesman_id. If there is no match, it will return NULL for the non-matching columns of customer table.

Visual Explanation:

Result of a list in ascending order for the salesmen who works either for one or more customer or not yet join under any of the customer

Practice Online


Query Visualization:

Duration:

Query visualization of Prepare a list in ascending order for the salesmen who works either for one or more customer or not yet join under any of the customers - Duration

Rows:

Query visualization of Prepare a list in ascending order for the salesmen who works either for one or more customer or not yet join under any of the customers - Rows

Cost:

Query visualization of Prepare a list in ascending order for the salesmen who works either for one or more customer or not yet join under any of the customers - Cost

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

Previous SQL Exercise: Find customers who have placed no order or one or more.
Next SQL Exercise: Salesmen who works either for one or more customer.

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.