0% found this document useful (0 votes)
27 views2 pages

Doubts

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

Doubts

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

2nd pdf

q)For the following relation schema:


employee(employee-name, street, city)
works(employee-name, company-name, salary)
company(company-name, city)
manages(employee-name, manager-name)

1.Find the names of all employees in the database who live in the same cities and
on the
same streets as do their managers.
select p.employee-name
from employee p, employee r, manages m
where p.employee-name = m.employee-name and m.manager-name =
r.employee-name
and p.street = r.street and p.city = r.city

2.Find the highest purchase amount ordered by the each customer


with their ID and highest purchase amount.
SELECT customer_id, MAX(purch_amt)
FROM orders
GROUP BY customer_id;

3.Find the highest purchase amount with their customer ID


and order date, for only those customers who have the
highest purchase amount in a day is more than 2000.
SELECT customer_id, ord_date, MAX(purch_amt)
FROM orders
GROUP BY customer_id, ord_date
HAVING MAX(purch_amt) > 2000.00;

4.Display all those orders by the customers not located


in the same cities where their salesmen live.
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;

5.Display all the orders issued by the salesman 'Paul Adam' from the orders table.
SELECT *
FROM orders
WHERE salesman_id =
(SELECT salesman_id
FROM salesman
WHERE name = 'Paul Adam');

6.Extract the data from the orders table for the


salesman who earned the maximum commission.
SELECT ord_no, purch_amt, ord_date, salesman_id
FROM orders
WHERE salesman_id IN (
SELECT salesman_id
FROM salesman
WHERE commission = (
SELECT MAX(commission)
FROM salesman)
);

7.Find the name and ids of all salesmen who


had more than one customer.
SELECT salesman_id, name
FROM salesman AS a
WHERE 1 <
(SELECT COUNT(*)
FROM customer AS c
WHERE c.salesman_id = a.salesman_id);
salesman_id name
5001 James Hoog
5002 Nail Knite
• 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;

8.Write a query to find all the salesmen who


worked for only one customer

SELECT c.salesman_id, s.name, s.city, s.commission


FROM salesman s, customer c
where s.salesman_id = c.salesman_id
group by c.salesman_id, s.name
Having count(c.salesman_id) = 1;

SELECT *
FROM salesman
WHERE salesman_id NOT IN (
SELECT a.salesman_idFROMcustomer a, customer b
WHERE a.salesman_id=b.salesman_idAND a.cust_name <> b.cust_name);

You might also like