Database Commands
-
Create a database
CREATE DATABASE database_name; -
Drop a database
DROP DATABASE database_name; -
Use a database
USE database_name;
Table Commands
-
Create a table
CREATE TABLE table_name ( column1 datatype, column2 datatype, column3 datatype, ... ); -
Add another column to an existing table
ALTER TABLE table_name ADD column_name datatype; -
Drop a table
DROP TABLE table_name; -
Auto ID
The primary key is the primary identifier of a row in a table. It is unique for each row and cannot be NULL. The auto-increment feature automatically generates a unique value for the primary key when a new row is inserted into the table.
CREATE TABLE table_name ( id INT NOT NULL AUTO_INCREMENT, column1 datatype, column2 datatype, PRIMARY KEY (id) ... ); -
Reference another table
CREATE TABLE table_name ( id INT AUTO_INCREMENT PRIMARY KEY, column1 datatype, column2 datatype, other_tables_column INT NOT NULL, PRIMARY KEY (id), FOREIGN KEY (other_tables_column) REFERENCES other_table(other_tables_column) );
CRUD Commands
-
Insert data into a table
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...); -
Insert multiple rows into a table
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...), (value4, value5, value6, ...), (value7, value8, value9, ...); -
Read all data from a table
SELECT * FROM table_name; -
Read limited data from a table
SELECT * FROM table_name LIMIT number_of_rows; -
Read specific columns from a table
SELECT column1, column2, column3 FROM table_name; -
Read columns with an alias
SELECT column1 AS alias_name, column2 AS alias_name FROM table_name; -
Order data by a column
SELECT * FROM table_name ORDER BY column_name ASC|DESC; -
Get only unique values
SELECT DISTINCT column_name FROM table_name; -
Update data in a table
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; -
Filter with LIKE string filter
SELECT * FROM table_name WHERE column_name LIKE '%value%';Everything inside the % can be anything. The % is a wildcard that matches any number of characters.
-
Use boolean operators
SELECT * FROM table_name WHERE column_name = value1 AND column_name = value2;SELECT * FROM table_name WHERE column_name = value1 OR column_name = value2; -
The BETWEEN operator
SELECT * FROM table_name WHERE column_name BETWEEN value1 AND value2; -
Delete data from a table
DELETE FROM table_name WHERE condition;
JOIN Commands
Use JOIN to combine rows from two or more tables based on a shared column.
-
Basic JOIN
SELECT * FROM table1 JOIN table2 ON table1.column_name = table2.column_name;
There are multiple types of JOINs:
- INNER JOIN: Returns records that have matching values in both tables.
- LEFT JOIN: Returns all records from the left table and the matched records from the right table.
- RIGHT JOIN: Returns all records from the right table and the matched records from the left table.
Aggregate Functions
-
Average
SELECT AVG(column_name) FROM table_name; -
Sum
SELECT SUM(column_name) FROM table_name; -
Count
SELECT COUNT(column_name) FROM table_name; -
Group by merges rows that have the same values in specified columns into summary rows.
SELECT id, COUNT(id) FROM table_name GROUP BY id;SELECT b.column1 as column_name, COUNT(a.coluumn2) as number_of_rows FROM table1 AS b LEFT JOIN table2 AS a ON a.column_name = b.column_name GROUP BY b.column1; -
Having The HAVING clause is used to filter records that work on summarized GROUP BY results.
SELECT column_name, COUNT(column_name) FROM table_name GROUP BY column_name HAVING COUNT(column_name) > value;