Performing More SQL queries
Make sure to perform the first 2 steps to build the database connection.
Using table ‘orderdetails’ in the database
R
query <- 'select * from orderdetails' ; result <- dbGetQuery (connection,query) print (result) |
Output:
orderNumber productCode quantityOrdered priceEach orderLineNumber
1 10100 S18_1749 30 136.00 3
2 10100 S18_2248 50 55.09 2
3 10100 S18_4409 22 75.46 4
4 10100 S24_3969 49 35.29 1
5 10101 S18_2325 25 108.06 4
6 10101 S18_2795 26 167.06 1
7 10101 S24_1937 45 32.53 3
8 10101 S24_2022 46 44.35 2
9 10102 S18_1342 39 95.55 2
10 10102 S18_1367 41 43.13 1
Calculating unique Order line numbers from table orderdetails
R
query <- 'select count(distinct orderLineNumber) from orderdetails' ; result <- dbGetQuery (connection,query) print (result) |
Output:
count(distinct orderLineNumber)
1 18
Close the Database Connection
R
dbDisconnect (connection) |
Output:
[1] TRUE
SQL Database Access using R DBI
DBI library in R programming is used for intеracting with different types of database systems such as MySQL for different types of professional work like data analysis using R language. Wе can еasily connect to the database, run queries and retrieve results from the database in the R еnvironmеnt with the DBI library.
Table of Content
- SQL (Structured Query Language) Databases
- How to access SQL Database using the DBI package
- Performing More SQL queries
- Benefits and Limitations of accessing SQL Database using R DBI package
Contact Us