*Kontak **\(\downarrow\)*
Email
Instagram https://www.instagram.com/its_bangjeki/
RPubs https://rpubs.com/valensiusjimy/
Nama Valensius Jimy
NIM 20214920005

  Setelah mempelajari berbagai macam cara dalam proses manipulasi data dalam SQL yang dilakukan melalui aplikasi R. Pada kesempatan kali ini, terdapat lebih dari 20 latihan soal untuk memperdalam bagaimana proses manipulasi data SQL. Dengan adanya latihan soal ini diharapkan dapat mengerti lebih dalam dan memahami. Maka dari itu, tidak perlu waktu yang lama lagi mari kita lakukan penyelesaiannya.

1 Menyambungkan R dengan SQL

  Karena ingin melakukan prosesnya melalui aplikasi R, maka kita harus menyambungkan koneksi R dengan SQL dan jangan lupa terdapat library yang digunakan dan harus dipanggil dan langkahnya adalah sebagai berikut ini.

pacman::p_load(RMariaDB,
               RMySQL,
               RSQLite,
               DBI)

setelah itu, kita dapat menyambungkan dengan sintaks berikut.

jekiw <- dbConnect(MariaDB(),
                   user = 'root',
                   password = '',
                   dbname = 'bang jeki',
                   host = 'localhost')
knitr::opts_chunk$set(connection = "jekiw")

2 Memilih dan Mengurutkan Kolom

  Pada soal yang pertama ini, kita akan memilih beberapa kolom dan isinya dari tabel suppliers yang kita miliki dan diurutkan secara A-Z atau dalam kata lain kita akan menggunakan fungsi ASC untuk mengurutkannya. Dan sebelum itu, kita lihat terlebih dahulu pada tabelnya terdapat kolom apa saja.

select *
from suppliers
Displaying records 1 - 10
SupplierID SupplierName ContactName Address City PostalCode Country Phone
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Londona EC1 4SD UK (171) 555-2222
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA (100) 555-4822
3 Grandma Kelly’s Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA (313) 555-5735
4 Tokyo Traders Yoshi Nagase 9-8 Sekimai Musashino-shi Tokyo 100 Japan (03) 3555-5011
5 Cooperativa de Quesos ‘Las Cabras’ Antonio del Valle Saavedra Calle del Rosal 4 Oviedo 33007 Spain (98) 598 76 54
6 Mayumi’s Mayumi Ohno 92 Setsuko Chuo-ku Osaka 545 Japan (06) 431-7877
7 Pavlova, Ltd. Ian Devling 74 Rose St. Moonie Ponds Melbourne 3058 Australia (03) 444-2343
8 Specialty Biscuits, Ltd. Peter Wilson 29 King’s Way Manchester M14 GSD UK (161) 555-4448
9 PB Knäckebröd AB Lars Peterson Kaloadagatan 13 Göteborg S-345 67 Sweden 031-987 65 43
10 Refrescos Americanas LTDA Carlos Diaz Av. das Americanas 12.890 São Paulo 5442 Brazil (11) 555 4640

Setelah melihat kolom yang terdapat pada tabel tersebut, mungkin tidak banyak informasi ringkas yang dapat kita ambil, sehingga di sini sebagai contohnya kita dapat memilih beberapa kolom saja untuk ditampilkan, misalnya SupplierName, Addres, City, dan Phone.

select SupplierName, Address, City, Phone
from suppliers
Displaying records 1 - 10
SupplierName Address City Phone
Exotic Liquid 49 Gilbert St. Londona (171) 555-2222
New Orleans Cajun Delights P.O. Box 78934 New Orleans (100) 555-4822
Grandma Kelly’s Homestead 707 Oxford Rd. Ann Arbor (313) 555-5735
Tokyo Traders 9-8 Sekimai Musashino-shi Tokyo (03) 3555-5011
Cooperativa de Quesos ‘Las Cabras’ Calle del Rosal 4 Oviedo (98) 598 76 54
Mayumi’s 92 Setsuko Chuo-ku Osaka (06) 431-7877
Pavlova, Ltd. 74 Rose St. Moonie Ponds Melbourne (03) 444-2343
Specialty Biscuits, Ltd. 29 King’s Way Manchester (161) 555-4448
PB Knäckebröd AB Kaloadagatan 13 Göteborg 031-987 65 43
Refrescos Americanas LTDA Av. das Americanas 12.890 São Paulo (11) 555 4640

