You are on page 1of 15

Lab Cycle -2

1 List all customers ordered by country, then by city within each country

SQL> select firstname, lastname, country, city


2 from customer
3 order by country, city;

FIRSTNAME LASTNAME COUNTRY CITY


--------------- --------------- --------------- ---------------
Patricio Simpson Argentina Buenos Aires
Roland Mendel Austria Graz
Catherine Dewey Belgium Bruxelles
Andr� Fonseca Brazil Campinas
Mario Pontes Brazil Rio de Janeiro
Pedro Afonso Brazil Sao Paulo
Aria Cruz Brazil Sao Paulo
Elizabeth Lincoln Canada Tsawassen
Yoshi Tannamuri Canada Vancouver
Martine Ranc� France Lille
Laurence Lebihan France Marseille
Janine Labrune France Nantes
Carine Schmitt France Nantes
Fr�d�rique Citeaux France Strasbourg
Annette Roulet France Toulouse
Daniel Tonini France Versailles
Sven Ottlieb Germany Aachen
Maria Anders Germany Berlin
Philip Cramer Germany Brandenburg
Renate Messner Germany Frankfurt a.M.
Hanna Moos Germany Mannheim
Peter Franken Germany M�nchen
Patricia McKenna Ireland Cork
Giovanni Rovelli Italy Bergamo
Paolo Accorti Italy Torino
Ana Trujillo Mexico M�xico D.F.
Francisco Chang Mexico M�xico D.F.
Antonio Moreno Mexico M�xico D.F.
Lino Rodriguez Portugal Lisboa
Eduardo Saavedra Spain Barcelona
Mart�n Sommer Spain Madrid
Diego Roel Spain Madrid
Jos� Pedro Freyre Spain Sevilla
Maria Larsson Sweden Br�cke
Christina Berglund Sweden Lule�
Yang Wang Switzerland Bern
Helen Bennett UK Cowes
Thomas Hardy UK London
Elizabeth Brown UK London
Ann Devon UK London
Victoria Ashworth UK London
Yoshi Latimer USA Elgin
Howard Snyder USA Eugene
Fran Wilson USA Portland
Jaime Yorres USA San Francisco
John Steel USA Walla Walla
Carlos Gonz�lez Venezuela Barquisimeto
Manuel Pereira Venezuela Caracas
Felipe Izquierdo Venezuela I. de Margarita
Carlos Hern�ndez Venezuela San Crist�bal

50 rows selected.

2 List all suppliers in the USA, Japan, and Germany, ordered by city,�then by
company name in reverse order

SQL> select * from supplier


2 where country in ('USA', 'Japan', 'Germany')
3 order by city, companyname desc;

ID COMPANYNAME CONTACTNAME CITY COUNTRY


PHONE FAX
---------- ------------------------- -------------------- ---------------
--------------- ----------
3 Grandma Kelly's Homestead Regina Murphy Ann Arbor USA
(313) 555-5735 (313) 555-3349
16 Bigfoot Breweries Cheryl Saylor Bend USA
(503) 555-9931
19 England Seafood Cannery Robb Merchant Boston USA
(617) 555-3267 (617) 555-3389
6 Mayumi's Mayumi Ohno Osaka Japan
(06) 431-7877
4 Tokyo Traders Yoshi Nagase Tokyo Japan
(03) 3555-5011

3 Show all orders, sorted by total amount, the largest first, within each year�

SQL> select * from orders


2 order by to_char(orderdate,'yyyy'), totalamount desc;

ID ORDERDATE ORDERNUMBE CUSTOMERID TOTALAMOUNT


---------- --------- ---------- ---------- -----------
20 29-JUL-12 542397 25 4031
95 30-OCT-12 542472 25 2300.8
43 27-AUG-12 542420 15 2169
121 29-NOV-12 542498 20 1834.2
62 19-SEP-12 542439 37 1762
133 12-DEC-12 542510 37 1419.8
49 03-SEP-12 542426 46 1050.6
117 26-NOV-12 542494 19 950
7 11-JUL-12 542384 14 625.2
71 01-OCT-12 542448 38 240.4
131 10-DEC-12 542508 24 103.2

