Sei sulla pagina 1di 11

1.

List all the columns of the Salespeople table

select *from Salespeople;


+------+---------+-----------+------+
| SNUM | SNAME | CITY | COMM |
+------+---------+-----------+------+
| 1001 | Peel | London | 0.12 |
| 1002 | Serres | San Jose | 0.13 |
| 1003 | AxelRod | New York | 0.10 |
| 1004 | Motika | London | 0.11 |
| 1005 | Fran | London | 0.26 |
| 1007 | Rifkin | Barcelona | 0.15 |
+------+---------+-----------+------+
6 rows in set (0.00 sec)

2. List all customers with a rating of 100.

mysql> select cname from Customers where rating=100;


+---------+
| cname |
+---------+
| Hoffman |
| Clemens |
| Pereira |
+---------+
3 rows in set (0.00 sec)

3. Find all records in the Customer table with NULL values in the city column.
mysql> select count(*) from Customers where CITY=NULL;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.09 sec)

4. Find the largest order taken by each salesperson on each date.


mysql> select SNUM,ODATE,max(AMT) from orders group by ODATE,SNUM;
+------+------------+----------+
| SNUM | ODATE | max(AMT) |
+------+------------+----------+
| 1001 | 1996-03-10 | 767.19 |
| 1002 | 1996-03-10 | 5160.45 |
| 1004 | 1996-03-10 | 1900.10 |
| 1007 | 1996-03-10 | 1098.16 |
| 1003 | 1996-04-10 | 1713.23 |
| 1001 | 1996-05-10 | 4723.00 |
| 1001 | 1996-06-10 | 9891.88 |
| 1002 | 1996-06-10 | 1309.95 |
+------+------------+----------+
8 rows in set (0.00 sec)

5. Arrange the Orders table by descending customer number.


mysql> select *from Orders order by CNUM desc;
+------+---------+------------+------+------+
| ONUM | AMT | ODATE | CNUM | SNUM |
+------+---------+------------+------+------+
| 3001 | 18.69 | 1996-03-10 | 2008 | 1007 |
| 3006 | 1098.16 | 1996-03-10 | 2008 | 1007 |
| 3002 | 1900.10 | 1996-03-10 | 2007 | 1004 |
| 3008 | 4723.00 | 1996-05-10 | 2006 | 1001 |
| 3011 | 9891.88 | 1996-06-10 | 2006 | 1001 |
| 3010 | 1309.95 | 1996-06-10 | 2004 | 1002 |
| 3005 | 5160.45 | 1996-03-10 | 2003 | 1002 |
| 3009 | 1713.23 | 1996-04-10 | 2002 | 1003 |
| 3007 | 75.75 | 1996-04-10 | 2002 | 1003 |
| 3003 | 767.19 | 1996-03-10 | 2001 | 1001 |
+------+---------+------------+------+------+
10 rows in set (0.00 sec)

6. Find which salespeople currently have orders in the Orders table.


mysql> select SNAME from salespeople
-> where SNUM in (select SNUM from Orders);
+---------+
| SNAME |
+---------+
| Peel |
| Serres |
| AxelRod |
| Motika |
| Rifkin |
+---------+
5 rows in set (0.02 sec)

7. List names of all customers matched with the salespeople serving them.

mysql> select CNAME from CUstomers


-> where SNUM in(select SNUM from Salespeople);
+----------+
| CNAME |
+----------+
| Hoffman |
| Giovanni |
| Liu |
| Grass |
| Clemens |
| Pereira |
| Cisneros |
+----------+
7 rows in set (0.00 sec)

8. Find the names and numbers of all salespeople who had more than one customer.

9. Count the orders of each of the salespeople and output the results in descending
order.
mysql> select count(ONUM),
-> SNUM from Orders
-> group by
-> SNUM
-> order by
-> count(onum) desc;
+-------------+------+
| count(ONUM) | SNUM |
+-------------+------+
| 3 | 1001 |
| 2 | 1003 |
| 2 | 1007 |
| 2 | 1002 |
| 1 | 1004 |
+-------------+------+
5 rows in set (0.00 sec)

10. List the Customer table if and only if one or more of the customers in the
Customer table are
located in San Jose.

