|
| 1 | +## MySQL Basics |
| 2 | + |
| 3 | +### ALTER TABLE |
| 4 | + |
| 5 | +``` |
| 6 | +ALTER TABLE table_name ADD column datatype; |
| 7 | +``` |
| 8 | + |
| 9 | +`ALTER TABLE` lets you add columns to a table in a database. |
| 10 | + |
| 11 | +### AND |
| 12 | + |
| 13 | +``` |
| 14 | +SELECT column_name(s) |
| 15 | +FROM table_name |
| 16 | +WHERE column_1 = value_1 |
| 17 | +AND column_2 = value_2; |
| 18 | +``` |
| 19 | + |
| 20 | +`AND` is an operator that combines two conditions. Both conditions must be true for the row to be included in the result set. |
| 21 | + |
| 22 | +### AS |
| 23 | + |
| 24 | +``` |
| 25 | +SELECT column_name AS 'Alias' |
| 26 | +FROM table_name; |
| 27 | +``` |
| 28 | + |
| 29 | +`AS` is a keyword in SQL that allows you to rename a column or table using an *alias*. |
| 30 | + |
| 31 | +### AVG |
| 32 | + |
| 33 | +``` |
| 34 | +SELECT AVG(column_name) |
| 35 | +FROM table_name; |
| 36 | +
|
| 37 | +``` |
| 38 | + |
| 39 | +`AVG()` is an aggregate function that returns the average value for a numeric column. |
| 40 | + |
| 41 | +### BETWEEN |
| 42 | + |
| 43 | +``` |
| 44 | +SELECT column_name(s) |
| 45 | +FROM table_name |
| 46 | +WHERE column_name BETWEEN value_1 AND value_2; |
| 47 | +``` |
| 48 | + |
| 49 | +The `BETWEEN` operator is used to filter the result set within a certain range. The values can be numbers, text or dates. |
| 50 | + |
| 51 | +### COUNT |
| 52 | + |
| 53 | +``` |
| 54 | +SELECT COUNT(column_name) |
| 55 | +FROM table_name; |
| 56 | +``` |
| 57 | + |
| 58 | +`COUNT()` is a function that takes the name of a column as an argument and counts the number of rows where the column is not `NULL`. |
| 59 | + |
| 60 | +### CREATE TABLE |
| 61 | + |
| 62 | +``` |
| 63 | +CREATE TABLE table_name (column_1 datatype, column_2 datatype, column_3 datatype); |
| 64 | +``` |
| 65 | + |
| 66 | +`CREATE TABLE` creates a new table in the database. It allows you to specify the name of the table and the name of each column in the table. |
| 67 | + |
| 68 | +### DELETE |
| 69 | + |
| 70 | +``` |
| 71 | +DELETE FROM table_name WHERE some_column = some_value; |
| 72 | +``` |
| 73 | + |
| 74 | +`DELETE` statements are used to remove rows from a table. |
| 75 | + |
| 76 | +### GROUP BY |
| 77 | + |
| 78 | +``` |
| 79 | +SELECT COUNT(*) |
| 80 | +FROM table_name |
| 81 | +GROUP BY column_name; |
| 82 | +``` |
| 83 | + |
| 84 | +`GROUP BY` is a clause in SQL that is only used with aggregate functions. It is used in collaboration with the `SELECT` statement to arrange identical data into groups. |
| 85 | + |
| 86 | +### INNER JOIN |
| 87 | + |
| 88 | +``` |
| 89 | +SELECT column_name(s) FROM table_1 |
| 90 | +JOIN table_2 |
| 91 | +ON table_1.column_name = table_2.column_name; |
| 92 | +``` |
| 93 | + |
| 94 | +An inner join will combine rows from different tables if the *join condition* is true. |
| 95 | + |
| 96 | +### INSERT |
| 97 | + |
| 98 | +``` |
| 99 | +INSERT INTO table_name (column_1, column_2, column_3) VALUES (value_1, 'value_2', value_3); |
| 100 | +``` |
| 101 | + |
| 102 | +`INSERT` statements are used to add a new row to a table. |
| 103 | + |
| 104 | +### LIKE |
| 105 | + |
| 106 | +``` |
| 107 | +SELECT column_name(s) |
| 108 | +FROM table_name |
| 109 | +WHERE column_name LIKE pattern; |
| 110 | +``` |
| 111 | + |
| 112 | +`LIKE` is a special operator used with the `WHERE` clause to search for a specific pattern in a column. |
| 113 | + |
| 114 | +### LIMIT |
| 115 | + |
| 116 | +``` |
| 117 | +SELECT column_name(s) |
| 118 | +FROM table_name |
| 119 | +LIMIT number; |
| 120 | +``` |
| 121 | + |
| 122 | +`LIMIT` is a clause that lets you specify the maximum number of rows the result set will have. |
| 123 | + |
| 124 | +### MAX |
| 125 | + |
| 126 | +``` |
| 127 | +SELECT MAX(column_name) |
| 128 | +FROM table_name; |
| 129 | +``` |
| 130 | + |
| 131 | +`MAX()` is a function that takes the name of a column as an argument and returns the largest value in that column. |
| 132 | + |
| 133 | +### MIN |
| 134 | + |
| 135 | +``` |
| 136 | +SELECT MIN(column_name) |
| 137 | +FROM table_name; |
| 138 | +``` |
| 139 | + |
| 140 | +`MIN()` is a function that takes the name of a column as an argument and returns the smallest value in that column. |
| 141 | + |
| 142 | +### OR |
| 143 | + |
| 144 | +``` |
| 145 | +SELECT column_name |
| 146 | +FROM table_name |
| 147 | +WHERE column_name = value_1 |
| 148 | +OR column_name = value_2; |
| 149 | +``` |
| 150 | + |
| 151 | +`OR` is an operator that filters the result set to only include rows where either condition is true. |
| 152 | + |
| 153 | +### ORDER BY |
| 154 | + |
| 155 | +``` |
| 156 | +SELECT column_name |
| 157 | +FROM table_name |
| 158 | +ORDER BY column_name ASC|DESC; |
| 159 | +``` |
| 160 | + |
| 161 | +`ORDER BY` is a clause that indicates you want to sort the result set by a particular column either alphabetically or numerically. |
| 162 | + |
| 163 | +### OUTER JOIN |
| 164 | + |
| 165 | +``` |
| 166 | +SELECT column_name(s) FROM table_1 |
| 167 | +LEFT JOIN table_2 |
| 168 | +ON table_1.column_name = table_2.column_name; |
| 169 | +``` |
| 170 | + |
| 171 | +An outer join will combine rows from different tables even if the the join condition is not met. Every row in the *left* table is returned in the result set, and if the join condition is not met, then `NULL` values are used to fill in the columns from the *right* table. |
| 172 | + |
| 173 | +### ROUND |
| 174 | + |
| 175 | +``` |
| 176 | +SELECT ROUND(column_name, integer) |
| 177 | +FROM table_name; |
| 178 | +``` |
| 179 | + |
| 180 | +`ROUND()` is a function that takes a column name and an integer as an argument. It rounds the values in the column to the number of decimal places specified by the integer. |
| 181 | + |
| 182 | +### SELECT |
| 183 | + |
| 184 | +``` |
| 185 | +SELECT column_name FROM table_name; |
| 186 | +``` |
| 187 | + |
| 188 | +`SELECT` statements are used to fetch data from a database. Every query will begin with SELECT. |
| 189 | + |
| 190 | +### SELECT DISTINCT |
| 191 | + |
| 192 | +``` |
| 193 | +SELECT DISTINCT column_name FROM table_name; |
| 194 | +``` |
| 195 | + |
| 196 | +`SELECT DISTINCT` specifies that the statement is going to be a query that returns unique values in the specified column(s). |
| 197 | + |
| 198 | +### SUM |
| 199 | + |
| 200 | +``` |
| 201 | +SELECT SUM(column_name) |
| 202 | +FROM table_name; |
| 203 | +``` |
| 204 | + |
| 205 | +`SUM()` is a function that takes the name of a column as an argument and returns the sum of all the values in that column. |
| 206 | + |
| 207 | +### UPDATE |
| 208 | + |
| 209 | +``` |
| 210 | +UPDATE table_name |
| 211 | +SET some_column = some_value |
| 212 | +WHERE some_column = some_value; |
| 213 | +``` |
| 214 | + |
| 215 | +`UPDATE` statments allow you to edit rows in a table. |
| 216 | + |
| 217 | +### WHERE |
| 218 | + |
| 219 | +``` |
| 220 | +SELECT column_name(s) |
| 221 | +FROM table_name |
| 222 | +WHERE column_name operator value; |
| 223 | +``` |
| 224 | + |
| 225 | +`WHERE` is a clause that indicates you want to filter the result set to include only rows where the following *condition* is true. |
0 commit comments