-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlTutorial.R
More file actions
62 lines (42 loc) · 1.56 KB
/
SqlTutorial.R
File metadata and controls
62 lines (42 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#install.packages("RODBC") #installing the package RODBC
library(RODBC) #loading the package RODBC
odbcDataSources() # to check all available ODBC data sources
#creating a Database connection
# for username,password,database name and DSN name
chan=odbcConnect("PostgreSQL30","postgres;Password=12345;Database=datascience")
#to list all table names
sqlTables(chan)
#and fetch some data
diamond <- sqlFetch(chan,"diamond")
library(sqldf)
sqldf("select * From diamond")
sqldf("Select carat, colour From diamond ")
sqldf("Select Distinct colour, carat From diamond")
sqldf("Select Count(Distinct colour) From diamond")
sqldf("Select Price from diamond
where colour == 'D'")
sqldf("Select Price, Colour From diamond
where carat <= 0.3")
sqldf("Select Count (Distinct Price) From diamond
where carat <= 0.3")
sqldf("Select Price,Colour,Carat from diamond
where carat = 0.1 OR colour = 'D'")
sqldf("Select Price,Colour,Carat from diamond
where carat = 1.0 AND colour = 'D'")
sqldf("Select Distinct(Colour) from diamond
where Not colour = 'D'")
sqldf("Select * from diamond
where colour = 'D' AND carat == 1
Order By Price ASC, carat DESC")
# Insert Not Working
sqldf("Insert Into diamond (carat, colour, clarity, certification, price)
values (1.00, 'A', 'XXX', 'ABC', 3)")
sqldf("Select * from diamond
where colour = 'A'")
# DELAING WITH NULL VALUE
sqldf("SELECT * From diamond
Where carat Is Not Null")
# Update
sqldf("Update diamond
Set colour = 'A', carat = 9
Where Colour = 'D' AND carat == 1.00")