mysql> select *from Customers where city='San Jose';


+------+----------+----------+--------+------+
| CNUM | CNAME | CITY | RATING | SNUM |
+------+----------+----------+--------+------+
| 2003 | Liu | San Jose | 200 | 1002 |
| 2008 | Cisneros | San Jose | 300 | 1007 |
+------+----------+----------+--------+------+
2 rows in set (0.00 sec)

11. Match salespeople to customers according to what city they lived in.

select SNUM,max(AMT) from Orders group by SNUM;


+------+----------+
| SNUM | max(AMT) |
+------+----------+
| 1001 | 9891.88 |
| 1002 | 5160.45 |
| 1003 | 1713.23 |
| 1004 | 1900.10 |
| 1007 | 1098.16 |
+------+----------+
5 rows in set (0.00 sec)

12. Find the largest order taken by each salesperson


mysql> select SNAME,o.SNUM,max(AMT) from Orders o join Salespeople s on
o.SNUM=s.SNUM group by s.SNAME;
+---------+------+----------+
| SNAME | SNUM | max(AMT) |
+---------+------+----------+
| AxelRod | 1003 | 1713.23 |
| Motika | 1004 | 1900.10 |
| Peel | 1001 | 9891.88 |
| Rifkin | 1007 | 1098.16 |
| Serres | 1002 | 5160.45 |
+---------+------+----------+
5 rows in set (0.00 sec)

13. Find customers in San Jose who have a rating above 200.
mysql> select *from Customers where CITY='San Jose'and rating>200;
+------+----------+----------+--------+------+
| CNUM | CNAME | CITY | RATING | SNUM |
+------+----------+----------+--------+------+
| 2008 | Cisneros | San Jose | 300 | 1007 |
+------+----------+----------+--------+------+
1 row in set (0.00 sec)

14. List the names and commissions of all salespeople in London.


mysql> select SNAME,COMM from Salespeople where CITY='London';
+--------+------+
| SNAME | COMM |
+--------+------+
| Peel | 0.12 |
| Motika | 0.11 |
| Fran | 0.26 |
+--------+------+
3 rows in set (0.00 sec)

15. List all the orders of salesperson Motika from the Orders table.
mysql> select ONUM from Orders where SNUM=(select SNUM from Salespeople where
SNAME='Motika');
+------+
| ONUM |
+------+
| 3002 |
+------+
1 row in set (0.02 sec)

16. Find all customers with orders on October 3.


mysql> select *from Customers where CNUM in(select CNUM from Orders where
odate='1996-03-10');
+------+----------+----------+--------+------+
| CNUM | CNAME | CITY | RATING | SNUM |
+------+----------+----------+--------+------+
| 2001 | Hoffman | London | 100 | 1001 |
| 2003 | Liu | San Jose | 200 | 1002 |
| 2007 | Pereira | Rome | 100 | 1004 |
| 2008 | Cisneros | San Jose | 300 | 1007 |
+------+----------+----------+--------+------+
4 rows in set (0.00 sec)

17. Give the sums of the amounts from the Orders table, grouped by date,
eliminating all those
dates where the SUM was not at least 2000.00 above the MAX amount.

