0% found this document useful (0 votes)
12 views

LABSHEET 6

Uploaded by

bhavisama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

LABSHEET 6

Uploaded by

bhavisama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

LABSHEET 6:

1. Write a SQL statement to know which salesman are working for which customer.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
s.city AS salesman_city,
c.customer_id,
c.cust_name AS customer_name,
c.city AS customer_city
FROM
salesman s
JOIN
customer1 c
ON
s.salesman_id = c.salesman_id;

Output:

2. Write a SQL statement to find the list of customers who appointed a salesman for their jobs
who gets more than 12% commission from the company.

Query:

SELECT
c.customer_id,
c.cust_name AS customer_name,
c.city AS customer_city,
s.salesman_id,
s.name AS salesman_name,
s.city AS salesman_city,
s.commission
FROM
customer1 c
JOIN
salesman s
ON
c.salesman_id = s.salesman_id
WHERE
s.commission > 0.12;

Output:

3. Write a SQL statement to find the list of customers who appointed a salesman for their jobs
who does not live in same city where their customer lives, and gets a commission above
12%.

Query:

SELECT
c.customer_id,
c.cust_name AS customer_name,
c.city AS customer_city,
s.salesman_id,
s.name AS salesman_name,
s.city AS salesman_city,
s.commission
FROM
customer1 c
JOIN
salesman s
ON
c.salesman_id = s.salesman_id
WHERE
s.commission > 0.12
AND c.city != s.city;

Output:
4. Write a SQL statement to find the details of order i.e. order number, order date, amount of
order, which customer gives the order and which salesman works for that customer and how
much commission he gets for an order.

Query:

SELECT
o.ord_no AS order_number,
o.ord_date AS order_date,
o.purch_amt AS order_amount,
c.customer_id,
c.cust_name AS customer_name,
c.city AS customer_city,
s.salesman_id,
s.name AS salesman_name,
s.commission,
o.purch_amt * s.commission AS commission_amount
FROM
orders1 o
JOIN
customer1 c
ON
o.customer_id = c.customer_id
JOIN
salesman s
ON
o.salesman_id = s.salesman_id;

Output:

5. Write a SQL statement to make a list in ascending order for the customer who works either
through a salesman or by own.

Query:

SELECT
customer_id,
cust_name AS customer_name,
city
FROM
customer1
ORDER BY
cust_name ASC;

Output:

6. Write a SQL statement to make a list in ascending order for the customer who holds a grade
less than 300 and works either through a salesman or by own.

Query:

SELECT
customer_id,
cust_name AS customer_name,
city
FROM
customer1
WHERE
grade < 300
ORDER BY
cust_name ASC;

Output:
7. Write a SQL statement to make a report with customer name, city, order number, order date
and order amount in ascending order according to the order date to find that either any of
the existing customer have not placed any order or placed an order.

Query:

SELECT
c.cust_name AS customer_name,
c.city AS customer_city,
o.ord_no AS order_number,
o.ord_date AS order_date,
o.purch_amt AS order_amount
FROM
customer1 c
LEFT JOIN
orders1 o ON c.customer_id = o.customer_id
ORDER BY
o.ord_date ASC;

Output:
8. 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
customer have placed no order or placed an order by their salesman or by own.

Query:

SELECT
c.cust_name AS customer_name,
c.city AS customer_city,
o.ord_no AS order_number,
o.ord_date AS order_date,
o.purch_amt AS order_amount,
s.name AS salesman_name,
s.commission,
o.purch_amt * s.commission AS commission_amount
FROM
customer1 c
LEFT JOIN
orders1 o ON c.customer_id = o.customer_id
LEFT JOIN
salesman s ON o.salesman_id = s.salesman_id
ORDER BY
o.ord_date ASC;

Output:
9. Write a SQL statement to make a list in ascending order for the salesmen who works either
for one or more customer or not yet joined under any of the customer.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
s.city AS salesman_city
FROM
salesman s
LEFT JOIN
customer1 c ON s.salesman_id = c.salesman_id
ORDER BY
s.name ASC;

Output:

10. Write a SQL statement to make a list for the salesmen who works either for one or more
customer or not yet join under any of the customer who placed either an order or no order.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
s.city AS salesman_city
FROM
salesman s
LEFT JOIN
customer1 c ON s.salesman_id = c.salesman_id
LEFT JOIN
orders1 o ON c.customer_id = o.customer_id
GROUP BY
s.salesman_id, s.name, s.city;

Output:

11. Write a SQL statement to make a list for the salesmen who either work for one or more
customer or yet to join any of the customer. The customer, may have placed, either an order
of or above order amount 2000 and must have a grade, or he may not have placed any order.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
s.city AS salesman_city
FROM
salesman s
LEFT JOIN
customer1 c ON s.salesman_id = c.salesman_id
LEFT JOIN
orders1 o ON c.customer_id = o.customer_id AND o.purch_amt >= 2000
WHERE
(c.grade IS NOT NULL AND o.ord_no IS NOT NULL) OR o.ord_no IS NULL
GROUP BY
s.salesman_id, s.name, s.city;

Output:
12. Write a SQL statement to make a cartesian product between salesman and customer i.e.
each salesman will appear for all customer and vice versa.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
c.customer_id,
c.cust_name AS customer_name
FROM
salesman s
CROSS JOIN
customer1 c;

Output:
13. Write a SQL statement to make a cartesian product between salesman and customer i.e.
each salesman will appear for all customer and vice versa for those customers who belongs
to the same city.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
c.customer_id,
c.cust_name AS customer_name
FROM
salesman s
CROSS JOIN
customer1 c
WHERE
s.city = c.city;

Output:
14. Write a SQL statement to make a cartesian product between salesman and customer i.e.
each salesman will appear for all customer and vice versa for those salesmen who belongs to
the same city and the customers who must have a grade.

Query:

SELECT
s.salesman_id,
s.name AS salesman_name,
c.customer_id,
c.cust_name AS customer_name
FROM
salesman s
CROSS JOIN
customer1 c
WHERE
s.city = c.city AND c.grade IS NOT NULL;

Output:

You might also like