ID ORDERDATE ORDERNUMBE CUSTOMERID TOTALAMOUNT


---------- --------- ---------- ---------- -----------
440 30-SEP-13 542817 37 6201.9
425 17-SEP-13 542802 5 4210.5
451 09-OCT-13 542828 20 3600.73
243 31-MAR-13 542620 35 3163.2
153 01-JAN-13 542530 19 3063
269 24-APR-13 542646 37 2614.5
346 09-JUL-13 542723 44 2493
289 14-MAY-13 542666 44 2085
304 28-MAY-13 542681 28 1836
223 11-MAR-13 542600 9 1820.8
325 18-JUN-13 542702 5 1565.65

ID ORDERDATE ORDERNUMBE CUSTOMERID TOTALAMOUNT


---------- --------- ---------- ---------- -----------
521 08-DEC-13 542898 4 1477
224 11-MAR-13 542601 11 1328
197 12-FEB-13 542574 5 1031.7
374 05-AUG-13 542751 38 758.5
533 16-DEC-13 542910 46 720
479 03-NOV-13 542856 19 655
442 01-OCT-13 542819 5 630
253 09-APR-13 542630 41 550.8
381 12-AUG-13 542758 7 450
362 24-JUL-13 542739 18 424
179 27-JAN-13 542556 29 338.2

ID ORDERDATE ORDERNUMBE CUSTOMERID TOTALAMOUNT


---------- --------- ---------- ---------- -----------
332 25-JUN-13 542709 45 317.75
478 31-OCT-13 542855 21 287.8
494 14-NOV-13 542871 4 285
198 13-FEB-13 542575 5 174.9
415 09-SEP-13 542792 48 125
373 05-AUG-13 542750 42 57.5
570 06-JAN-14 542947 39 11490.7
569 06-JAN-14 542946 32 8891
825 05-MAY-14 543202 20 5218
761 08-APR-14 543138 20 4903.5
732 26-MAR-14 543109 20 4813.5

ID ORDERDATE ORDERNUMBE CUSTOMERID TOTALAMOUNT


---------- --------- ---------- ---------- -----------
624 05-FEB-14 543001 9 2083.4
823 05-MAY-14 543200 44 1873.5
776 14-APR-14 543153 11 1500
829 06-MAY-14 543206 9 1057
824 05-MAY-14 543201 46 510
627 06-FEB-14 543004 30 310
593 19-JAN-14 542970 47 264
708 17-MAR-14 543085 24 93

52 rows selected.

4 List top 10 most expensive products

SQL> select id, unitprice


2 from product p
3 where(
4 select count(*)
5 from product
6 where unitprice>p.unitprice)<10

ID UNITPRICE
---------- ----------
8 40
9 97
17 39
18 62.5
20 81
43 46
51 53
56 38
59 55
63 43.9

10 rows selected.

5 Get all but the 10 most expensive products sorted by price

SQL> select id, unitprice


2 from product p
3 where (
4 select count(*)
5 from product
6 where unitprice>p.unitprice)>=10;

ID UNITPRICE
---------- ----------
1 18
2 19
3 10
6 25
7 30
10 31
13 6
14 23.25
15 15.5
16 17.45
19 9.2
21 10
22 21
23 9
24 4.5
31 12.5
32 32
33 2.5
34 14
35 18
36 19
37 26
40 18.4
41 9.65
42 14
44 19.45
45 9.5
46 12
47 9.5
48 12.75
49 20
50 16.25
52 7
53 32.8
54 7.45
55 24
57 19.5
58 13.25
60 34
67 14
68 12.5
69 36
70 15
71 21.5
72 34.8
73 15
74 10
76 18
78 9.75

49 rows selected.

6 Find the cheapest product

SQL> select * from product


2 where unitprice=(select min(unitprice) from product);

ID PRODUCTNAME SUPPLIERID UNITPRICE PACKAGE


ISDISCONTINUED
---------- ------------------------------ ---------- ----------
------------------------------ -----
33 Geitost 15 2.5 500 g
0

