This demo will use the iris dataset.
The SELECT statement is used to retrieve data from a table. In its simplest form, without a table, it will return what you pass it:
sqldf("
SELECT 1 + 5;
")
## 1 + 5
## 1 6
As a general rule, you will want to SELECT data FROM a table. If no table is present then, for MySQL, it is good practice to use DUAL as a dummy table. If you do have a table, an individual column (field) can be selected. Selecting the Species column from the iris dataset:
sqldf("
SELECT Species FROM iris;
")
## Species
## 1 setosa
## 2 setosa
## 3 setosa
## 4 setosa
## 5 setosa
## 6 setosa
## 7 setosa
## 8 setosa
## 9 setosa
## 10 setosa
## 11 setosa
## 12 setosa
## 13 setosa
## 14 setosa
## 15 setosa
## 16 setosa
## 17 setosa
## 18 setosa
## 19 setosa
## 20 setosa
## 21 setosa
## 22 setosa
## 23 setosa
## 24 setosa
## 25 setosa
## 26 setosa
## 27 setosa
## 28 setosa
## 29 setosa
## 30 setosa
## 31 setosa
## 32 setosa
## 33 setosa
## 34 setosa
## 35 setosa
## 36 setosa
## 37 setosa
## 38 setosa
## 39 setosa
## 40 setosa
## 41 setosa
## 42 setosa
## 43 setosa
## 44 setosa
## 45 setosa
## 46 setosa
## 47 setosa
## 48 setosa
## 49 setosa
## 50 setosa
## 51 versicolor
## 52 versicolor
## 53 versicolor
## 54 versicolor
## 55 versicolor
## 56 versicolor
## 57 versicolor
## 58 versicolor
## 59 versicolor
## 60 versicolor
## 61 versicolor
## 62 versicolor
## 63 versicolor
## 64 versicolor
## 65 versicolor
## 66 versicolor
## 67 versicolor
## 68 versicolor
## 69 versicolor
## 70 versicolor
## 71 versicolor
## 72 versicolor
## 73 versicolor
## 74 versicolor
## 75 versicolor
## 76 versicolor
## 77 versicolor
## 78 versicolor
## 79 versicolor
## 80 versicolor
## 81 versicolor
## 82 versicolor
## 83 versicolor
## 84 versicolor
## 85 versicolor
## 86 versicolor
## 87 versicolor
## 88 versicolor
## 89 versicolor
## 90 versicolor
## 91 versicolor
## 92 versicolor
## 93 versicolor
## 94 versicolor
## 95 versicolor
## 96 versicolor
## 97 versicolor
## 98 versicolor
## 99 versicolor
## 100 versicolor
## 101 virginica
## 102 virginica
## 103 virginica
## 104 virginica
## 105 virginica
## 106 virginica
## 107 virginica
## 108 virginica
## 109 virginica
## 110 virginica
## 111 virginica
## 112 virginica
## 113 virginica
## 114 virginica
## 115 virginica
## 116 virginica
## 117 virginica
## 118 virginica
## 119 virginica
## 120 virginica
## 121 virginica
## 122 virginica
## 123 virginica
## 124 virginica
## 125 virginica
## 126 virginica
## 127 virginica
## 128 virginica
## 129 virginica
## 130 virginica
## 131 virginica
## 132 virginica
## 133 virginica
## 134 virginica
## 135 virginica
## 136 virginica
## 137 virginica
## 138 virginica
## 139 virginica
## 140 virginica
## 141 virginica
## 142 virginica
## 143 virginica
## 144 virginica
## 145 virginica
## 146 virginica
## 147 virginica
## 148 virginica
## 149 virginica
## 150 virginica
That is a lot of output! This is where the LIMIT statement comes in. You can LIMIT the number of entries returned:
sqldf("
SELECT Species FROM iris LIMIT 5;
")
## Species
## 1 setosa
## 2 setosa
## 3 setosa
## 4 setosa
## 5 setosa
Multiple columns can be selected at once but the same number of entries must be returned for each. When a field name contains a “.” the name must be surrounded by `, ” or []:
sqldf("
SELECT `Petal.Length`, Species FROM iris LIMIT 8;
")
## Petal.Length Species
## 1 1.4 setosa
## 2 1.4 setosa
## 3 1.3 setosa
## 4 1.5 setosa
## 5 1.4 setosa
## 6 1.7 setosa
## 7 1.4 setosa
## 8 1.5 setosa