MENYAMBUNGKAN R TO
SQL
## Loading required package: DBI
library(DBI)
Jengkolbalado <- dbConnect(MySQL(),
user='root',
password='',
dbname='jengkolbalado',
host='localhost')
List All order with
customer.
SELECT *
FROM Customers C
LEFT JOIN Orders O
ON C.CustomerID = O.CustomerID
Displaying records 1 - 10
| 90 |
90 |
Wilman Kala |
Matti Karttunen |
Keskuskatu 45 |
Helsinki |
21240 |
Finland |
1 |
10248 |
90 |
5 |
04/07/1996 |
3 |
| 81 |
81 |
Tradição Hipermercados |
Anabela Domingues |
Av. Inês de Castro, 414 |
São Paulo |
05634-030 |
Brazil |
2 |
10249 |
81 |
6 |
05/07/1996 |
1 |
| 34 |
34 |
Hanari Carnes |
Mario Pontes |
Rua do Paço, 67 |
Rio de Janeiro |
05454-876 |
Brazil |
3 |
10250 |
34 |
4 |
08/07/1996 |
2 |
| 84 |
84 |
Victuailles en stock |
Mary Saveley |
2, rue du Commerce |
Lyon |
69004 |
France |
4 |
10251 |
84 |
3 |
08/07/1996 |
1 |
| 76 |
76 |
Suprêmes délices |
Pascale Cartrain |
Boulevard Tirou, 255 |
Charleroi |
B-6000 |
Belgium |
5 |
10252 |
76 |
4 |
09/07/1996 |
2 |
| 34 |
34 |
Hanari Carnes |
Mario Pontes |
Rua do Paço, 67 |
Rio de Janeiro |
05454-876 |
Brazil |
6 |
10253 |
34 |
3 |
10/07/1996 |
2 |
| 14 |
14 |
Chop-suey Chinese |
Yang Wang |
Hauptstr. 29 |
Bern |
3012 |
Switzerland |
7 |
10254 |
14 |
5 |
11/07/1996 |
2 |
| 68 |
68 |
Richter Supermarkt |
Michael Holz |
Grenzacherweg 237 |
Genève |
1203 |
Switzerland |
8 |
10255 |
68 |
9 |
12/07/1996 |
3 |
| 88 |
88 |
Wellington Importadora |
Paula Parente |
Rua do Mercado, 12 |
Resende |
08737-363 |
Brazil |
9 |
10256 |
88 |
3 |
15/07/1996 |
2 |
| 35 |
35 |
HILARIÓN-Abastos |
Carlos Hernández |
Carrera 22 con Ave. Carlos Soublette #8-35 |
San Cristóbal |
5022 |
Venezuela |
10 |
10257 |
35 |
4 |
16/07/1996 |
3 |
List orders with
product names,quantities,price.
SELECT od.orderid, p.productname, od.quantity, p.price from orderdetails od
left join products p on p.ProductID = od.ProductID
order by orderid
Displaying records 1 - 10
| 10248 |
Queso Cabrales |
12 |
21 |
| 10248 |
Singaporean Hokkien Fried Mee |
10 |
14 |
| 10248 |
Mozzarella di Giovanni |
5 |
34,8 |
| 10249 |
Tofu |
9 |
23,25 |
| 10249 |
Manjimup Dried Apples |
40 |
53 |
| 10250 |
Jack’s New England Clam Chowder |
10 |
9,65 |
| 10250 |
Manjimup Dried Apples |
35 |
53 |
| 10250 |
Louisiana Fiery Hot Pepper Sauce |
15 |
21,05 |
| 10251 |
Gustaf’s Knäckebröd |
6 |
21 |
| 10251 |
Ravioli Angelo |
15 |
19,5 |
this will list all
customers,whether they placed any order or not.
SELECT CustomerName As all_CustomerName From Customers
Displaying records 1 - 10
| Alfreds Futterkiste |
| Ana Trujillo Emparedados y helados |
| Antonio Moreno Taquería |
| Around the Horn |
| Berglunds snabbköp |
| Blauer See Delikatessen |
| Blondel père et fils |
| Bólido Comidas preparadas |
| Bon app’ |
| Bottom-Dollar Marketse |
list customers that
have not placed orders
SELECT CustomerName as customerajikOrder From customers
WHERE CustomerID NOT IN (SELECT CustomerID From Orders)
Displaying records 1 - 10
| Alfreds Futterkiste |
| Blauer See Delikatessen |
| Cactus Comidas para llevar |
| FISSA Fabrica Inter. Salchichas S.A. |
| France restauration |
| Great Lakes Food Market |
| La corne d’abondance |
| Laughing Bacchus Wine Cellars |
| Lazy K Kountry Store |
| Let’s Stop N Shop |
list product with order
quantities greater than 80.
select od.orderid, p.productname, od.quantity from orderdetails od
left join products as p
on p.productID = od.ProductID
where quantity > 80
3 records
| 10286 |
Steeleye Stout |
100 |
| 10398 |
Pâté chinois |
120 |
| 10440 |
Sirop d’érable |
90 |
which product were sold
bay the unit (i.e. quantity=1)
select od.orderid, P.ProductName, P.unit, od.quantity from orderdetails od
left join products p
on p.ProductID = od.ProductID
where quantity=1
3 records
| 10281 |
Teatime Chocolate Biscuits |
10 boxes x 12 pieces |
1 |
| 10259 |
Gravad lax |
12 - 500 g pkgs. |
1 |
| 10308 |
Gudbrandsdalsost |
10 kg pkg. |
1 |
list customer who
placed orders that are large than the average of each customer
order.
select customers.CustomerName, count(orders.orderid) as total_ordered from customers
inner join orders on customers.CustomerID=orders.CustomerID
group by customername
having count(orders.orderid)
order by count(orders.orderid) desc
Displaying records 1 - 10
| Ernst Handel |
10 |
| Rattlesnake Canyon Grocery |
7 |
| Wartian Herkku |
7 |
| QUICK-Stop |
7 |
| Split Rail Beer & Ale |
6 |
| Hungry Owl All-Night Grocers |
6 |
| LILA-Supermercado |
5 |
| Mère Paillarde |
5 |
| La maison d’Asie |
5 |
| Old World Delicatessen |
4 |
Find best selling
Product base on quantity.
Select products.ProductName, sum(orderdetails.quantity) as best_products
from orderdetails
Inner join products on orderdetails.ProductID = products.ProductID
group by ProductName
order by best_products desc
Limit 1
1 records
| Gorgonzola Telino |
458 |
Find best selling
Product based on revenue.
select products.productname, products.price*orderdetails.quantity as total_amount from orderdetails
join Products on orderdetails.ProductID=products.ProductID
group by productname
Order by Total_amount DESC
Limit 1
1 records
| Côte de Blaye |
5260 |
Find best selling
Product based on revenue for each country.
select country from customers
group by country
Displaying records 1 - 10
| Argentina |
| Austria |
| Belgium |
| Brazil |
| Canada |
| Denmark |
| Finland |
| France |
| Germany |
| Ireland |
Find suppliers with a
product price less than $50
select suppliername from suppliers
where exists (select productname from products where products.SupplierID = suppliers.SupplierID and price<50)
Displaying records 1 - 10
| Exotic Liquid |
| New Orleans Cajun Delights |
| Grandma Kelly’s Homestead |
| Tokyo Traders |
| Cooperativa de Quesos ‘Las Cabras’ |
| Mayumi’s |
| Pavlova, Ltd. |
| Specialty Biscuits, Ltd. |
| PB Knäckebröd AB |
| Refrescos Americanas LTDA |
Find top 10 best
employees base on their sales quantity.
select employees.FirstName, sum(orderdetails.quantity) as top10sales from orders
inner join Employees on orders.EmployeeID = Employees.EmployeeID
inner join orderdetails on orderdetails.OrderID = orders.OrderID
group by firstname
order by top10sales desc
9 records
| Margaret |
3232 |
| Nancy |
1924 |
| Janet |
1725 |
| Andrew |
1315 |
| Laura |
1293 |
| Michael |
1094 |
| Steven |
778 |
| Robert |
733 |
| Anne |
649 |
Find top 10 best
supplier countries based on quantity.
Select suppliers.Country, sum(Orderdetails.quantity) top10country_supplier from orders
inner join orderdetails on orders.OrderID = orderdetails.OrderID
inner join products on orderdetails.ProductID = products.ProductID
inner join suppliers on products.SupplierID = suppliers.SupplierID
group by country
order by top10country_supplier desc
limit 10
Displaying records 1 - 10
| Australia |
1610 |
| USA |
1581 |
| France |
1436 |
| Germany |
1339 |
| Italy |
1217 |
| UK |
1213 |
| Canada |
949 |
| Norway |
836 |
| Japan |
560 |
| Sweden |
465 |
FInd top 10 best
customer countries base on quantity.
select customers.Country,sum(orderdetails.quantity) 10topcountry_sales from orders
inner join customers on orders.CustomerID = customers.CustomerID
inner join orderdetails on orderdetails.OrderID = orders.OrderID
group by country
order by 10topcountry_sales desc
limit 10
Displaying records 1 - 10
| USA |
2139 |
| Germany |
2015 |
| Austria |
1565 |
| Brazil |
1117 |
| France |
889 |
| Canada |
772 |
| UK |
698 |
| Ireland |
565 |
| Venezuela |
492 |
| Sweden |
399 |
Find top 10 best
selling product based on quantity in every year.
select products.ProductName, sum(orderdetails.quantity) as top10_products from orderdetails
inner join products on orderdetails.ProductID = products.ProductID
group by productname
order by top10_products desc
limit 10
Displaying records 1 - 10
| Gorgonzola Telino |
458 |
| Camembert Pierrot |
430 |
| Steeleye Stout |
369 |
| Raclette Courdavault |
346 |
| Chang |
341 |
| Pavlova |
338 |
| Fløtemysost |
336 |
| Alice Mutton |
331 |
| Tarte au sucre |
325 |
| Geitost |
316 |
select products.ProductName, sum(orderdetails.quantity) as top10_products1996 from orderdetails
inner join products on orderdetails.ProductID = products.ProductID
inner join orders on orderdetails.OrderID = orders.OrderID
where orderdate like '%1996%'
group by productname
order by top10_products1996 desc
limit 10
Displaying records 1 - 10
| Gorgonzola Telino |
444 |
| Camembert Pierrot |
370 |
| Steeleye Stout |
274 |
| Chartreuse verte |
266 |
| Fløtemysost |
261 |
| Mozzarella di Giovanni |
260 |
| Pavlova |
252 |
| Tarte au sucre |
250 |
| Alice Mutton |
234 |
| Raclette Courdavault |
231 |
select products.ProductName, sum(orderdetails.quantity) as top10_products1997 from orderdetails
inner join products on orderdetails.ProductID = products.ProductID
inner join orders on orderdetails.OrderID = orders.OrderID
where orderdate like '%1997%'
group by productname
order by top10_products1997 desc
limit 10
Displaying records 1 - 10
| Gnocchi di nonna Alice |
173 |
| Tourtière |
126 |
| Geitost |
119 |
| Raclette Courdavault |
115 |
| Chang |
115 |
| Sirop d’érable |
106 |
| Vegie-spread |
100 |
| Côte de Blaye |
99 |
| Alice Mutton |
97 |
| Steeleye Stout |
95 |