Terlihat dari hasil di atas, tentang nama supplier hingga nomer teleponnya dan berdasarkan hasil tersebut juga memudahkan pembaca untuk menarik informasi tanpa membaca kolom tabel yang terlalu banyak. Selanjutnya, bagaimana jika ingin mengurutkan abjad nama supplier entah secara ascending atau descending.

2.1 Mengurutkan Nama A-Z

Disini kita dapat mencoba untuk menampilkan urutan nama supplier secara A-Z

select SupplierName, Address, City, Phone
from suppliers
order by SupplierName ASC
Displaying records 1 - 10
SupplierName Address City Phone
Aux joyeux ecclésiastiques 203, Rue des Francs-Bourgeois Paris (1) 03.83.00.68
Bigfoot Breweries 3400 - 8th Avenue Suite 210 Bend (503) 555-9931
Cooperativa de Quesos ‘Las Cabras’ Calle del Rosal 4 Oviedo (98) 598 76 54
Escargots Nouveaux 22, rue H. Voiron Montceau 85.57.00.07
Exotic Liquid 49 Gilbert St. Londona (171) 555-2222
Forêts d’érables 148 rue Chasseur Ste-Hyacinthe (514) 555-2955
Formaggi Fortini s.r.l. Viale Dante, 75 Ravenna (0544) 60323
G’day, Mate 170 Prince Edward Parade Hunter’s Hill Sydney (02) 555-5914
Gai pâturage Bat. B 3, rue des Alpes Annecy 38.76.98.06
Grandma Kelly’s Homestead 707 Oxford Rd. Ann Arbor (313) 555-5735

2.2 Mengurutkan Nama Z-A

Dan kita dapat mengurutkan nama tersebut secara Z-A

select SupplierName, Address, City, Phone
from suppliers
order by SupplierName DESC
Displaying records 1 - 10
SupplierName Address City Phone
Zaanse Snoepfabriek Verkoop Rijnweg 22 Zaandam (12345) 1212
Tokyo Traders 9-8 Sekimai Musashino-shi Tokyo (03) 3555-5011
Svensk Sjöföda AB Brovallavägen 231 Stockholm 08-123 45 67
Specialty Biscuits, Ltd. 29 King’s Way Manchester (161) 555-4448
Refrescos Americanas LTDA Av. das Americanas 12.890 São Paulo (11) 555 4640
Plutzer Lebensmittelgroßmärkte AG Bogenallee 51 Frankfurt (069) 992755
PB Knäckebröd AB Kaloadagatan 13 Göteborg 031-987 65 43
Pavlova, Ltd. 74 Rose St. Moonie Ponds Melbourne (03) 444-2343
Pasta Buttini s.r.l. Via dei Gelsomini, 153 Salerno (089) 6547665
Norske Meierier Hatlevegen 5 Sandvika (0)2-953010

3 Mengurutkan Berdasarkan Kolom lain

Kita juga dapat memanfaatkan kolom lain sebagai acuan untuk mengurutkan sebuah datanya, di sini kita dapat mengurutkan supplier dengan kelompok Country, dilanjutkan dengan City.

select SupplierName, City, Country, Phone
from suppliers
order by Country, City
Displaying records 1 - 10
SupplierName City Country Phone
Pavlova, Ltd. Melbourne Australia (03) 444-2343
G’day, Mate Sydney Australia (02) 555-5914
Refrescos Americanas LTDA São Paulo Brazil (11) 555 4640
Ma Maison Montréal Canada (514) 555-9022
Forêts d’érables Ste-Hyacinthe Canada (514) 555-2955
Lyngbysild Lyngby Denmark 43844108
Karkki Oy Lappeenranta Finland (953) 10956
Gai pâturage Annecy France 38.76.98.06
Escargots Nouveaux Montceau France 85.57.00.07
Aux joyeux ecclésiastiques Paris France (1) 03.83.00.68