7 Find the largest order placed in 2014

SQL> select orderid, quantity


2 from orderitem
3 where quantity=(
4 select max(quantity)
5 from orders, orderitem
6 where orders.id=orderid and to_char(orderdate,'yyyy')=2014)

ORDERID QUANTITY
---------- ----------
761 90

8 Find the last order date in 2013

SQL> select max(orderdate) Lastorder


2 from orders, orderitem
3 where orders.id=orderid and to_char(orderdate, 'yyyy')=2013

LASTORDER
---------
16-DEC-13

9 List all customers that are not from the USA

SQL> select * from customer


2 where country not in ('USA');

ID FIRSTNAME LASTNAME CITY COUNTRY PHONE


---------- --------------- --------------- --------------- ---------------
---------------
1 Maria Anders Berlin Germany 030-
0074321
2 Ana Trujillo M�xico D.F. Mexico (5) 555-
4729
3 Antonio Moreno M�xico D.F. Mexico (5) 555-
3932
4 Thomas Hardy London UK (171)
555-7788
5 Christina Berglund Lule� Sweden 0921-12
34 65
6 Hanna Moos Mannheim Germany 0621-
08460
7 Fr�d�rique Citeaux Strasbourg France
88.60.15.31
8 Mart�n Sommer Madrid Spain (91) 555
22 82
9 Laurence Lebihan Marseille France
91.24.45.40
10 Elizabeth Lincoln Tsawassen Canada (604)
555-4729
11 Victoria Ashworth London UK (171)
555-1212
12 Patricio Simpson Buenos Aires Argentina (1) 135-
5555
13 Francisco Chang M�xico D.F. Mexico (5) 555-
3392
14 Yang Wang Bern Switzerland 0452-
076545
15 Pedro Afonso Sao Paulo Brazil (11)
555-7647
16 Elizabeth Brown London UK (171)
555-2282
17 Sven Ottlieb Aachen Germany 0241-
039123
18 Janine Labrune Nantes France
40.67.88.88
19 Ann Devon London UK (171)
555-0297
20 Roland Mendel Graz Austria 7675-
3425
21 Aria Cruz Sao Paulo Brazil (11)
555-9857
22 Diego Roel Madrid Spain (91) 555
94 44
23 Martine Ranc� Lille France
20.16.10.16
24 Maria Larsson Br�cke Sweden 0695-34
67 21
25 Peter Franken M�nchen Germany 089-
0877310
26 Carine Schmitt Nantes France
40.32.21.21
27 Paolo Accorti Torino Italy 011-
4988260
28 Lino Rodriguez Lisboa Portugal (1) 354-
2534
29 Eduardo Saavedra Barcelona Spain (93) 203
4560
30 Jos� Pedro Freyre Sevilla Spain (95) 555
82 82
31 Andr� Fonseca Campinas Brazil (11)
555-9482
33 Manuel Pereira Caracas Venezuela (2) 283-
2951
34 Mario Pontes Rio de Janeiro Brazil (21)
555-0091
35 Carlos Hern�ndez San Crist�bal Venezuela (5) 555-
1340
37 Patricia McKenna Cork Ireland 2967 542
38 Helen Bennett Cowes UK (198)
555-8888
39 Philip Cramer Brandenburg Germany 0555-
09876
40 Daniel Tonini Versailles France
30.59.84.10
41 Annette Roulet Toulouse France
61.77.61.10
42 Yoshi Tannamuri Vancouver Canada (604)
555-3392
44 Renate Messner Frankfurt a.M. Germany 069-
0245984
46 Carlos Gonz�lez Barquisimeto Venezuela (9) 331-
6954
47 Felipe Izquierdo I. de Margarita Venezuela (8) 34-
56-12
49 Giovanni Rovelli Bergamo Italy 035-
640230
50 Catherine Dewey Bruxelles Belgium (02) 201
24 67

45 rows selected.

10 List all customers that are from�the same countries as the suppliers.

SQL> select c.firstname, c.lastname, c.country, c.city from customer c, orders o,


orderitem oi, prod
2 where c.id=o.customerid and o.id=oi.orderid and p.id=oi.productid and
s.id=p.supplierid and c.c

FIRSTNAME LASTNAME COUNTRY CITY


--------------- --------------- --------------- ---------------
Helen Bennett UK Cowes
Thomas Hardy UK London

11 List all products with names that start with 'Ca'

SQL> select * from product


2 where productname like 'Ca%';

ID PRODUCTNAME SUPPLIERID UNITPRICE PACKAGE


ISDISCONTINUED
---------- ------------------------------ ---------- ----------
------------------------------ -----
18 Carnarvon Tigers 7 62.5 16 kg pkg.
0
60 Camembert Pierrot 28 34 15 - 300 g rounds
0
12 List all products that start with 'Cha' or 'Chan' and have one more
character.

SQL> select * from product


2 where productname like 'Cha_%' or productname like 'Chan_%';

ID PRODUCTNAME SUPPLIERID UNITPRICE PACKAGE


ISDISCONTINUED
---------- ------------------------------ ---------- ----------
------------------------------ -----
1 Chai 1 18 10 boxes x 20 bags
0
2 Chang 1 19 24 - 12 oz bottles
0

13 List all suppliers that have no fax number

SQL> select * from supplier


2 where fax is null;

ID COMPANYNAME CONTACTNAME CITY COUNTRY


PHONE FAX
---------- ------------------------- -------------------- ---------------
--------------- ----------
1 Exotic Liquids Charlotte Cooper London UK
(171) 555-2222
4 Tokyo Traders Yoshi Nagase Tokyo Japan
(03) 3555-5011
6 Mayumi's Mayumi Ohno Osaka Japan
(06) 431-7877
8 Specialty Biscuits, Ltd. Peter Wilson Manchester UK
(161) 555-4448
10 Refrescos Americanas LTDA Carlos Diaz Sao Paulo Brazil
(11) 555 4640
15 Norske Meierier Beate Vileid Sandvika Norway
(0)2-953010
16 Bigfoot Breweries Cheryl Saylor Bend USA
(503) 555-9931
17 Svensk Sj�f�da AB Michael Bj�rn Stockholm Sweden
08-123 45 67
20 Leka Trading Chandra Leka Singapore Singapore
555-8787
23 Karkki Oy Anne Heikkonen Lappeenranta Finland
(953) 10956
25 Ma Maison Jean-Guy Lauzon Montr�al Canada
(514) 555-9022
27 Escargots Nouveaux Marie Delamare Montceau France
85.57.00.07

12 rows selected.

14 List the total amount ordered for each customer

SQL> select customerid, sum(totalamount) totalamount


2 from orders
3 group by customerid;

CUSTOMERID TOTALAMOUNT
---------- -----------
4 1762
5 7612.75
7 450
9 4961.2
11 2828
14 625.2
15 2169
18 424
19 4668
20 20369.93
21 287.8
24 196.2
25 6331.8
28 1836
29 338.2
30 310
32 8891
35 3163.2
37 11998.2
38 998.9
39 11490.7
41 550.8
42 57.5
44 6451.5
45 317.75
46 2280.6
47 264
48 125

28 rows selected.

15 List the number of customers in each country, except the USA, sorted high to
low. Only include countries with 3 or more customers.

SQL> select count(*), country


2 from customer
3 where country <>'USA'
4 group by country
5 having count(*)>=3
6* order by count(*) desc

COUNT(*) COUNTRY
---------- ---------------
7 France
6 Germany
5 UK
4 Brazil
4 Venezuela
4 Spain
3 Mexico

7 rows selected.
16 List all customer with average orders between $100 and $1200.

SQL> select customerid, avg(totalamount)


2 from customer, orders
3 where customerid=customer.id
4 group by customerid
5 having avg(totalamount) between 100 and 1200

CUSTOMERID AVG(TOTALAMOUNT)
---------- ----------------
4 881
7 450
14 625.2
18 424
21 287.8
29 338.2
30 310
38 499.45
41 550.8
45 317.75
46 760.2
47 264
48 125

