Conversation
|
|
||
| 3) Find the largest order taken by each salesperson on each date. | ||
|
|
||
| Ans :- mysql> select salespeople.snum, salespeople.sname, max(amt) as MAX_ORDER from orders inner join salespeople on orders.snum=salespeople.snum group by snum; |
There was a problem hiding this comment.
Grouping should be done on the OrderDate. This is a wrong query.
There was a problem hiding this comment.
mysql> select salespeople.snum, salespeople.sname, orders.odate, max(amt) as MAX_ORDER from orders inner join salespeople on orders.snum=salespeople.snum group by odate;
+------+---------+----------+-----------+
| snum | sname | odate | MAX_ORDER |
+------+---------+----------+-----------+
| 1007 | rifkin | 10/03/90 | 767.19 |
| 1003 | AlexRod | 10/04/90 | 75.75 |
| 1001 | peel | 10/05/90 | 4723.00 |
| 1002 | seeres | 10/06/90 | 9891.88 |
+------+---------+----------+-----------+
|
|
||
| 5) Find which salespeople currently have orders in the order table. | ||
|
|
||
| Ans :- mysql> select salespeople.sname from salespeople join orders on (salespeople.snum = orders.snum); |
There was a problem hiding this comment.
Please display only distinct sales person name.
There was a problem hiding this comment.
mysql> select salespeople.sname from salespeople join orders on (salespeople.snum = orders.snum) group by sname;
+---------+
| sname |
+---------+
| AlexRod |
| motika |
| peel |
| rifkin |
| seeres |
+---------+
|
|
||
| 10) Match salespeople to customers according to what city they live in. | ||
|
|
||
| Ans :-mysql> select salespeople.sname,customers.city,customers.cname from salespeople join customers on (customers.city = salespeople.city ); |
|
|
||
| 16) Select all orders that had amounts that were greater than at least one of the orders from October 6. | ||
|
|
||
| Ans :- mysql> select * from orders where amt > (select min(amt) from orders where odate='10/06/90'); |
|
|
||
| 20) Find the largest order taken by each salesperson on each date, eliminating those Maximum orders, which are less than 3000. | ||
|
|
||
| Ans :- mysql> select sp.SNAME,od.AMT,od.ODate from salespeople as sp,orders AS od WHERE sp.SNUM = od.SNUM AND od.AMT > 1000 group by ODATE; |
There was a problem hiding this comment.
The conditional check will be for 3000 instead of 1000.
|
|
||
| 27) Find salespeople with customers located in their own cities. | ||
|
|
||
| Ans :-mysql> select sp.SNAME,cust.CNAME,cust.CITY from salespeople as sp Inner join customers as cust on sp.CITY=cust.CITY order by cust.CITY; |
| +------+-------+----------+------+------+ | ||
| 1 row in set (0.00 sec) | ||
|
|
||
| Ans :- mysql> select sp.SNAME,max(od.AMT) as LargestOrder from orders as od inner join salespeople as sp on sp.SNAME in ('Serres','Rifkin' ) And od.SNUM=sp.SNUM group by sp.SNUM; |
There was a problem hiding this comment.
The largest order taken by Rifkin is 1098.16, why your result is showing 10000?
|
|
||
| 38) Find all orders with amounts smaller than any amount for a customer in SanJose. | ||
|
|
||
| Ans :-mysql> select od.* from orders as od left join customers as cust on cust.city='sanjose' and cust.CNUM=od.CNUM; |
|
|
||
| 39) Find all orders with above average amounts for their customers. | ||
|
|
||
| Ans :- mysql> select * from orders natural join customers where AMT > (select AVG(AMT) from orders); |
|
|
||
| 49) Write two queries that will produce all orders taken on October 3 or October 4. | ||
|
|
||
| Ans :- mysql> select * from orders where ODATE = '10,03,90' or odate = '10,04,90' ; |
|
|
||
| 55) Produce all combinations of salespeople and customer names such that the former precedes the latter alphabetically, and the latter has a rating of less than 200. | ||
|
|
||
| Ans :- mysql> SELECT Customers.cname, salespeople.sname , customers.rating FROM Customers CROSS JOIN salespeople WHERE customers.rating<200 order by Customers.cname, salespeople.sname; |
|
|
||
| 64) List the commissions of all salespeople servicing customers in London. | ||
|
|
||
| Ans :- mysql> select salespeople.comm,salespeople.sname , customers.cname, customers.city from salespeople inner join customers where customers.city = 'london'; |
|
|
||
| 67) Write a query that selects all customers serviced by Peel or Motika. (Hint: The snum field relates the 2 tables to one another.) | ||
|
|
||
| Ans :-mysql> select customers.cname, salespeople.sname from customers inner join salespeople on salespeople.sname='motika' or salespeople.sname='peel' order by salespeople.sname ; |
|
|
||
| 71) Find all salespeople who have customers with more than one current order. | ||
|
|
||
| Ans :- mysql> select sp.sname,count(cust.cnum) from salespeople sp Inner join customers cust on sp.snum=cust.cnum group by cust.snum having count(cust.snum)>1; |
|
|
||
| 72) Write a query that extracts from the customer’s table every customer assigned to a salesperson, who is currently having at least one another customer(besides the customer being selected) with orders in the Orders Table. | ||
|
|
||
| Ans:- mysql> select (select sname from salespeople where snum=cust.snum ) as salespeoplesHavingmoreCustomers from customers as cust group by snum having count(snum)>1; |
|
|
||
| 80) Write a query that selects each customer’s smallest order. | ||
|
|
||
| Ans:-mysql> select ord.*,min(ord.AMT) as SmallestOrderOfCustommer from customers as cust natural join orders as ord group by cust.CName ; |
There was a problem hiding this comment.
Please display the name of the customer as well in the query.
|
|
||
| 104) Select all salespeople by name and number who have customers in their city whom they don’t service. | ||
|
|
||
| Ans :- mysql> select sname, snum from salespeople where snum in ( select snum from customers group by snum having count(snum)>1); |
SQL Assignment Added