3.1 Diurutkan secara Descending

Jika sebelumnya sudah mengurutkan sesuai dengan urutan abjad, kini dapat dilakukan urutan yang sebaliknya.

select SupplierName, City, Country, Phone
from suppliers
order by country desc, city desc 
Displaying records 1 - 10
SupplierName City Country Phone
New Orleans Cajun Delights New Orleans USA (100) 555-4822
New England Seafood Cannery Boston USA (617) 555-3267
Bigfoot Breweries Bend USA (503) 555-9931
Grandma Kelly’s Homestead Ann Arbor USA (313) 555-5735
Specialty Biscuits, Ltd. Manchester UK (161) 555-4448
Exotic Liquid Londona UK (171) 555-2222
Svensk Sjöföda AB Stockholm Sweden 08-123 45 67
PB Knäckebröd AB Göteborg Sweden 031-987 65 43
Cooperativa de Quesos ‘Las Cabras’ Oviedo Spain (98) 598 76 54
Leka Trading Singapore Singapore 555-8787

4 Mengurutkan Order Berdasarkan Total Pembelian Tertinggi

Karena pada data kita tidak terdapat kolom total pembelian dari customer, sehingga kita harus membuat kolom tersebut dengan memanfaatkan variabel yang lain yang bisa menjadi total penjualan. Maka dari itu, kita dapat memilih Quantity dan Price karena pada dasarnya untuk menentukan harganya adalah dengan mengkalikan jumlah barang yang terjual dengan harga per unitnya.

select M.ProductID, ProductName, Unit, (M.price*MK.quantity) as total_amount
from products M
right join orderdetails MK
on M.ProductID = MK.ProductID
order by total_amount DESC
Displaying records 1 - 10
ProductID ProductName Unit total_amount
38 Côte de Blaye 12 - 75 cl bottles 13175.00
38 Côte de Blaye 12 - 75 cl bottles 13175.00
38 Côte de Blaye 12 - 75 cl bottles 12911.50
38 Côte de Blaye 12 - 75 cl bottles 10540.00
38 Côte de Blaye 12 - 75 cl bottles 5270.00
38 Côte de Blaye 12 - 75 cl bottles 5270.00
29 Thüringer Rostbratwurst 50 bags x 30 sausgs. 4332.65
59 Raclette Courdavault 5 kg pkg. 3850.00
59 Raclette Courdavault 5 kg pkg. 3850.00
63 Vegie-spread 15 - 625 g jars 3512.00

Dari hasil tersebut terlihat bahwa order yang terjadi sudah diurutkan sesuai dengan total penjualan tertinggi.

5 10 Produk Termahal Berdasarkan Harga

Kasus kali ini, kita akan menampilkan 10 produk termahal berdasarkan harganya.

select *
from products
order by Price DESC
limit 10
Displaying records 1 - 10
ProductID ProductName SupplierID CategoryID Unit Price
38 Côte de Blaye 18 1 12 - 75 cl bottles 263.50
29 Thüringer Rostbratwurst 12 6 50 bags x 30 sausgs. 123.79
9 Mishi Kobe Niku 4 6 18 - 500 g pkgs. 97.00
20 Sir Rodney’s Marmalade 8 3 30 gift boxes 81.00
18 Carnarvon Tigers 7 8 16 kg pkg. 62.50
59 Raclette Courdavault 28 4 5 kg pkg. 55.00
51 Manjimup Dried Apples 24 7 50 - 300 g pkgs. 53.00
62 Tarte au sucre 29 3 48 pies 49.30
43 Ipoh Coffee 20 1 16 - 500 g tins 46.00
28 Rössle Sauerkraut 12 7 25 - 825 g cans 45.60

Dapat dilihat, ternyata produk yang paling mahal adalah Côte de Blaye.

6 Produk termahal ke-10 sampai ke-15

Selanjutnya, kita coba untuk mencari produk termahal pada posisi 10-15 berdasarkan harganya.

select productname, price
from products 
order by price desc
limit 10, 6
6 records
productname price
Schoggi Schokolade 43.9
Vegie-spread 43.9
Northwoods Cranberry Sauce 40.0
Alice Mutton 39.0
Queso Manchego La Pastora 38.0
Gnocchi di nonna Alice 38.0

