Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sql assignment #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Sql assignment #5

wants to merge 2 commits into from

Conversation

Naman1109
Copy link
Owner

SQL Assignment Added


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grouping should be done on the OrderDate. This is a wrong query.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please display only distinct sales person name.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query

+------+-------+----------+------+------+
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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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' ;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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 ;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query


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 ;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a wrong query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants