Skip to content

Instantly share code, notes, and snippets.

@egenedy97
Forked from hofmannsven/README.md
Created December 25, 2018 05:01

Revisions

  1. @hofmannsven hofmannsven revised this gist Jul 4, 2017. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,13 +6,13 @@ Getting started:
    - https://www.codecademy.com/courses/learn-sql

    Related tutorials:
    - MySQL-CLI: https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P
    - Analyzing Business Metrics: https://www.codecademy.com/learn/sql-analyzing-business-metrics
    - SQL joins infografic: http://cd64.de/sql-joins
    - [MySQL-CLI](https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P)
    - [Analyzing Business Metrics](https://www.codecademy.com/learn/sql-analyzing-business-metrics)
    - [SQL joins infografic](https://lh4.googleusercontent.com/-RdjzcoAwBYg/UxTXWGJHgoI/AAAAAAAACrs/Gqbu6zyksgo/w852-h670/sql-joins.jpg)

    Tools:
    - DataGrip: https://www.jetbrains.com/datagrip/
    - Sequel Pro: http://www.sequelpro.com/
    - [DataGrip](https://www.jetbrains.com/datagrip/)
    - [Sequel Pro](http://www.sequelpro.com/)


    Commands
  2. @hofmannsven hofmannsven revised this gist Jul 28, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -28,10 +28,14 @@ Create new database: `create database [database];`

    Select database: `use [database];`

    Determine what database is in use: `select database();`

    Show all tables: `show tables;`

    Show table structure: `describe [table];`

    List all indexes on a table: `show index from [table];`

    Create new table with columns: `CREATE TABLE [table] ([column] VARCHAR(120), [another-column] DATETIME);`

    Adding a column: `ALTER TABLE [table] ADD COLUMN [column] VARCHAR(120);`
    @@ -71,6 +75,8 @@ Deleting records: `DELETE FROM [table] WHERE [column] = [value];`
    Delete *all records* from a table (without dropping the table itself): `DELETE FROM [table];`
    (This also resets the incrementing counter for auto generated columns like an id column.)

    Delete all records in a table: `truncate table [table];`

    Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];`

    Deleting tables: `DROP TABLE [table];`
  3. @hofmannsven hofmannsven revised this gist Mar 15, 2016. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,10 @@ Related tutorials:
    - Analyzing Business Metrics: https://www.codecademy.com/learn/sql-analyzing-business-metrics
    - SQL joins infografic: http://cd64.de/sql-joins

    Tools:
    - DataGrip: https://www.jetbrains.com/datagrip/
    - Sequel Pro: http://www.sequelpro.com/


    Commands
    -----------
  4. @hofmannsven hofmannsven revised this gist Feb 26, 2016. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,10 @@ Getting started:
    - http://www.sqlteaching.com/
    - https://www.codecademy.com/courses/learn-sql

    Related tutorial: http://cd64.de/mysql-cli

    SQL joins infografic: http://cd64.de/sql-joins
    Related tutorials:
    - MySQL-CLI: https://www.youtube.com/playlist?list=PLfdtiltiRHWEw4-kRrh1ZZy_3OcQxTn7P
    - Analyzing Business Metrics: https://www.codecademy.com/learn/sql-analyzing-business-metrics
    - SQL joins infografic: http://cd64.de/sql-joins


    Commands
  5. @hofmannsven hofmannsven revised this gist Feb 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -125,4 +125,4 @@ Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'l

    Find out the IP Address of the Mysql Host
    -----------
    `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646), contribution @junrillg)
    `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646))
  6. @hofmannsven hofmannsven revised this gist Feb 5, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -125,4 +125,4 @@ Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'l

    Find out the IP Address of the Mysql Host
    -----------
    `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646))
    `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646), contribution @junrillg)
  7. @hofmannsven hofmannsven revised this gist Feb 5, 2016. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -116,8 +116,13 @@ Rename column or table using an _alias_: `SELECT [table1].[column] AS '[value]',
    Users functions
    -----------

    List all users: `select User,Host from mysql.user;`
    List all users: `SELECT User,Host FROM mysql.user;`

    Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`

    Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
    Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`


    Find out the IP Address of the Mysql Host
    -----------
    `SHOW VARIABLES WHERE Variable_name = 'hostname';` ([source](http://serverfault.com/a/129646))
  8. @hofmannsven hofmannsven revised this gist Feb 5, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -39,6 +39,8 @@ MySQL function for datetime input: `NOW()`

    Selecting records: `SELECT * FROM [table];`

    Explain records: `EXPLAIN SELECT * FROM [table];`

    Selecting parts of records: `SELECT [column], [another-column] FROM [table];`

    Counting records: `SELECT COUNT([column]) FROM [table];`
  9. @hofmannsven hofmannsven revised this gist Jan 8, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -74,6 +74,8 @@ Custom column output names: `SELECT [column] AS [custom-column] FROM [table];`

    Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql`

    Use `--lock-tables=false` option for locked tables (more info [here](http://stackoverflow.com/a/104628/1815847)).

    Import a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysql -u [username] -p -h localhost [database] < db_backup.sql`

    Logout: `exit;`
  10. @hofmannsven hofmannsven revised this gist Sep 18, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -97,6 +97,18 @@ Get average value: `SELECT AVG([column]) FROM [table];`
    Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];`


    Multiple tables
    -----------

    Select from multiple tables: `SELECT [table1].[column], [table1].[another-column], [table2].[column] FROM [table1], [table2];`

    Combine rows from different tables: `SELECT * FROM [table1] INNER JOIN [table2] ON [table1].[column] = [table2].[column];`

    Combine rows from different tables but do not require the join condition: `SELECT * FROM [table1] LEFT OUTER JOIN [table2] ON [table1].[column] = [table2].[column];` (The left table is the first table that appears in the statement.)

    Rename column or table using an _alias_: `SELECT [table1].[column] AS '[value]', [table2].[column] AS '[value]' FROM [table1], [table2];`


    Users functions
    -----------

  11. @hofmannsven hofmannsven revised this gist Sep 18, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,7 @@ Get average value: `SELECT AVG([column]) FROM [table];`
    Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];`


    User functions
    Users functions
    -----------

    List all users: `select User,Host from mysql.user;`
  12. @hofmannsven hofmannsven revised this gist Sep 18, 2015. 3 changed files with 29 additions and 13 deletions.
    30 changes: 29 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -76,4 +76,32 @@ Export a database dump (more info [here](http://stackoverflow.com/a/21091197/181

    Import a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysql -u [username] -p -h localhost [database] < db_backup.sql`

    Logout: `exit;`
    Logout: `exit;`


    Aggregate functions
    -----------

    Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00`

    Calculate total number of records: `SELECT SUM([column]) FROM [table];`

    Count total number of `[column]` and group by `[category-column]`: `SELECT [category-column], SUM([column]) FROM [table] GROUP BY [category-column];`

    Get largest value in `[column]`: `SELECT MAX([column]) FROM [table];`

    Get smallest value: `SELECT MIN([column]) FROM [table];`

    Get average value: `SELECT AVG([column]) FROM [table];`

    Get rounded average value and group by `[category-column]`: `SELECT [category-column], ROUND(AVG([column]), 2) FROM [table] GROUP BY [category-column];`


    User functions
    -----------

    List all users: `select User,Host from mysql.user;`

    Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`

    Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
    4 changes: 0 additions & 4 deletions SPECIAL.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +0,0 @@
    Commands
    -----------

    Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00`
    8 changes: 0 additions & 8 deletions USERS.md
    Original file line number Diff line number Diff line change
    @@ -1,8 +0,0 @@
    Commands
    -----------

    List all users: `select User,Host from mysql.user;`

    Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`

    Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
  13. @hofmannsven hofmannsven revised this gist Sep 18, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -55,6 +55,8 @@ Select records starting with `val` and ending with `ue`: `SELECT * FROM [table]

    Select a range: `SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];`

    Select with custom order and only limit: `SELECT * FROM [table] WHERE [column] ORDER BY [column] ASC LIMIT [value];` (Order: `DESC`, `ASC`)

    Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];`

    Deleting records: `DELETE FROM [table] WHERE [column] = [value];`
  14. @hofmannsven hofmannsven revised this gist Sep 18, 2015. 1 changed file with 6 additions and 4 deletions.
    10 changes: 6 additions & 4 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -45,13 +45,15 @@ Counting records: `SELECT COUNT([column]) FROM [table];`

    Counting and selecting grouped records: `SELECT *, (SELECT COUNT([column]) FROM [table]) AS count FROM [table] GROUP BY [column];`

    Selecting specific records: `SELECT * FROM [table] WHERE [column] = [value];` (Selectors: `<`, `>`, `!=`)
    Selecting specific records: `SELECT * FROM [table] WHERE [column] = [value];` (Selectors: `<`, `>`, `!=`; combine multiple selectors with `AND`, `OR`)

    Searching records for a word: `SELECT * FROM [table] WHERE [column] LIKE '%[value]%';`
    Select records containing `[value]`: `SELECT * FROM [table] WHERE [column] LIKE '%[value]%';`

    Searching records for a word starting with [value]: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';`
    Select records starting with `[value]`: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';`

    Searching for words starting with `val` and ending with `ue`: `SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';`
    Select records starting with `val` and ending with `ue`: `SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';`

    Select a range: `SELECT * FROM [table] WHERE [column] BETWEEN [value1] and [value2];`

    Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];`

  15. @hofmannsven hofmannsven revised this gist Sep 18, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,8 @@ Searching records for a word: `SELECT * FROM [table] WHERE [column] LIKE '%[valu

    Searching records for a word starting with [value]: `SELECT * FROM [table] WHERE [column] LIKE '[value]%';`

    Searching for words starting with `val` and ending with `ue`: `SELECT * FROM [table] WHERE [column] LIKE '[val_ue]';`

    Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column] = [value];`

    Deleting records: `DELETE FROM [table] WHERE [column] = [value];`
  16. @hofmannsven hofmannsven revised this gist Sep 15, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions USERS.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    Commands
    -----------

    List all users: `select User,Host from mysql.user;`

    Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`

    Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
  17. @hofmannsven hofmannsven revised this gist Sep 13, 2015. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    MySQL
    ===============

    Getting started: http://www.sqlteaching.com/
    Getting started:
    - http://www.sqlteaching.com/
    - https://www.codecademy.com/courses/learn-sql

    Related tutorial: http://cd64.de/mysql-cli

  18. @hofmannsven hofmannsven revised this gist Sep 13, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -66,4 +66,6 @@ Custom column output names: `SELECT [column] AS [custom-column] FROM [table];`

    Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql`

    Import a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysql -u [username] -p -h localhost [database] < db_backup.sql`

    Logout: `exit;`
  19. @hofmannsven hofmannsven revised this gist Aug 11, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion USERS.md
    Original file line number Diff line number Diff line change
    @@ -3,4 +3,4 @@ Commands

    Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`

    Grunt `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
    Grant `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
  20. @hofmannsven hofmannsven revised this gist Jul 3, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion SPECIAL.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    Commands
    -----------

    Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1`
    Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1 AND date >= 2015-01-01 00:00:00`
  21. @hofmannsven hofmannsven renamed this gist Jun 14, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  22. @hofmannsven hofmannsven renamed this gist Jun 14, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  23. @hofmannsven hofmannsven revised this gist Jun 14, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions DISTINCT.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    Commands
    -----------

    Select but without duplicates: `SELECT distinct name, email, acception FROM owners WHERE acception = 1`
  24. @hofmannsven hofmannsven revised this gist May 28, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions USERS.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    Commands
    -----------

    Create new user: `CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';`

    Grunt `ALL` access to user for `*` tables: `GRANT ALL ON database.* TO 'user'@'localhost';`
  25. @hofmannsven hofmannsven revised this gist Feb 19, 2015. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,8 @@
    MySQL
    ===============

    Getting started: http://www.sqlteaching.com/

    Related tutorial: http://cd64.de/mysql-cli

    SQL joins infografic: http://cd64.de/sql-joins
  26. @hofmannsven hofmannsven revised this gist Nov 8, 2014. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -52,6 +52,7 @@ Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column
    Deleting records: `DELETE FROM [table] WHERE [column] = [value];`

    Delete *all records* from a table (without dropping the table itself): `DELETE FROM [table];`
    (This also resets the incrementing counter for auto generated columns like an id column.)

    Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];`

  27. @hofmannsven hofmannsven revised this gist Nov 8, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -51,6 +51,8 @@ Updating records: `UPDATE [table] SET [column] = '[updated-value]' WHERE [column

    Deleting records: `DELETE FROM [table] WHERE [column] = [value];`

    Delete *all records* from a table (without dropping the table itself): `DELETE FROM [table];`

    Removing table columns: `ALTER TABLE [table] DROP COLUMN [column];`

    Deleting tables: `DROP TABLE [table];`
    @@ -61,4 +63,4 @@ Custom column output names: `SELECT [column] AS [custom-column] FROM [table];`

    Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql`

    Logout: `exit`
    Logout: `exit;`
  28. @hofmannsven hofmannsven revised this gist Sep 11, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions my.cnf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    [mysqld]
    max_allowed_packet=64M
  29. @hofmannsven hofmannsven revised this gist Aug 3, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -59,6 +59,6 @@ Deleting databases: `DROP DATABASE [database];`

    Custom column output names: `SELECT [column] AS [custom-column] FROM [table];`

    Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` (will prompt for password)
    Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql`

    Logout: `exit`
  30. @hofmannsven hofmannsven revised this gist Aug 3, 2014. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion README.md
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ SQL joins infografic: http://cd64.de/sql-joins
    Commands
    -----------

    Access monitor: `mysql -u [username] -p` (will prompt for password)
    Access monitor: `mysql -u [username] -p;` (will prompt for password)

    Show all databases: `show databases;`

    @@ -59,4 +59,6 @@ Deleting databases: `DROP DATABASE [database];`

    Custom column output names: `SELECT [column] AS [custom-column] FROM [table];`

    Export a database dump (more info [here](http://stackoverflow.com/a/21091197/1815847)): `mysqldump -u [username] -p [database] > db_backup.sql` (will prompt for password)

    Logout: `exit`