7 Daftar Negara Supplier

select distinct country
from suppliers
Displaying records 1 - 10
country
UK
USA
Japan
Spain
Australia
Sweden
Brazil
Germany
Italy
Norway

terlihat hasil di atas berbagai negara asal dari supplier terdaftar, mari kita coba untuk mengurutkannya berdasarkan urutan abjad.

select distinct country
from suppliers
order by country asc
Displaying records 1 - 10
country
Australia
Brazil
Canada
Denmark
Finland
France
Germany
Italy
Japan
Netherlands

8 Produk Termurah dan Termahal

Kita akan mencari produk yang termurah dan termahal

8.1 produk termurah

select productname, Price
from products
order by price asc
limit 1
1 records
productname Price
Geitost 2.5

dapat dilihat ternyata produk termurah adalah Geitost.

8.2 produk termahal

select productname, Price
from products
order by price desc
limit 1
1 records
productname Price
Côte de Blaye 263.5

sedangkan untuk produk paling mahal adalah Côte de Blaye.

9 Jumlah Supplier dari USA

selanjutnya, kita akan mencari jumlah supplier yang berasal dari USA

select country, count(country) as total_supplier
from suppliers
where country='USA'
1 records
country total_supplier
USA 4

Jika kita ingin mencari dari negara lain, tinggal pilih atau masukkan negara mana yang ingin kita cari di bagian conditionnya. Seperti di bawah ini, kita dapat coba untuk mencari jumlah supplier yang berasal dari negara Germany.

select country, count(country) as total_supplier
from suppliers
where Country = 'Germany'
1 records
country total_supplier
Germany 3

10 Menghitung Jumlah Kuantitas Terjual

Kita dapat menghitung jumlah kuantitas produk yang terjual pada tabel yang kita miliki, ingin sangat membantu dalam bidang bisnis seperti ini.

select sum(quantity) as total_quantity
from orderdetails
1 records
total_quantity
12743

Berdasarkan hasil tersebut, kita menjadi paham bahwa total kuantitas atau produk yang terjual sebanyak 12743.

11 Menghitung Rerata Harga Produk

Selanjutnya, kita akan mencari tahu sebetulnya produk yang dijual memiliki rata-rata harga berapa.

select avg(price) as average_unit_price
from products
1 records
average_unit_price
28.86636

ternyata dari semua produk yang dijual memiliki rata-rata harga sebesar 28.86636

12 Informasi Tentang Thomas Hardy

Pada tabel customer kita kali ini terdapat seorang customer yang bernama Thomas Hardy dan kita dapat mencari tahu detail informasi tentang dia dengan cara sebagai berikut ini.

select distinct *
from ((customers M
join orders K 
on M.customerID=K.customerID)
join orderdetails MK
on K.orderID = MK.orderID)
join products H
on MK.ProductID=H.ProductID
where contactname='thomas hardy'
order by orderdate
5 records
CustomerID CustomerName ContactName Address City PostalCode Country OrderID CustomerID..9 EmployeeID OrderDate ShipperID OrderDetailID OrderID..14 ProductID Quantity ProductID..17 ProductName SupplierID CategoryID Unit Price
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 10355 4 6 1996-11-15 1 285 10355 24 25 24 Guaraná Fantástica 10 1 12 - 355 ml cans 4.50
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 10355 4 6 1996-11-15 1 286 10355 57 25 57 Ravioli Angelo 26 5 24 - 250 g pkgs. 19.50
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 10383 4 8 1996-12-16 3 360 10383 56 20 56 Gnocchi di nonna Alice 26 5 24 - 250 g pkgs. 38.00
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 10383 4 8 1996-12-16 3 358 10383 13 20 13 Konbu 6 8 2 kg box 6.00
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK 10383 4 8 1996-12-16 3 359 10383 50 15 50 Valkoinen suklaa 23 3 12 - 100 g bars 16.25

dengan melakukan hal tersebut, kita dapat mengharapkan adanya repeat order dari Thomas Hardy dengan cara men-follow up data tersebut.

13 Daftar Customer dari Spanyol dan Prancis

Pada kasus ini, kita dapat mencari data customer yang berasal dari negara Spain dan France dengan langkah sebagai berikut.

select * 
from customers
where country = "spain" or country = "france"
Displaying records 1 - 10
CustomerID CustomerName ContactName Address City PostalCode Country
7 Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
8 Bólido Comidas preparadas Martín Sommer C/ Araquil, 67 Madrid 28023 Spain
9 Bon app’ Laurence Lebihans 12, rue des Bouchers Marseille 13008 France
18 Du monde entier Janine Labrune 67, rue des Cinquante Otages Nantes 44000 France
22 FISSA Fabrica Inter. Salchichas S.A. Diego Roel C/ Moralzarzal, 86 Madrid 28034 Spain
23 Folies gourmandes Martine Rancé 184, chaussée de Tournai Lille 59000 France
26 France restauration Carine Schmitt 54, rue Royale Nantes 44000 France
29 Galería del gastrónomo Eduardo Saavedra Rambla de Cataluña, 23 Barcelona 8022 Spain
30 Godos Cocina Típica José Pedro Freyre C/ Romero, 33 Sevilla 41101 Spain
40 La corne d’abondance Daniel Tonini 67, avenue de l’Europe Versailles 78000 France

14 Daftar Customer bukan berasal dari USA

Kita juga dapat mencari tahu detail informasi customer yang bukan berasal dari negara USA dengan menggunakan kondisi where not.

select * 
from customers
where not country="USA"
Displaying records 1 - 10
CustomerID CustomerName ContactName Address City PostalCode Country
1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Avda. de la Constitución 2222 México D.F. 5021 Mexico
3 Antonio Moreno Taquería Antonio Moreno Mataderos 2312 México D.F. 5023 Mexico
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
5 Berglunds snabbköp Christina Berglund Berguvsvägen 8 Luleå S-958 22 Sweden
6 Blauer See Delikatessen Hanna Moos Forsterstr. 57 Mannheim 68306 Germany
7 Blondel père et fils Frédérique Citeaux 24, place Kléber Strasbourg 67000 France
8 Bólido Comidas preparadas Martín Sommer C/ Araquil, 67 Madrid 28023 Spain
9 Bon app’ Laurence Lebihans 12, rue des Bouchers Marseille 13008 France
10 Bottom-Dollar Marketse Elizabeth Lincoln 23 Tsawassen Blvd. Tsawassen T2F 8M4 Canada

15 Order yang tidak berharga diantara

kita akan mencoba untuk mencari tahu order yang tidak bernilai diantara harga 50 USD dan 15000 USD

select j.*, e.productid, e.productname, e.price
from orders j
left join orderdetails OD
on j.orderid = od.orderid
left join products e
on od.productid = e.ProductID
where e.price
not between 50 and 1500
Displaying records 1 - 10
OrderID CustomerID EmployeeID OrderDate ShipperID productid productname price
10248 90 5 1996-07-04 3 11 Queso Cabrales 21.00
10248 90 5 1996-07-04 3 42 Singaporean Hokkien Fried Mee 14.00
10248 90 5 1996-07-04 3 72 Mozzarella di Giovanni 34.80
10249 81 6 1996-07-05 1 14 Tofu 23.25
10250 34 4 1996-07-08 2 41 Jack’s New England Clam Chowder 9.65
10250 34 4 1996-07-08 2 65 Louisiana Fiery Hot Pepper Sauce 21.05
10251 84 3 1996-07-08 1 22 Gustaf’s Knäckebröd 21.00
10251 84 3 1996-07-08 1 57 Ravioli Angelo 19.50
10251 84 3 1996-07-08 1 65 Louisiana Fiery Hot Pepper Sauce 21.05
10252 76 4 1996-07-09 2 33 Geitost 2.50

16 Produk Bernilai antara $10 dan $20

