-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.html
More file actions
232 lines (203 loc) · 11.5 KB
/
Copy pathsql.html
File metadata and controls
232 lines (203 loc) · 11.5 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!doctype html>
<html>
<head>
<title>SQL | DB Cheatsheet</title>
<link rel="stylesheet" style="text/css" href="../stylesheets/cheat-sheet.css">
<link href='https://fonts.googleapis.com/css?family=Roboto' rel='stylesheet' type='text/css'>
</head>
<body>
<header>
<h1>Bopes DB Cheatsheet</h1>
<ul>
<li><a href="sql.html">SQL</a></li>
<li><a href="activerecord.html">ActiveRecord</a></li>
<li><a href="../index.html">Home</a></li>
</ul>
</header>
<article>
<h2>Structured Query Language</h2>
<table>
<tr>
<td id="manipulation">
<h3>Manipulation</h3>
<section class="topic" id="create_table">
<h4>Create Table</h4>
<code><span class="command">CREATE TABLE</span> table_name (column1_name data_type, column2_name data_type, etc.);</code>
<p>This creates a new table in your database. You can include as many columns as you want.</p>
<!-- <ul class="sublist">
<li>CREATE TABLE - command that creates the table</li>
<li>table_name - what your new table will be named</li>
<li>column1_name - what the first column in the table will be named</li>
<li>data_type - the data type for that column</li>
</ul> -->
</section>
<section class="topic" id="alter_table">
<h4>Alter Table</h4>
<code><span class="command">ALTER TABLE</span> table <span class="command">ADD COLUMN</span> new_column data_type;</code>
<p>Modify the columns in a table.</p>
</section>
<section class="topic" id="select">
<h4>Select</h4>
<code><span class="command">SELECT</span> column1, column2, etc. <span class="command">FROM</span> table;</code>
<p>This pulls data from a table.</p>
<!-- <ul class="sublist">
<li>SELECT - command to pull data</li>
<li>table - which table to pull from</li>
<li>column1, column2, etc. - which columns you want to pull. You can choose all columns with "*"</li>
<li>FROM - command that chooses which table to pull from</li>
<li>table - which table to pull from</li>
</ul> -->
</section>
<section class="topic" id="select_distinct">
<h4>Select Distinct</h4>
<code><span class="command">SELECT DISTINCT</span> column <span class="command">FROM</span> table;</code>
<p>Select every unique value in a column.</p>
</section>
<section class="topic" id="insert">
<h4>Insert</h4>
<code><span class="command">INSERT INTO</span> table (column1, column2, etc.) <span class="command">VALUES</span> (value1, value2, etc.);</code>
<p>This inserts a row into the specified table.</p>
<!-- <ul class="sublist">
<li>INSERT INTO - command that inserts your new row</li>
<li>table - which table to insert into</li>
<li>column1, column2, etc. - which columns in the table you will be filling in</li>
<li>VALUES - command that precedes new data</li>
<li>value1, value2, etc. - the new data to be inserted. The order corresponds to column1, column2, etc.</li>
</ul> -->
</section>
<section class="topic" id="update">
<h4>Update</h4>
<code><span class="command">UPDATE</span> table <span class="command">SET</span> column1 = new_value <span class="command">WHERE</span> column2 = value;</code>
<p>This updates data in a table. You can change the data in a column for all rows meeting certain criteria.</p>
<!-- <ul class="sublist">
<li>UPDATE - command to update data</li>
<li>table - which table to update</li>
<li>SET - command that precedes the new data</li>
<li>column1 - the column that will be changed</li>
<li>new_value - the value it will be changed to</li>
<li>WHERE - command that finds the rows meeting certain criteria</li>
<li>column2 - the column containing the search criteria</li>
<li>value2 - the search criteria</li>
</ul> -->
</section>
<section class="topic" id="delete_from">
<h4>Delete From</h4>
<code><span class="command">DELETE FROM</span> table <span class="command">WHERE</span> column <span class="command">IS</span> criteria;</code>
<p>Delete rows in a table.</p>
</section>
</td>
<td id="queries">
<h3>Queries</h3>
<section class="topic" id="like">
<h4>Like</h4>
<code><span class="command">SELECT</span> column <span class="command">FROM</span> table <span class="command">WHERE</span> column <span class="command">LIKE</span> criteria;</code>
<p>
Return rows where data in the specified column matches the criteria.<br>
'_' is a search wildcard ('Se_en' returns 'Seven' and 'Se7en'). <br>
'%' is a append wildcard ('d%' returns 'dog,' '%h' returns 'fish,' '%a%' returns 'cat').
</p>
</section>
<section class="topic" id="and">
<h4>And</h4>
<code><span class="command">SELECT</span> column <span class="command">FROM</span> table <span class="command">WHERE</span> column1 = criteria1 <span class="command">AND</span> column 2 = criteria2;</code>
<p>Link multiple search criteria in a single query.</p>
</section>
<section class="topic" id="between">
<h4>Between ... And ...</h4>
<code><span class="command">SELECT</span> column <span class="command">FROM</span> table <span class="command">WHERE</span> column <span class="command">BETWEEN</span> criteria1 <span class="command">AND</span> criteria2;</code>
<p>Search for rows with values between two boundary values.</p>
</section>
<section class="topic" id="or">
<h4>Or</h4>
<code><span class="command">SELECT</span> column <span class="command">FROM</span> table <span class="command">WHERE</span> column1 = criteria1 <span class="command">OR</span> column2 = criteria2;</code>
<p>Search for rows that satisfy at least one of a given OR statement.</p>
</section>
<section class="topic" id="order_by">
<h4>Order by</h4>
<code><span class="command">SELECT</span> column <span class="command">FROM</span> table <span class="command">ORDER BY</span> criteria <span class="command">ASC</span>;</code>
<p>Return the columns, ordered either ascending (smallest to largest, ASC) or descending (largest to smallest, DESC).</p>
</section>
<section class="topic" id="limit">
<h4>Limit</h4>
<code><span class="command">SELECT</span> column <span class="command">FROM</span> table <span class="command">LIMIT</span> number;</code>
<p>Return up to the limit rows meeting your search criteria.</p>
</section>
</td>
</tr>
<tr>
<td id="aggregate_functions">
<h3>Aggregate Functions</h3>
<section class="topic" id="count">
<h4>Count</h4>
<code><span class="command">SELECT COUNT(</span>column<span class="command">) FROM</span> table;</code>
<p>Return a count of rows with column =/= NULL. To see all rows, set column = * </p>
</section>
<section class="topic" id="group_by">
<h4>Group By</h4>
<code><span class="command">SELECT</span> column1, <span class="command">FUNCTION(</span>column2<span class="command">) FROM</span> table <span class="command">GROUP BY</span> column1;</code>
<p>Groups the results by the distinct values in column1. Works best with aggregator functions.</p>
</section>
<section class="topic" id="sum">
<h4>Sum</h4>
<code><span class="command">SELECT SUM(</span>column<span class="command">) FROM</span> table;</code>
<p>Aggregates sum for the specified column.</p>
</section>
<section class="topic" id="max">
<h4>Max</h4>
<code><span class="command">SELECT MAX(</span>column<span class="command">) FROM</span> table;</code>
<p>Finds the maximum value within the specified column.</p>
</section>
<section class="topic" id="min">
<h4>Min</h4>
<code><span class="command">SELECT MIN(</span>column<span class="command">) FROM</span> table;</code>
<p>Finds the minimum value within the specified column.</p>
</section>
<section class="topic" id="average">
<h4>Average</h4>
<code><span class="command">SELECT AVG(</span>column<span class="command">) FROM</span> table;</code>
<p>Finds the average value of a specified column.</p>
</section>
<section class="topic" id="round">
<h4>Round</h4>
<code><span class="command">SELECT ROUND(</span>column, decimal_places<span class="command">) FROM</span> table;</code>
<p>Rounds the given output to the stated number of decimal places. If decimal_places is excluded, it will round to the nearest integer.</p>
</section>
</td>
<td id="multiple_tables">
<h3>Multiple Tables</h3>
<section class="topic" id="primary_key">
<h4>Primary Key</h4>
<code><span class="command">CREATE TABLE</span> table_name (id <span class="command">INTEGER PRIMARY KEY</span>, column2_name data_type2, etc.);</code>
<p>The primary key acts as the id value for the record. Each value in this column will be unique, and no value will be NULL.</p>
</section>
<section class="topic" id="inner_join">
<h4>Inner Join</h4>
<code><span class="command">SELECT</span> table1.column, table2.column <br>
<span class="command">FROM</span> table1 <br>
<span class="command">JOIN</span> table2 <span class="command">ON</span> table1.foreign_key = table2.primary_key;</code>
<p>The inner join returns only the records from table1 that join to table2.</p>
</section>
<section class="topic" id="left_outer_join">
<h4>Left Outer Join</h4>
<code><span class="command">SELECT</span> table1.column, table2.column <br>
<span class="command">FROM</span> table1 <br>
<span class="command">LEFT JOIN</span> table2 <span class="command">ON</span> table1.foreign_key = table2.primary_key;</code>
<p>The outer join returns all records from table1. If they join to table2, it will show the joined data. If they don't join, it will return NULL in the joined columns.</p>
</section>
<section class="topic" id="alias">
<h4>Alias</h4>
<code><span class="command">SELECT</span> <br>
table1.column <span class="command">AS</span> alias1, <br>
table2.column <span class="command">AS</span> alias2 <br>
<span class="command">FROM</span> table1 <br>
<span class="command">JOIN</span> table2 <span class="command">ON</span> table1.foreign_key = table2.primary_key;</code>
<p>Use AS to create a new alias for each column in the query results.</p>
</section>
</td>
</tr>
</table>
</article>
<footer>
</footer>
</body>
</html>