├── README.md └── SQL_commands.md /README.md: -------------------------------------------------------------------------------- 1 | # SQL cheatsheet 2 | 3 | #### This is note taken from the [SQL course on Codecademy](https://www.codecademy.com/learn/learn-sql). 4 | 5 | ## Manipulation 6 | 7 | ```sql 8 | CREATE TABLE celebs (id INTEGER, name TEXT, age INTEGER); 9 | ``` 10 | 11 | This `CREATE` statement creates a new table in the database named `celebs`. You can use the `CREATE` statement anytime you want to create a new table from scratch. 12 | 13 | 1. `CREATE TABLE` is a clause that tells SQL you want to create a new table. 14 | 15 | 16 | 2. `celebs` is the name of the table. 17 | 18 | 19 | 3. `(id INTEGER, name TEXT, age INTEGER)`is a list of parameters defining each column in the table and its data type. 20 | 21 | - `id` is the first column in the table. It stores values of data type `INTEGER` 22 | - `name` is the second column in the table. It stores values of data type `TEXT` 23 | - `age` is the third column in the table. It stores values of data type `INTEGER` 24 | 25 | 26 | 27 | Add a row to the table. In the code editor type 28 | 29 | ```sql 30 | INSERT INTO celebs (id, name, age) VALUES (1, 'Justin Bieber', 21); 31 | ``` 32 | 33 | To view the row you just created, under the `INSERT` statement type `SELECT * FROM celebs;`. 34 | 35 | This `INSERT` statement inserts new rows into a table. You can use the `INSERT` statement when you want to add new records. 36 | 37 | 1. `INSERT INTO` is a clause that adds the specified row or rows. 38 | 2. `celebs` is the name of the table the row is added to. 39 | 3. `(id, name, age)` is a parameter identifying the columns that data will be inserted into. 40 | 4. `VALUES` is a clause that indicates the data being inserted. 41 | `(1, 'Justin Bieber', 21)` is a parameter identifying the values being inserted. 42 | 43 | - `1` is an integer that will be inserted into the `id` column 44 | - `'Justin Bieber'` is text that will be inserted into the `name` column 45 | - `21` is an integer that will be inserted into the `age` column 46 | 47 | 48 | 49 | ```sql 50 | SELECT name FROM celebs; 51 | ``` 52 | 53 | `SELECT` statements are used to fetch data from a database. Here, `SELECT` returns all data in the `name` column of the `celebs` table. 54 | 55 | 1. `SELECT` is a clause that indicates that the statement is a query. You will use `SELECT` every time you query data from a database. 56 | 2. `name` specifies the column to query data from. 57 | 3. `FROM celebs` specifies the name of the table to query data from. In this statement, data is queried from the `celebs` table. 58 | 59 | You can also query data from all columns in a table with `SELECT`. 60 | 61 | ```sql 62 | SELECT * FROM celebs; 63 | ``` 64 | 65 | `*` is a special wildcard character that we have been using. It allows you to select every column in a table without having to name each one individually. Here, the result set contains every column in the `celebs` table. 66 | 67 | `SELECT` statements always return a new table called the *result set*. 68 | 69 | 70 | 71 | ```sql 72 | UPDATE celebs 73 | SET age = 22 74 | WHERE id = 1; 75 | ``` 76 | 77 | The `UPDATE` statement edits a row in the table. You can use the `UPDATE` statement when you want to change existing records. 78 | 79 | 1. `UPDATE` is a clause that edits a row in the table. 80 | 2. `celebs` is the name of the table. 81 | 3. `SET` is a clause that indicates the column to edit. 82 | 83 | - `age` is the name of the column that is going to be updated 84 | - `22` is the new value that is going to be inserted into the `age` column. 85 | 86 | 4. `WHERE` is a clause that indicates which row(s) to update with the new column value. Here the row with a `1` in the `id` column is the row that will have the `age` updated to `22`. 87 | 88 | 89 | 90 | ```sql 91 | ALTER TABLE celebs ADD COLUMN twitter_handle TEXT; 92 | ``` 93 | 94 | The `ALTER TABLE` statement added a new column to the table. You can use this command when you want to add columns to a table. 95 | 96 | 1. `ALTER TABLE` is a clause that lets you make the specified changes. 97 | 2. `celebs` is the name of the table that is being changed. 98 | 3. `ADD COLUMN` is a clause that lets you add a new column to a table. 99 | 100 | - `twitter_handle` is the name of the new column being added 101 | - `TEXT` is the data type for the new column 102 | 103 | 4. `NULL` is a special value in SQL that represents missing or unknown data. Here, the rows that existed before the column was added have `NULL`values for `twitter_handle`. 104 | 105 | 106 | 107 | ```sql 108 | DELETE FROM celebs WHERE twitter_handle IS NULL; 109 | ``` 110 | 111 | The `DELETE FROM` statement deletes one or more rows from a table. You can use the statement when you want to delete existing records. 112 | 113 | 1. `DELETE FROM` is a clause that lets you delete rows from a table. 114 | 2. `celebs` is the name of the table we want to delete rows from. 115 | 3. `WHERE` is a clause that lets you select which rows you want to delete. Here we want to delete all of the rows where the twitter_handle column `IS NULL`. 116 | 4. `IS NULL` is a condition in SQL that returns true when the value is `NULL` and false otherwise. 117 | 118 | 119 | 120 | ## Queries 121 | 122 | ### Database Schema 123 | 124 | | movies220 rows | | 125 | | -------------- | ------- | 126 | | id | INTEGER | 127 | | name | TEXT | 128 | | genre | TEXT | 129 | | year | INTEGER | 130 | | imdb_rating | REAL | 131 | 132 | 133 | 134 | ```sql 135 | SELECT name, imdb_rating FROM movies; 136 | ``` 137 | 138 | In Lesson 1 you learned that `SELECT` is used every time you want to query data from a database. 139 | 140 | Multiple columns can be queried at once by separating column names with a comma. By specifying `name, imdb_rating`, the result set contains a `name` and `imdb_rating` column. 141 | 142 | 143 | 144 | ```sql 145 | SELECT DISTINCT genre FROM movies; 146 | ``` 147 | 148 | `SELECT DISTINCT` is used to return unique values in the result set. It filters out all duplicate values. Here, the result set lists each genre in the `movies` table exactly once. 149 | 150 | 1. `SELECT DISTINCT` specifies that the statement is going to be a query that returns unique values in the specified column(s) 151 | 152 | 2. `genre` is the name of the column to display in the result set. 153 | 154 | 3. `FROM movies` indicates the table name to query from. 155 | 156 | Filtering the results of a query is an important skill in SQL. It is easier to see the different possible genres a movie can have after the data has been filtered, than to scan every row in the table. 157 | 158 | The rest of this lesson will teach you different commands in SQL to filter the results of a query. 159 | 160 | 161 | 162 | ```sql 163 | SELECT * FROM movies 164 | WHERE imdb_rating > 8; 165 | ``` 166 | 167 | This statement filters the result set to only include movies with IMDb ratings greater than 8. How does it work? 168 | 169 | 1. `WHERE` is a clause that indicates you want to filter the result set to include only rows where the following *condition* is true. 170 | 171 | 2. `imdb_rating > 8` is a condition that filters the result set. Here, only rows with a value greater than 8 in the `imdb_rating` column will be returned in the result set. 172 | 173 | 3. `>` is an *operator*. Operators create a condition that can be evaluated as either true or false. Common operators used with the `WHERE` clause are: 174 | 175 | - `=` equals 176 | - `!=` not equals 177 | - `>` greater than 178 | - `<` less than 179 | - `>=` greater than or equal to 180 | - `<=` less than or equal to 181 | 182 | There are also some special operators that we will learn more about in the upcoming exercises. 183 | 184 | 185 | 186 | ```sql 187 | SELECT * FROM movies 188 | WHERE name LIKE 'Se_en'; 189 | ``` 190 | 191 | `LIKE` can be a useful operator when you want to compare similar values. Here, we are comparing two movies with the same name but are spelled differently. 192 | 193 | 1. `LIKE` is a special operator used with the `WHERE` clause to search for a specific pattern in a column. 194 | 195 | 2. `name LIKE Se_en` is a condition evaluating the `name` column for a specific pattern. 196 | 197 | 3. `Se_en` represents a pattern with a *wildcard* character. The `_` means you can substitute any individual character here without breaking the pattern. The names `Seven` and `Se7en` both match this pattern. 198 | 199 | `%` is another wildcard character that can be used with `LIKE`. We will learn more about `%` in the next exercise. 200 | 201 | 202 | 203 | ```sql 204 | SELECT * FROM movies 205 | WHERE name LIKE 'A%'; 206 | ``` 207 | 208 | This statement filters the result set to only include movies with names that begin with the letter "A" 209 | 210 | `%` is a wildcard character that matches zero or more missing letters in the pattern. 211 | 212 | - `A%` matches all movies with names that begin with "A" 213 | - `%a` matches all movies that end with "a" 214 | 215 | ```sql 216 | SELECT * FROM movies WHERE name LIKE '%man%'; 217 | ``` 218 | 219 | You can use `%` both before and after a pattern. Here, any movie that contains the word "man" in its name will be returned in the result set. Notice, that `LIKE` is not case sensitive. "Batman" and "Man Of Steel" both appear in the result set. 220 | 221 | 222 | 223 | The `BETWEEN` operator is used to filter the result set within a certain range. The values can be numbers, text or dates. 224 | 225 | ```sql 226 | SELECT * FROM movies 227 | WHERE name BETWEEN 'A' AND 'J'; 228 | ``` 229 | 230 | This statement filters the result set to only include movies with `name`s that begin with letters "A" up to but not including "J". 231 | 232 | ```sql 233 | SELECT * FROM movies WHERE year BETWEEN 1990 AND 2000; 234 | ``` 235 | 236 | In this statement, the `BETWEEN` operator is being used to filter the result set to only include movies with `year`s between 1990 up to and including 2000. 237 | 238 | 239 | 240 | ```sql 241 | SELECT * FROM movies 242 | WHERE year BETWEEN 1990 and 2000 243 | AND genre = 'comedy'; 244 | ``` 245 | 246 | Sometimes you want to combine multiple conditions in a `WHERE` clause to make the result set more specific and useful. One way of doing this is to use the `AND` operator. 247 | 248 | 1. `year BETWEEN 1990 and 2000` is the first condition in the `WHERE` clause. 249 | 250 | 251 | 2. `AND genre = 'comedy'` is the second condition in the `WHERE` clause. 252 | 253 | 3. `AND` is an operator that combines two conditions. Both conditions must be true for the row to be included in the result set. Here, we use the `AND` operator to only return movies made between 1990 and 2000 that are also comedies. 254 | 255 | 256 | 257 | ```sql 258 | SELECT * FROM movies 259 | WHERE genre = 'comedy' 260 | OR year < 1980; 261 | ``` 262 | 263 | The `OR` operator can also be used to combine more than one condition in a `WHERE` clause. The `OR` operator evaluates each condition separately and if any of the conditions are true then the row is added to the result set. 264 | 265 | 1. `WHERE genre = 'comedy'` is the first condition in the `WHERE` clause. 266 | 267 | 2. `OR year < 1980` is the second condition in the `WHERE` clause. 268 | 269 | 3. `OR` is an operator that filters the result set to only include rows where either condition is true. Here, we return movies that either have a genre of comedy or were released before 1980. 270 | 271 | 272 | 273 | ```sql 274 | SELECT * FROM movies 275 | ORDER BY imdb_rating DESC; 276 | ``` 277 | 278 | You can sort the results of your query using `ORDER BY`. Sorting the results often makes the data more useful and easier to analyze. 279 | 280 | 1. `ORDER BY` is a clause that indicates you want to sort the result set by a particular column either alphabetically or numerically. 281 | 282 | 2. `imdb_rating` is the name of the column that will be sorted. 283 | 284 | 3. `DESC` is a keyword in SQL that is used with `ORDER BY` to sort the results in *descending order* (high to low or Z-A). Here, it sorts all of the movies from highest to lowest by their IMDb rating. 285 | 286 | It is also possible to sort the results in *ascending order*. `ASC` is a keyword in SQL that is used with `ORDER BY` to sort the results in ascending order (low to high or A-Z). 287 | 288 | 289 | 290 | ```sql 291 | SELECT * FROM movies 292 | ORDER BY imdb_rating DESC 293 | LIMIT 3; 294 | ``` 295 | 296 | Sometimes even filtered results can return thousands of rows in large databases. In these situations it becomes important to cap the number of rows in a result set. 297 | 298 | `LIMIT` is a clause that lets you specify the maximum number of rows the result set will have. Here, we specify that the result set can not have more than three rows. 299 | 300 | 301 | 302 | ## Aggregrate Functions 303 | 304 | ### Database Schema 305 | 306 | | fake_apps200 rows | | 307 | | ----------------- | ------- | 308 | | id | INTEGER | 309 | | name | TEXT | 310 | | category | TEXT | 311 | | downloads | INTEGER | 312 | | price | REAL | 313 | 314 | 315 | 316 | ```sql 317 | SELECT COUNT(*) FROM fake_apps; 318 | ``` 319 | 320 | The fastest way to calculate the number of rows in a table is to use the `COUNT()` function. 321 | 322 | `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`. Here, we want to count every row so we pass `*` as an argument. 323 | 324 | 325 | 326 | ```sql 327 | SELECT price, COUNT(*) FROM fake_apps 328 | GROUP BY price; 329 | ``` 330 | 331 | Aggregate functions are more useful when they organize data into groups. 332 | 333 | `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. 334 | 335 | Here, our aggregate function is `COUNT()` and we are passing `price` as an argument to `GROUP BY`. SQL will count the total number of apps for each `price` in the table. 336 | 337 | It is usually helpful to `SELECT` the column you pass as an argument to `GROUP BY`. Here we select `price` and `COUNT(*)`. You can see that the result set is organized into two columns making it easy to see the number of apps at each price. 338 | 339 | Count the total number of apps at each price that have been downloaded more than 20,000 times. 340 | 341 | ```sql 342 | SELECT price, COUNT(*) FROM fake_apps WHERE downloads > 20000 GROUP BY price; 343 | ``` 344 | 345 | 346 | 347 | ```sql 348 | SELECT SUM(downloads) FROM fake_apps; 349 | ``` 350 | 351 | SQL makes it easy to add all values in a particular column using `SUM()`. 352 | 353 | `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. Here, it adds all the values in the `downloads` column. 354 | 355 | 356 | 357 | ```sql 358 | SELECT MAX(downloads) FROM fake_apps; 359 | ``` 360 | 361 | You can find the largest value in a column by using `MAX()`. 362 | 363 | `MAX()` is a function that takes the name of a column as an argument and returns the largest value in that column. Here, we pass `downloads`as an argument so it will return the largest value in the `downloads` column. 364 | 365 | 366 | 367 | Return the names of the most downloaded apps in each category. 368 | 369 | ```sql 370 | SELECT name, category, MAX(downloads) FROM fake_apps GROUP BY category; 371 | ``` 372 | 373 | 374 | 375 | ```sql 376 | SELECT MIN(downloads) FROM fake_apps; 377 | ``` 378 | 379 | Similar to `MAX()`, SQL also makes it easy to return the smallest value in a column by using the `MIN()` function. `MIN()` is a function that takes the name of a column as an argument and returns the smallest value in that column. Here, we pass `downloads`as an argument so it will return the smallest value in the `downloads` column. 380 | 381 | 382 | 383 | ```sql 384 | SELECT AVG(downloads) FROM fake_apps; 385 | ``` 386 | 387 | This statement returns the average number of downloads for an app in our database. SQL uses the `AVG()` function to quickly calculate the average value of a particular column. 388 | 389 | The `AVG()` function works by taking a column name as an argument and returns the average value for that column. 390 | 391 | 392 | 393 | ```sql 394 | SELECT price, ROUND(AVG(downloads), 2) FROM fake_apps 395 | GROUP BY price; 396 | ``` 397 | 398 | By default, SQL tries to be as precise as possible without rounding. We can make the result set easier to read using the `ROUND()` function. 399 | 400 | `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. Here, we pass the column `AVG(downloads)` and `2` as arguments. SQL first calculates the average for each price and then rounds the result to two decimal places in the result set. 401 | 402 | 403 | 404 | Round the average number of downloads to the nearest integer for each price. 405 | 406 | ```sql 407 | SELECT price, ROUND(AVG(downloads)) FROM fake_apps 408 | GROUP BY price; 409 | ``` 410 | 411 | ## Multiple Tables 412 | 413 | So far we have learned how to build tables, write queries, and perform calculations using one table. In this lesson we will learn to query multiple tables that have relationships with each other. 414 | 415 | Most of the time, data is distributed across multiple tables in the database. Imagine a database with two tables, `artists` and `albums`. An artist can produce many different albums, and an album is produced by an artist. 416 | 417 | The data in these tables are related to each other. Through SQL, we can write queries that combine data from multiple tables that are related to one another. This is one of the most powerful features of relational databases. 418 | 419 | ### Database Schema 420 | 421 | | albums14 rows | | 422 | | ------------- | ------- | 423 | | id | INTEGER | 424 | | name | TEXT | 425 | | artist_id | INTEGER | 426 | | year | INTEGER | 427 | 428 | | artists0 rows | | 429 | | ------------- | ------- | 430 | | id | INTEGER | 431 | | name | TEXT | 432 | 433 | 434 | 435 | We have created a table named `albums`for you. Create a second table named `artists`. 436 | 437 | ```sql 438 | CREATE TABLE artists(id INTEGER PRIMARY KEY, name TEXT); 439 | ``` 440 | 441 | ```sql 442 | CREATE TABLE artists(id INTEGER PRIMARY KEY, name TEXT) 443 | ``` 444 | 445 | Using the `CREATE TABLE` statement we added a `PRIMARY KEY` to the `id` column. 446 | 447 | A **primary key** serves as a unique identifier for each row or record in a given table. The primary key is literally an `id` value for a record. We're going to use this value to connect `artists` to the `albums` they have produced. 448 | 449 | By specifying that the `id` column is the `PRIMARY KEY`, SQL makes sure that: 450 | 451 | - None of the values in this column are `NULL` 452 | - Each value in this column is unique 453 | 454 | A table can not have more than one `PRIMARY KEY` column. 455 | 456 | 457 | 458 | ```sql 459 | SELECT * FROM albums WHERE artist_id = 3; 460 | SELECT * FROM artists WHERE id = 3; 461 | ``` 462 | 463 | A *foreign key* is a column that contains the primary key of another table in the database. We use foreign keys and primary keys to connect rows in two different tables. One table's foreign key holds the value of another table's primary key. Unlike primary keys, foreign keys do not need to be unique and can be `NULL`. 464 | 465 | Here, `artist_id` is a foreign key in the `albums`table. We can see that Michael Jackson has an `id`of 3 in the `artists` table. All of the albums by Michael Jackson also have a 3 in the `artist_id`column in the `albums` table. 466 | 467 | This is how SQL is linking data between the two tables. The *relationship* between the `artists`table and the `albums` table is the `id` value of the artists. 468 | 469 | 470 | 471 | ```sql 472 | SELECT albums.name, albums.year, artists.name FROM albums, artists 473 | ``` 474 | 475 | One way to query multiple tables is to write a `SELECT` statement with multiple table names separated by a comma. This is also known as a *cross join*. Here, `albums` and `artists` are the different tables we are querying. 476 | 477 | When querying more than one table, column names need to be specified by `table_name.column_name`. Here, the result set includes the `name` and `year` columns from the `albums` table and the `name` column from the `artists` table. 478 | 479 | **Unfortunately the result of this cross join is not very useful. It combines every row of the `artists` table with every row of the `albums`table. It would be more useful to only combine the rows where the album was created by the artist.** 480 | 481 | 482 | 483 | ```sql 484 | SELECT * FROM albums JOIN artists ON albums.artist_id = artists.id; 485 | ``` 486 | 487 | In SQL, joins are used to combine rows from two or more tables. The most common type of join in SQL is an *inner join*. 488 | 489 | An inner join will combine rows from different tables if the *join condition* is true. Let's look at the syntax to see how it works. 490 | 491 | 1. `SELECT *` specifies the columns our result set will have. Here, we want to include every column in both tables. 492 | 2. `FROM albums` specifies the first table we are querying. 493 | 3. `JOIN artists ON` specifies the type of join we are going to use as well as the name of the second table. Here, we want to do an inner join and the second table we want to query is `artists`. 494 | 4. `albums.artist_id = artists.id` is the join condition that describes how the two tables are related to each other. Here, SQL uses the foreign key column `artist_id` in the `albums` table to match it with exactly one row in the `artists` table with the same value in the `id` column. We know it will only match one row in the `artists` table because `id` is the `PRIMARY KEY` of `artists`. 495 | 496 | 497 | 498 | ```sql 499 | SELECT * FROM albums LEFT JOIN artists ON albums.artist_id = artists.id; 500 | ``` 501 | 502 | Outer joins also combine rows from two or more tables, but unlike inner joins, they do not require the join condition to be met. Instead, 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. 503 | 504 | The left table is simply the first table that appears in the statement. Here, the left table is `albums`. Likewise, the right table is the second table that appears. Here, `artists` is the right table. 505 | 506 | 507 | 508 | ```sql 509 | SELECT 510 | albums.name AS 'Album', 511 | albums.year, 512 | artists.name AS 'Artist' 513 | FROM 514 | albums 515 | JOIN artists ON 516 | albums.artist_id = artists.id 517 | WHERE 518 | albums.year > 1980; 519 | ``` 520 | 521 | `AS` is a keyword in SQL that allows you to rename a column or table using an *alias*. The new name can be anything you want as long as you put it inside of single quotes. Here we want to rename the `albums.name` column as `'Album'`, and the `artists.name` column as `'Artist'`. 522 | 523 | It is important to note that the columns have not been renamed in either table. The aliases only appear in the result set. 524 | 525 | ## Some other commands: 526 | The `SUBSTR` functions return a portion of `char`, beginning at character `position`, `substring_length` characters long. `SUBSTR` calculates lengths using characters as defined by the input character set. `SUBSTRB` uses bytes instead of characters. `SUBSTRC` uses Unicode complete characters. `SUBSTR2` uses UCS2 code points. `SUBSTR4`uses UCS4 code points. 527 | 528 | - If `position` is 0, then it is treated as 1. 529 | - If `position` is positive, then Oracle Database counts from the beginning of `char` to find the first character. 530 | - If `position` is negative, then Oracle counts backward from the end of `char`. 531 | - If `substring_length` is omitted, then Oracle returns all characters to the end of `char`. If `substring_length` is less than 1, then Oracle returns null. 532 | 533 | - Examples 534 | 535 | The following example returns several specified substrings of "ABCDEFG": 536 | 537 | ```sql 538 | SELECT SUBSTR('ABCDEFG',3,4) "Substring" 539 | FROM DUAL; 540 | 541 | Substring 542 | --------- 543 | CDEF 544 | 545 | SELECT SUBSTR('ABCDEFG',-5,4) "Substring" 546 | FROM DUAL; 547 | 548 | Substring 549 | --------- 550 | CDEF 551 | ``` 552 | -------------------------------------------------------------------------------- /SQL_commands.md: -------------------------------------------------------------------------------- 1 | ## COMMANDS 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. 226 | --------------------------------------------------------------------------------