select distinct *
from products
where Price
between 10 and 20
Displaying records 1 - 10
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 bags 18.00
2 Chang 1 1 24 - 12 oz bottles 19.00
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10.00
15 Genen Shouyu 6 2 24 - 250 ml bottles 15.50
16 Pavlova 7 3 32 - 500 g boxes 17.45
21 Sir Rodney’s Scones 8 3 24 pkgs. x 4 pieces 10.00
25 NuNuCa Nuß-Nougat-Creme 11 3 20 - 450 g glasses 14.00
31 Gorgonzola Telino 14 4 12 - 100 g pkgs 12.50
34 Sasquatch Ale 16 1 24 - 12 oz bottles 14.00
35 Steeleye Stout 16 1 24 - 12 oz bottles 18.00

16.1 Produk Bukan Bernilai antara $10 dan $20

Sebaliknya, kita sudah mencoba untuk menampilkan produk dengan harga diantara 10 USD dan 20 USD. Saat ini, kita tampilkan yang bukan diantara segitu

select distinct *
from products
where price
not between 10 and 100
order by price asc
Displaying records 1 - 10
ProductID ProductName SupplierID CategoryID Unit Price
33 Geitost 15 4 500 g 2.50
24 Guaraná Fantástica 10 1 12 - 355 ml cans 4.50
13 Konbu 6 8 2 kg box 6.00
52 Filo Mix 24 5 16 - 2 kg boxes 7.00
54 Tourtière 25 6 16 pies 7.45
75 Rhönbräu Klosterbier 12 1 24 - 0.5 l bottles 7.75
23 Tunnbröd 9 5 12 - 250 g pkgs. 9.00
19 Teatime Chocolate Biscuits 8 3 10 boxes x 12 pieces 9.20
45 Røgede sild 21 8 1k pkg. 9.50
47 Zaanse koeken 22 3 10 - 4 oz boxes 9.50

17 Jumlah Terjual Pada Rentang Waktu Tertentu

Selanjutnya, kita akan mencari tahu jumlah order yang terjadi pada rentang waktu tertentu, misalkan pada kali ini kita ingin mencari tahu jumlah order yang terjadi pada rentang waktu 1 Januari 1996 dan 31 Desember 1996

select m.orderdate, sum(MK.quantity) as Jumlah_Terjual
from orders m
join orderdetails MK
on m.orderid = mk.orderid
group by m.OrderID
having m.orderdate
between '1996-01-01' and '1996-12-31'
order by m.orderdate
Displaying records 1 - 10
orderdate Jumlah_Terjual
1996-07-04 27
1996-07-05 49
1996-07-08 41
1996-07-08 60
1996-07-09 105
1996-07-10 102
1996-07-11 57
1996-07-12 110
1996-07-15 27
1996-07-16 46

18 Daftar Supplier Berasal Dari Negara Tertentu

Kemudian, mencari daftar supplier yang berasal dari negara USA, UK dan Japan.

select *
from suppliers
where Country
in ('USA', 'UK', 'JAPAN')
8 records
SupplierID SupplierName ContactName Address City PostalCode Country Phone
1 Exotic Liquid Charlotte Cooper 49 Gilbert St. Londona EC1 4SD UK (171) 555-2222
2 New Orleans Cajun Delights Shelley Burke P.O. Box 78934 New Orleans 70117 USA (100) 555-4822
3 Grandma Kelly’s Homestead Regina Murphy 707 Oxford Rd. Ann Arbor 48104 USA (313) 555-5735
4 Tokyo Traders Yoshi Nagase 9-8 Sekimai Musashino-shi Tokyo 100 Japan (03) 3555-5011
6 Mayumi’s Mayumi Ohno 92 Setsuko Chuo-ku Osaka 545 Japan (06) 431-7877
8 Specialty Biscuits, Ltd. Peter Wilson 29 King’s Way Manchester M14 GSD UK (161) 555-4448
16 Bigfoot Breweries Cheryl Saylor 3400 - 8th Avenue Suite 210 Bend 97101 USA (503) 555-9931
19 New England Seafood Cannery Robb Merchant Order Processing Dept. 2100 Paul Revere Blvd. Boston 2134 USA (617) 555-3267

19 Produk Bukan Harga Tertentu

adapun kita dapat memilih harga produk tertentu, misalkan harga 20 USD atau lainnya