18. Select all orders that had amounts that were greater than at least one of the
orders from
October 6.
mysql> select *from Orders where AMT>any(select amt from Orders where odate='1996-
06-10');
+------+---------+------------+------+------+
| ONUM | AMT | ODATE | CNUM | SNUM |
+------+---------+------------+------+------+
| 3002 | 1900.10 | 1996-03-10 | 2007 | 1004 |
| 3005 | 5160.45 | 1996-03-10 | 2003 | 1002 |
| 3008 | 4723.00 | 1996-05-10 | 2006 | 1001 |
| 3009 | 1713.23 | 1996-04-10 | 2002 | 1003 |
| 3011 | 9891.88 | 1996-06-10 | 2006 | 1001 |
+------+---------+------------+------+------+
5 rows in set (0.00 sec)

19. Write a query that uses the EXISTS operator to extract all salespeople who have
customers
with a rating of 300.

20. Find all pairs of customers having the same rating.


mysql> select *from Customers order by rating;
+------+----------+----------+--------+------+
| CNUM | CNAME | CITY | RATING | SNUM |
+------+----------+----------+--------+------+
| 2001 | Hoffman | London | 100 | 1001 |
| 2006 | Clemens | London | 100 | 1001 |
| 2007 | Pereira | Rome | 100 | 1004 |
| 2002 | Giovanni | Rome | 200 | 1003 |
| 2003 | Liu | San Jose | 200 | 1002 |
| 2004 | Grass | Berlin | 300 | 1002 |
| 2008 | Cisneros | San Jose | 300 | 1007 |
+------+----------+----------+--------+------+
7 rows in set (0.00 sec)

22. Give the salespeople�s commissions as percentages instead of decimal numbers.


mysql> select comm*100 from Salespeople;
+----------+
| comm*100 |
+----------+
| 12.00 |
| 13.00 |
| 10.00 |
| 11.00 |
| 26.00 |
| 15.00 |
+----------+
6 rows in set (0.07 sec)

26. Select all customers with a rating above 200.00.


mysql> select *from Customers where rating > 200;
+------+----------+----------+--------+------+
| CNUM | CNAME | CITY | RATING | SNUM |
+------+----------+----------+--------+------+
| 2004 | Grass | Berlin | 300 | 1002 |
| 2008 | Cisneros | San Jose | 300 | 1007 |
+------+----------+----------+--------+------+
2 rows in set (0.00 sec)

27. Count the number of salespeople currently listing orders in the Orders table.

31. Find all salespeople whose name starts with �P� and the fourth character is
�l�.
mysql> select SNAME from Salespeople where SNAME like 'p__l%';
+-------+
| SNAME |
+-------+
| Peel |
+-------+
1 row in set (0.00 sec)

32. Write a query that uses a subquery to obtain all orders for the customer named
Cisneros. Assume you do not know his customer number.
mysql> select ONUM from Orders
-> where SNUM=(select SNUM from customers where CNAME='cisneros');
+------+
| ONUM |
+------+
| 3001 |
| 3006 |
+------+
2 rows in set (0.00 sec)

33. Find the largest orders for Serres and Rifkin.


mysql> select SNUM,max(AmT) from Orders group by SNUM
-> having snum in(select snum from salespeople where sname='Serres' or
sname='Rifkin');
+------+----------+
| SNUM | max(AmT) |
+------+----------+
| 1002 | 5160.45 |
| 1007 | 1098.16 |
+------+----------+
2 rows in set (0.10 sec)

34. Extract the Salespeople table in the following order : SNUM, SNAME, COMMISSION,
CITY.
mysql> select SNUM,SNAME,COMM,CITY from Salespeople;
+------+---------+------+-----------+
| SNUM | SNAME | COMM | CITY |
+------+---------+------+-----------+
| 1001 | Peel | 0.12 | London |
| 1002 | Serres | 0.13 | San Jose |
| 1003 | AxelRod | 0.10 | New York |
| 1004 | Motika | 0.11 | London |
| 1005 | Fran | 0.26 | London |
| 1007 | Rifkin | 0.15 | Barcelona |
+------+---------+------+-----------+
6 rows in set (0.00 sec)

35. Select all customers whose names fall in between �A� and �G� alphabetical
range.
mysql> select CNAME from Customers where CNAME between 'A' and 'G';
+----------+
| CNAME |
+----------+
| Clemens |
| Cisneros |
+----------+
2 rows in set (0.00 sec)

36. Select all the possible combinations of customers that you can assign
mysql> select c.CNAME,s.SNAME from Customers c join Salespeople s on c.SNUM=s.SNUM;
+----------+---------+
| CNAME | SNAME |
+----------+---------+
| Hoffman | Peel |
| Clemens | Peel |
| Liu | Serres |
| Grass | Serres |
| Giovanni | AxelRod |
| Pereira | Motika |
| Cisneros | Rifkin |
+----------+---------+
7 rows in set (0.00 sec)

37. Select all orders that are greater than the average for October 4.
mysql> select CNUM,CNAME,RATING from CUstomers where rating in(select max(rating)
from Customers group by CITY);
+------+----------+--------+
| CNUM | CNAME | RATING |
+------+----------+--------+
| 2001 | Hoffman | 100 |
| 2002 | Giovanni | 200 |
| 2003 | Liu | 200 |
| 2004 | Grass | 300 |
| 2006 | Clemens | 100 |
| 2007 | Pereira | 100 |
| 2008 | Cisneros | 300 |
+------+----------+--------+
7 rows in set (0.08 sec)

39. Write a query that totals the orders for each day and places the results in
descending order.
mysql> select odate,count(onum)from orders group by odate order by count(onum)
desc;
+------------+-------------+
| odate | count(onum) |
+------------+-------------+
| 1996-03-10 | 5 |
| 1996-04-10 | 2 |
| 1996-06-10 | 2 |
| 1996-05-10 | 1 |
+------------+-------------+
4 rows in set (0.00 sec)

40. Write a select command that produces the rating followed by the name of each
customer in San Jose.
mysql> select rating,cname from customers where city='San Jose';
+--------+----------+
| rating | cname |
+--------+----------+
| 200 | Liu |
| 300 | Cisneros |
+--------+----------+
2 rows in set (0.07 sec)

41. Find all orders with amounts smaller than any amount for a customer in San
Jose.
mysql> select onum from orders where amt<any(select amt from orders o,customers c
where c.cnum=o.cnum and c.city='san jose');
+------+
| onum |
+------+
| 3001 |
| 3002 |
| 3003 |
| 3006 |
| 3007 |
| 3008 |
| 3009 |
| 3010 |
+------+
8 rows in set (0.00 sec)

42. Find all orders with above average amounts for their customers.
mysql> select onum,cnum,amt from orders a where amt> (select avg(amt) from orders b
where a.cnum=b.cnum group by cnum);
+------+------+---------+
| onum | cnum | amt |
+------+------+---------+
| 3006 | 2008 | 1098.16 |
| 3009 | 2002 | 1713.23 |
| 3011 | 2006 | 9891.88 |
+------+------+---------+
3 rows in set (0.00 sec)

43. Write a query that selects the highest rating in each city.
mysql> select city,max(c.rating)from customers c group by city;
+----------+---------------+
| city | max(c.rating) |
+----------+---------------+
| Berlin | 300 |
| London | 100 |
| Rome | 200 |
| San Jose | 300 |
+----------+---------------+
4 rows in set (0.00 sec)

44. Write a query that calculates the amount of the salesperson�s commission on
each order by a customer with a rating above 100.00.
mysql> select sname,amt*comm from orders,customers,salespeople where rating > 100
and salespeople.snum=customers.snum and salespeople.snum=orders.snum and
customers.cnum=orders.cnum;
+---------+----------+
| sname | amt*comm |
+---------+----------+
| Serres | 670.8585 |
| Serres | 170.2935 |
| AxelRod | 7.5750 |
| AxelRod | 171.3230 |
| Rifkin | 2.8035 |
| Rifkin | 164.7240 |
+---------+----------+
6 rows in set (0.00 sec)

45. Count the customers with ratings above San Jose�s average.
mysql> select count(city) from customers where rating > (select avg(rating) from
customers where city='san jose');
+-------------+
| count(city) |
+-------------+
| 2 |
+-------------+
1 row in set (0.00 sec)

46. Write a query that produces all pairs of salespeople with themselves as well
as duplicate rows with the order reversed.

47.Find all salespeople that are located in either Barcelona or London.


mysql> select sname from salespeople where city='barcelona' or city='london';
+--------+
| sname |
+--------+
| Peel |
| Motika |
| Fran |
| Rifkin |
+--------+
4 rows in set (0.00 sec)

48.Find all salespeople with only one customer.


mysql> select snum,count(cnum) from customers group by snum having count(cnum)=1;
+------+-------------+
| snum | count(cnum) |
+------+-------------+
| 1003 | 1 |
| 1004 | 1 |
| 1007 | 1 |
+------+-------------+
3 rows in set (0.00 sec)
49. Write a query that joins the Customer table to itself to find all pairs of
customers served by a single salesperson.
50. Write a query that will give you all orders for more than $1000.00
mysql> select onum from orders where amt>1000;
+------+
| onum |
+------select
| 3002 |
| 3005 |
| 3006 |
| 3008 |
| 3009 |
| 3010 |
| 3011 |
+------+
7 rows in set (0.00 sec)

51. Write a query that lists each order number followed by the name of the customer
who made that order.

mysql> select orders.onum,customers.cname from customers,orders


-> where customers.cnum=orders.cnum;
+------+----------+
| onum | cname |
+------+----------+
| 3003 | Hoffman |
| 3007 | Giovanni |
| 3009 | Giovanni |
| 3005 | Liu |
| 3010 | Grass |
| 3008 | Clemens |
| 3011 | Clemens |
| 3002 | Pereira |
| 3001 | Cisneros |
| 3006 | Cisneros |
+------+----------+
10 rows in set (0.00 sec)

52. Write 2 queries that select all salespeople (by name and number) who have
customers in their cities who they do not service, one using a join and one a
corelated subquery. Which solution is more elegant?
mysql> select c.snum,s.snum from salespeople s,customers c where s.city=c.city
group by s.snum,c.snum having c.snum!=s.snum;
+------+------+
| snum | snum |
+------+------+
| 1007 | 1002 |
| 1001 | 1004 |
| 1001 | 1005 |
+------+------+
3 rows in set (0.00 sec)

53. Write a query that selects all customers whose ratings are equal to or greater
than ANY (in the SQL sense) of Serres�?
mysql> select cname from customers where rating >= any (select rating from
customers where snum =(select snum from salespeople where sname='Serres'));
+----------+
| cname |
+----------+
| Giovanni |
| Liu |
| Grass |
| Cisneros |
+----------+
4 rows in set (0.00 sec)

54. Write 2 queries that will produce all orders taken on October 3 or October 4.
mysql> select onum from orders where odate='1996-03-10' or odate='1996-04-10';
+------+
| onum |
+------+
| 3001 |
| 3002 |
| 3003 |
| 3005 |
| 3006 |
| 3007 |
| 3009 |
+------+
7 rows in set (0.00 sec)

55. Write a query that produces all pairs of orders by a given customer. Name that
customer and eliminate duplicates.
mysql> select c.cname,o.onum from customers c,orders o where c.cnum=o.cnum group by
c.cname,o.onum;
+----------+------+
| cname | onum |
+----------+------+
| Cisneros | 3001 |
| Cisneros | 3006 |
| Clemens | 3008 |
| Clemens | 3011 |
| Giovanni | 3007 |
| Giovanni | 3009 |
| Grass | 3010 |
| Hoffman | 3003 |
| Liu | 3005 |
| Pereira | 3002 |
+----------+------+
10 rows in set (0.00 sec)

56. Find only those customers whose ratings are higher than every customer in Rome.
mysql> select cname from customers where rating > (select max(rating) from
customers where city='rome');
+----------+
| cname |
+----------+
| Grass |
| Cisneros |
+----------+
2 rows in set (0.00 sec)

57. Write a query on the Customers table whose output will exclude all customers
with a rating <= 100.00, unless they are located in Rome.
mysql> select cname from customers where rating <100 or city='rome';
+----------+
| cname |
+----------+
| Giovanni |
| Pereira |
+----------+
2 rows in set (0.00 sec)

58. Find all rows from the Customers table for which the salesperson number is
1001.
mysql> select *from customers where snum=1001;
+------+---------+--------+--------+------+
| CNUM | CNAME | CITY | RATING | SNUM |
+------+---------+--------+--------+------+
| 2001 | Hoffman | London | 100 | 1001 |
| 2006 | Clemens | London | 100 | 1001 |
+------+---------+--------+--------+------+
2 rows in set (0.00 sec)

59.Find the total amount in Orders for each salesperson for whom this total is g
reater than the amount of the largest order in the table.
mysql> select snum,sum(amt) from orders group by snum having sum(amt) > (select
max(amt) from orders);
+------+----------+
| snum | sum(amt) |
+------+----------+
| 1001 | 15382.07 |
+------+----------+
1 row in set (0.00 sec)

60. Write a query that selects all orders save those with zeroes or NULLs in the
amount field.
mysql> select onum from orders where amt is NULL or amt=0;
Empty set (0.00 sec)

61. 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.

Potrebbero piacerti anche