13 rows selected.

17 List all customers, whether they placed any order or not.

SQL> select distinct c.id, firstname, totalamount


2 from orders o, customer c
3 where o.customerid(+)=c.id;

ID FIRSTNAME TOTALAMOUNT
---------- --------------- -----------
1 Maria
2 Ana
3 Antonio
4 Thomas 285
4 Thomas 1477
5 Christina 174.9
5 Christina 630
5 Christina 1031.7
5 Christina 1565.65
5 Christina 4210.5
6 Hanna
7 Fr�d�rique 450
8 Mart�n
9 Laurence 1057
9 Laurence 1820.8
9 Laurence 2083.4
10 Elizabeth
11 Victoria 1328
11 Victoria 1500
12 Patricio
13 Francisco
14 Yang 625.2
15 Pedro 2169
16 Elizabeth
17 Sven
18 Janine 424
19 Ann 655
19 Ann 950
19 Ann 3063
20 Roland 1834.2
20 Roland 3600.73
20 Roland 4813.5
20 Roland 4903.5
20 Roland 5218
21 Aria 287.8
22 Diego
23 Martine
24 Maria 93
24 Maria 103.2
25 Peter 2300.8
25 Peter 4031
26 Carine
27 Paolo
28 Lino 1836
29 Eduardo 338.2
30 Jos� 310
31 Andr�
32 Howard 8891
33 Manuel
34 Mario
35 Carlos 3163.2
36 Yoshi
37 Patricia 1419.8
37 Patricia 1762
37 Patricia 2614.5
37 Patricia 6201.9
38 Helen 240.4
38 Helen 758.5
39 Philip 11490.7
40 Daniel
41 Annette 550.8
42 Yoshi 57.5
43 John
44 Renate 1873.5
44 Renate 2085
44 Renate 2493
45 Jaime 317.75
46 Carlos 510
46 Carlos 720
46 Carlos 1050.6
47 Felipe 264
48 Fran 125
49 Giovanni
50 Catherine

74 rows selected.

18 Match customers that are from the same city and country

SQL> select firstname, city, country


2 from customer c1
3 where (city, country) in (select city, country from customer c2 where
c1.id<>c2.id);

FIRSTNAME CITY COUNTRY


--------------- --------------- ---------------
Ana M�xico D.F. Mexico
Antonio M�xico D.F. Mexico
Thomas London UK
Mart�n Madrid Spain
Victoria London UK
Francisco M�xico D.F. Mexico
Pedro Sao Paulo Brazil
Elizabeth London UK
Janine Nantes France
Ann London UK
Aria Sao Paulo Brazil
Diego Madrid Spain
Carine Nantes France

13 rows selected.

19 List all customers with their total number of orders

SQL> select customerid, count(*)


2 from orders
3 group by customerid

CUSTOMERID COUNT(*)
---------- ----------
4 2
5 5
7 1
9 3
11 2
14 1
15 1
18 1
19 3
20 5
21 1
24 2
25 2
28 1
29 1
30 1
32 1
35 1
37 4
38 2
39 1
41 1
42 1
44 3
45 1
46 3
47 1
48 1

28 rows selected.
20 Which products were sold by the unit (i.e. quantity = 1)

SQL> select * from product, orderitem


2 where product.id=productid and quantity=1;

no rows selected

21 List customers who placed orders that are�larger than the average of each
customer order

SQL> select customerid, totalamount


2 from orders o
3 where totalamount>(
4 select avg(totalamount)
5 from orders
6 where o.customerid=customerid
7 group by customerid)

CUSTOMERID TOTALAMOUNT
---------- -----------
25 4031
46 1050.6
24 103.2
19 3063
9 1820.8
5 1565.65
44 2493
38 758.5
5 4210.5
37 6201.9
4 1477
9 2083.4
20 4813.5
20 4903.5
11 1500
20 5218

16 rows selected.

22 Find suppliers with products over $100.

�SQL> select supplierid, sum(unitprice) productsprice from product


2 group by supplierid
3 having sum(unitprice)>100

