Skip to content

Instantly share code, notes, and snippets.

@AntoOnline
Last active April 29, 2023 19:54
Show Gist options
  • Save AntoOnline/69502c1b3969be8475bbe0970102e367 to your computer and use it in GitHub Desktop.
Save AntoOnline/69502c1b3969be8475bbe0970102e367 to your computer and use it in GitHub Desktop.

MySQL Cheat Sheet

Connecting to MySQL Server

mysql -u [username] -p

Basic MySQL Commands

Show all databases

SHOW DATABASES;

Select a database

USE [database_name];

Show tables in the current database

SHOW TABLES;

Show columns of a table

SHOW COLUMNS FROM [table_name];

Create a new database

CREATE DATABASE [database_name];

Delete a database

DROP DATABASE [database_name];

CRUD Operations

Create a table

CREATE TABLE [table_name] (
  column1 datatype,
  column2 datatype,
  ...
);

Insert data into a table

INSERT INTO [table_name] (column1, column2, ...)
VALUES (value1, value2, ...);

Select data from a table

SELECT column1, column2, ...
FROM [table_name];

Update data in a table

UPDATE [table_name]
SET column1=value1, column2=value2, ...
WHERE [condition];

Delete data from a table

DELETE FROM [table_name]
WHERE [condition];

MySQL Joins

INNER JOIN

SELECT column1, column2, ...
FROM table1
INNER JOIN table2
ON table1.column=table2.column;

LEFT JOIN

SELECT column1, column2, ...
FROM table1
LEFT JOIN table2
ON table1.column=table2.column;

RIGHT JOIN

SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2
ON table1.column=table2.column;

FULL JOIN (not supported in MySQL, can be simulated)

SELECT column1, column2, ...
FROM table1
LEFT JOIN table2
ON table1.column=table2.column
UNION
SELECT column1, column2, ...
FROM table1
RIGHT JOIN table2
ON table1.column=table2.column;

Aggregation Functions

COUNT

SELECT COUNT(column_name)
FROM [table_name]
WHERE [condition];

SUM

SELECT SUM(column_name)
FROM [table_name]
WHERE [condition];

AVG

SELECT AVG(column_name)
FROM [table_name]
WHERE [condition];

MIN

SELECT MIN(column_name)
FROM [table_name]
WHERE [condition];

MAX

SELECT MAX(column_name)
FROM [table_name]
WHERE [condition];

This cheat sheet provides a quick reference for someone new to MySQL, covering basic commands, CRUD operations, joins, and aggregation functions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment