How to execute SQL queries in R

This recipe helps you execute SQL queries in R

Recipe Objective

How to execute SQL queries in R?

SQL stands for Structured Query Language. SQL is a database language which can store, create, retrieve and maintain data in the databases. Various database systems like Oracle, MySQL, MS Access, etc. use SQL as the standard database language. SQL language can create, tables in the databases and queries are written in order to access the details in the tables. This example uses the "sqldf" package for executing SQL queries in R. This recipe demonstrates an example of how to execute SQL queries in R.

Step 1 - Install necessary package

install.packages("sqldf") library(sqldf)

Step 2 - Create a dataframe

dt <- data.frame( emp_id = c('X1','X2','X3','X4','X5','X6','X7'), emp_name = c("A","B","C","D","E","F","G"), emp_salary = c(10000,50000,30000,75000,30000,45000,65000)) print(dt)

 "Dataframe is:" 
  emp_id emp_name emp_salary
1     X1        A      10000
2     X2        B      50000
3     X3        C      30000
4     X4        D      75000
5     X5        E      30000
6     X6        F      45000
7     X7        G      65000

Step 3 - Write SQL queries

a <- sqldf("select * from dt where emp_salary >= 50000") print(a)

 "Output of the query is :" 
  emp_id emp_name emp_salary
1     X2        B      50000
2     X4        D      75000
3     X7        G      65000

b <- sqldf("select * from dt limit 3") print(b)

 "Output of the query is :" 
  emp_id emp_name emp_salary
1     X1        A      10000
2     X2        B      50000
3     X3        C      30000

{"mode":"full","isActive":false}

What Users are saying..

profile image

Ray han

Tech Leader | Stanford / Yale University
linkedin profile url

I think that they are fantastic. I attended Yale and Stanford and have worked at Honeywell,Oracle, and Arthur Andersen(Accenture) in the US. I have taken Big Data and Hadoop,NoSQL, Spark, Hadoop... Read More

Relevant Projects

NLP Project for Multi Class Text Classification using BERT Model
In this NLP Project, you will learn how to build a multi-class text classification model using using the pre-trained BERT model.

Digit Recognition using CNN for MNIST Dataset in Python
In this deep learning project, you will build a convolutional neural network using MNIST dataset for handwritten digit recognition.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Classification Projects on Machine Learning for Beginners - 1
Classification ML Project for Beginners - A Hands-On Approach to Implementing Different Types of Classification Algorithms in Machine Learning for Predictive Modelling

Recommender System Machine Learning Project for Beginners-4
Collaborative Filtering Recommender System Project - Comparison of different model based and memory based methods to build recommendation system using collaborative filtering.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Build Regression (Linear,Ridge,Lasso) Models in NumPy Python
In this machine learning regression project, you will learn to build NumPy Regression Models (Linear Regression, Ridge Regression, Lasso Regression) from Scratch.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.