Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
41 views

CS Me SQL Query 5

This document contains 5 SQL queries: 1. Finds customers and salesmen who live in the same city. 2. Finds all customers and their salesmen. 3. Finds orders by customers not in the same city as their salesman. 4. Finds salesmen with more than one customer, using a subquery. This can be rewritten without a subquery using a join and group by. 5. Finds salesmen with only one customer, using a subquery and not exists condition.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

CS Me SQL Query 5

This document contains 5 SQL queries: 1. Finds customers and salesmen who live in the same city. 2. Finds all customers and their salesmen. 3. Finds orders by customers not in the same city as their salesman. 4. Finds salesmen with more than one customer, using a subquery. This can be rewritten without a subquery using a join and group by. 5. Finds salesmen with only one customer, using a subquery and not exists condition.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

TABLE

Order of SQL Statement


Query 1
• Find the name and city of those
customers and salesmen who lives in the
same city.

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

SELECT C.cust_name S.name S.city


FROM salesman AS S customer AS C
WHERE S.city = C.city
Query 2
• Find the names of all customers along
with the salesmen who works for them.
cust_name name
Nick Rimando James Hoog
Brad Davis James Hoog
Graham Zusi Nail Knite
Julian Green Nail Knite
Fabian Johnson Mc Lyon
Geoff Cameron Lauson Hen
Jozy Altidor Paul Adam
Brad Guzan Pit Alex

SELECT customer.cust_name salesman.name


FROM customer salesman
WHERE salesman.salesman_id = customer.salesman_id;
Query 3
• Display all those orders by the customers not located
in the same cities where their salesmen live.

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

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;
Query 4 (using subquery)
Find the name and ids of all salesmen who
had more than one customer.
salesman_id name
5001 James Hoog
5002 Nail Knite

SELECT salesman_id, name


FROM salesman AS a
WHERE 1 <
(SELECT COUNT(*)
FROM customer AS c
WHERE c.salesman_id = a.salesman_id);

• Can we make this query unnested? If yes how?

SELECT c.salesman_id, s.name FROM salesman AS s, customer AS c


where s.salesman_id = c.salesman_id
group by c.salesman_id, s.name Having count(c.salesman_id) > 1;
Query 5 (using subquery)
Write a query to find all the salesmen who worked for
only one customer.
salesman_id name city commission
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

SELECT *
FROM salesman
WHERE salesman_id IN ( SELECT
DISTINCT salesman_id FROM
customer a
WHERE NOT EXISTS (
SELECT * FROM customer b
WHERE a.salesman_id = b.salesman_id
AND a.cust_name <> b.cust_name));

You might also like