Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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)
7. List names of all customers matched with the salespeople serving them.
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.
11. Match salespeople to customers according to what city they lived in.
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)
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)
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.
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)
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.
51. Write a query that lists each order number followed by the name of the customer
who made that order.
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.