SUPPLIERID PRODUCTSPRICE
---------- -------------
4 138
7 177.85
8 112.7

23 Retrieve the customer names in the lower case, their city and country names in
upper case.

NAME CITY COUNTRY


------------------------------ --------------- ---------------
mariaanders BERLIN GERMANY
anatrujillo M�XICO D.F. MEXICO
antoniomoreno M�XICO D.F. MEXICO
thomashardy LONDON UK
christinaberglund LULE� SWEDEN
hannamoos MANNHEIM GERMANY
fr�d�riqueciteaux STRASBOURG FRANCE
mart�nsommer MADRID SPAIN
laurencelebihan MARSEILLE FRANCE
elizabethlincoln TSAWASSEN CANADA
victoriaashworth LONDON UK
patriciosimpson BUENOS AIRES ARGENTINA
franciscochang M�XICO D.F. MEXICO
yangwang BERN SWITZERLAND
pedroafonso SAO PAULO BRAZIL
elizabethbrown LONDON UK
svenottlieb AACHEN GERMANY
janinelabrune NANTES FRANCE
anndevon LONDON UK
rolandmendel GRAZ AUSTRIA
ariacruz SAO PAULO BRAZIL
diegoroel MADRID SPAIN
martineranc� LILLE FRANCE
marialarsson BR�CKE SWEDEN
peterfranken M�NCHEN GERMANY
carineschmitt NANTES FRANCE
paoloaccorti TORINO ITALY
linorodriguez LISBOA PORTUGAL
eduardosaavedra BARCELONA SPAIN
jos�pedro freyre SEVILLA SPAIN
andr�fonseca CAMPINAS BRAZIL
howardsnyder EUGENE USA
manuelpereira CARACAS VENEZUELA
mariopontes RIO DE JANEIRO BRAZIL
carloshern�ndez SAN CRIST�BAL VENEZUELA
yoshilatimer ELGIN USA
patriciamckenna CORK IRELAND
helenbennett COWES UK
philipcramer BRANDENBURG GERMANY
danieltonini VERSAILLES FRANCE
annetteroulet TOULOUSE FRANCE
yoshitannamuri VANCOUVER CANADA
johnsteel WALLA WALLA USA
renatemessner FRANKFURT A.M. GERMANY
jaimeyorres SAN FRANCISCO USA
carlosgonz�lez BARQUISIMETO VENEZUELA
felipeizquierdo I. DE MARGARITA VENEZUELA
franwilson PORTLAND USA
giovannirovelli BERGAMO ITALY
catherinedewey BRUXELLES BELGIUM

50 rows selected.

24 For American customers, count the number of times �A� occurs in their first
names.

25 Write a query to display the products that are not ordered by any customers.
SQL> select productid
2 from orderitem
3 where productid not in(
4 select productid
5 from orderitem, orders
6 where orderid=orders.id);

no rows selected

26 Write a query to display all customer details where customer placed an order
during the year 2013 and in the 4th week of a month.

27 Display the time of day.

28 Write an SQL statement to convert the current date to new date picture. ex:
MONDAY 10th 2005 10:30.00 PM
29 Write a query which will return the DAY of the week. (i.e. MONDAY), for any date
entered in the format: DD.MM.YY.
30 Write a query to display details of orders which are placed on last Friday of a
month.
31 Write a query that will display list of products and their unit price and the
comments as follows.
32 If the unitprice is more than 20 then display �EXPENSIVE�
33 If the salary is equal to 20 then display �AVERAGE�
34 If the salary is less than 20 then display �INEXPENSIVE�
35 Write a query to find out the customers who have placed maximum number of
orders.
36 Write a query to find out the year, where most orders were placed. Display the
year and No. of orders.
37 Write a query to retrieve the customer name, total order amount by the customer.
38 Write a query to retrieve the company name detail of the supplier living in the
same city of any of his customers.
39 Find all suppliers who supplied lowest valued products in each category
(Discontinued / Not Discontinued).
40 Retrieve the details of supplier who supplied least number of products during
the year 2013.

You might also like