select *
from products
where price
not in (10, 20, 30, 40, 50)
Displaying records 1 - 10
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 bags 18.00
2 Chang 1 1 24 - 12 oz bottles 19.00
4 Chef Anton’s Cajun Seasoning 2 2 48 - 6 oz jars 22.00
5 Chef Anton’s Gumbo Mix 2 2 36 boxes 21.35
6 Grandma’s Boysenberry Spread 3 2 12 - 8 oz jars 25.00
9 Mishi Kobe Niku 4 6 18 - 500 g pkgs. 97.00
10 Ikura 4 8 12 - 200 ml jars 31.00
11 Queso Cabrales 5 4 1 kg pkg. 21.00
12 Queso Manchego La Pastora 5 4 10 - 500 g pkgs. 38.00
13 Konbu 6 8 2 kg box 6.00

atau bisa juga nmelihat harga produk dengan satu harga saja, seperti berikuti ini.

select *
from products
where Price = 10
3 records
ProductID ProductName SupplierID CategoryID Unit Price
3 Aniseed Syrup 1 2 12 - 550 ml bottles 10
21 Sir Rodney’s Scones 8 3 24 pkgs. x 4 pieces 10
74 Longlife Tofu 4 7 5 kg pkg. 10

terlihat bahwa kita memilih produk dengan harga 10 USD saja dan hasilnya ternyata ada 3 produk dengan harga tertentu.

20 Customer yang berasal dari negara yang sama dengan negara supplier

select customername, suppliername, c.country, s.country
from customers c
join orders o
on c.customerid = o.CustomerID
join orderdetails OD
on o.orderid = od.orderid
join products p
on od.productid = p.productid
join suppliers s
on p.SupplierID = s.SupplierID
where c.country=s.country
Displaying records 1 - 10
customername suppliername country country..4
Old World Delicatessen New England Seafood Cannery USA USA
Rattlesnake Canyon Grocery New Orleans Cajun Delights USA USA
Rattlesnake Canyon Grocery Grandma Kelly’s Homestead USA USA
Morgenstern Gesundkost Plutzer Lebensmittelgroßmärkte AG Germany Germany
Berglunds snabbköp Svensk Sjöföda AB Sweden Sweden
Lehmanns Marktstand Heli Süßwaren GmbH & Co. KG Germany Germany
B’s Beverages Exotic Liquid UK UK
Blondel père et fils Aux joyeux ecclésiastiques France France
Die Wandernde Kuh Plutzer Lebensmittelgroßmärkte AG Germany Germany
Die Wandernde Kuh Plutzer Lebensmittelgroßmärkte AG Germany Germany

21 Produk dengan nama depan ‘cha’ dan ‘chan’

select * 
from products
where ProductName
like 'cha_%' or 'chan_%'
3 records
ProductID ProductName SupplierID CategoryID Unit Price
1 Chais 1 1 10 boxes x 20 bags 18
2 Chang 1 1 24 - 12 oz bottles 19
39 Chartreuse verte 18 1 750 cc per bottle 18

22 Customer Dengan Rerata Beli $1000 dan $1200

SELECT CustomerName, AVG(TotalAmount) AverageOrders
FROM 
(
SELECT CustomerName, SUM(od.Quantity * p.Price) TotalAmount
FROM customers c
JOIN orders o
ON c.CustomerID = o.CustomerID
JOIN orderdetails od
ON o.OrderID = od.OrderID
JOIN products p
ON od.ProductID = p.ProductID
GROUP BY od.OrderID
) A
GROUP BY CustomerName
HAVING AverageOrders
BETWEEN 1000 AND 1200
ORDER BY AverageOrders
6 records
CustomerName AverageOrders
Que Delícia 1012.275
Königlich Essen 1039.375
Tortuga Restaurante 1067.350
Folk och fä HB 1078.475
Magazzini Alimentari Riuniti 1080.300
Chop-suey Chinese 1124.500

23 Total Customer dari Seluruh Negara

SELECT Country, COUNT(CustomerName) as Total
FROM customers
GROUP BY Country
Displaying records 1 - 10
Country Total
Argentina 3
Austria 2
Belgium 2
Brazil 9
Canada 3
Denmark 2
Finland 2
France 11
Germany 11
Ireland 1