CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype,
....
);
DROP TABLE table_name;
ALTER TABLE table_name
ADD column_name datatype;
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE table_name
MODIFY COLUMN column_name datatype;
ALTER TABLE old_table_name
RENAME TO new_table_name;
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
DELETE FROM table_name
WHERE condition;
SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM table_name;
SELECT table1.column1, table2.column2, ...
FROM table1
JOIN table2 ON table1.common_column = table2.common_column;
SELECT column_name AS alias_name
FROM table_name;
SELECT COUNT(column_name)
FROM table_name
WHERE condition;
SELECT AVG(column_name)
FROM table_name
WHERE condition;
SELECT SUM(column_name)
FROM table_name
WHERE condition;
SELECT MAX(column_name)
FROM table_name
WHERE condition;
SELECT MIN(column_name)
FROM table_name
WHERE condition;
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host';
GRANT SELECT, INSERT, UPDATE, DELETE ON database_name.table_name TO 'username'@'host';
REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'host';
REVOKE SELECT, INSERT, UPDATE, DELETE ON database_name.table_name FROM 'username'@'host';
START TRANSACTION;
COMMIT;
ROLLBACK;
SAVEPOINT savepoint_name;
ROLLBACK TO SAVEPOINT savepoint_name;
CREATE INDEX index_name
ON table_name (column1, column2, ...);
DROP INDEX index_name;
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
DROP VIEW view_name;
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 ASC|DESC;
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1;
SELECT column1, COUNT(*)
FROM table_name
GROUP BY column1
HAVING COUNT(*) > value;
SELECT column1, column2, ...
FROM table_name
LIMIT number;
SELECT CONCAT(column1, column2) AS new_column
FROM table_name;
SELECT SUBSTRING(column_name, start, length) AS new_column
FROM table_name;
SELECT REPLACE(column_name, 'old_value', 'new_value') AS new_column
FROM table_name;
SELECT CURRENT_DATE;
SELECT CURRENT_TIME;
SELECT DATE_ADD(column_name, INTERVAL value DAY) AS new_date
FROM table_name;
SELECT DATE_SUB(column_name, INTERVAL value DAY) AS new_date
FROM table_name;
SELECT column_name,
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE result
END AS new_column
FROM table_name;
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;
Returns all records from the left table (table1), and the matched records from the right table (table2). The result is NULL from the right side if there is no match.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
SELECT employees.name, departments.name
FROM employees
LEFT JOIN departments
ON employees.department_id = departments.id;
Returns all records from the right table (table2), and the matched records from the left table (table1). The result is NULL from the left side when there is no match.
SELECT columns
FROM table1
RIGHT JOIN table2
ON table1.column = table2.column;
SELECT employees.name, departments.name
FROM employees
RIGHT JOIN departments
ON employees.department_id = departments.id;
Returns all records when there is a match in either left (table1) or right (table2) table records. The result is NULL from the side where there is no match.
SELECT columns
FROM table1
FULL JOIN table2
ON table1.column = table2.column;
SELECT employees.name, departments.name
FROM employees
FULL JOIN departments
ON employees.department_id = departments.id;
Returns the Cartesian product of both tables. This means it returns all possible combinations of rows from both tables.
SELECT columns
FROM table1
CROSS JOIN table2;
SELECT employees.name, departments.name
FROM employees
CROSS JOIN departments;
A self JOIN is a regular join but the table is joined with itself.
SELECT a.columns, b.columns
FROM table a, table b
WHERE condition;
SELECT a.employee_id, a.name, b.name AS manager_name
FROM employees a, employees b
WHERE a.manager_id = b.employee_id;
A NATURAL JOIN is based on all columns in the two tables that have the same name and selects rows with equal values in the relevant columns.
SELECT columns
FROM table1
NATURAL JOIN table2;
SELECT *
FROM employees
NATURAL JOIN departments;
The USING clause is used to specify only those columns that should be used for an EQUIJOIN.
SELECT columns
FROM table1
JOIN table2
USING (column);
SELECT employees.name, departments.name
FROM employees
JOIN departments
USING (department_id);