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

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


Sample table: salesman


Sample Solution:

SELECT a.cust_name,a.city,a.grade, 
b.name AS "Salesman", b.city 
FROM customer a 
RIGHT OUTER JOIN salesman b 
ON b.salesman_id=a.salesman_id 
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:

Syntax 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

Pictorial presentation:

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: Write a SQL statement to make a report with customer name, city, order number, order date, order amount salesman name and commission to find that either any of the existing customers have placed no order or placed one or more orders by their salesman or by own.
Next: From the following tables write a SQL query to list all salespersons along with customer name, city, grade, order number, date, and amount.

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