SQL Cheat Sheet - MySQL - www.databasestar.
com
SELECT Modifying Data MERGE:
MERGE INTO table_name
SELECT col1, col2
INSERT: USING table_name
FROM table
INSERT INTO tablename (col1, col2...) ON (condition)
WHERE condition
VALUES (val1, val2); WHEN MATCHED THEN update_clause
GROUP BY cols
DELETE where_clause
HAVING condition INSERT From Table: WHEN NOT MATCHED THEN insert_clause;
ORDER BY col; INSERT INTO tablename (col1, col2…)
SELECT col1, col2… Joins
Order of Processing
UPDATE: SELECT t1.*, t2.*
1. FROM
UPDATE tablename SET col1 = val1 FROM t1
2. JOIN
WHERE condition; join_type t2 ON t1.col = t2.col;
3. WHERE
4. GROUP BY INNER JOIN: show all matching records in both
DELETE:
5. HAVING tables.
DELETE FROM tablename WHERE condition;
6. SELECT
7. DISTINCT TRUNCATE: LEFT JOIN: show all records from left table, and any
8. ORDER BY TRUNCATE TABLE tablename; matching records from right table.
9. LIMIT
UPDATE with Join: RIGHT JOIN: show all records from right table, and
SELECT Keywords UPDATE t any matching records from left table.
SET col1 = val1
DISTINCT: Removes duplicate results FULL JOIN: show all records from both tables,
FROM tablename t
whether there is a match or not.
INNER JOIN table x ON t.id = x.tid
BETWEEN: Matches a value between two other
WHERE condition; CROSS JOIN: show all combinations of records from
values (inclusive)
both tables.
INSERT Multiple Rows:
IN: Matches a value to one of many values
INSERT INTO tablename (col1, col2…) SELF JOIN: join a table to itself. Used for hierarchical
LIKE: Performs partial/wildcard matches VALUES (valA1, valB1), (valA2, valB2), data.
(valA3, valB3); SELECT p.*, c.*
FROM yourtable p
INNER JOIN yourtable c ON p.id =
c.parent_id;
SQL Cheat Sheet - MySQL - www.databasestar.com
Create Table Modify Column UNION ALL: Shows all rows from two result sets.
ALTER TABLE tablename CHANGE columnname
Create Table: INTERSECT: Shows rows that exist in both result
newcolumnname newdatatype; [MySQL]
CREATE TABLE tablename ( sets.
column_name data_type Rename Column
); ALTER TABLE tablename CHANGE COLUMN MINUS: Shows rows that exist in the first result set
currentname TO newname; but not the second.
Create Table WIth Constraints:
CREATE TABLE tablename ( Add Constraint Analytic Functions
column_name data_type NOT NULL, ALTER TABLE tablename ADD CONSTRAINT function_name ( arguments ) OVER (
CONSTRAINT pkname PRIMARY KEY (col), constraintname constrainttype (columns); [query_partition_clause]
CONSTRAINT fkname FOREIGN KEY (col)
[ORDER BY order_by_clause
REFERENCES Drop Constraint
[windowing_clause] ] )
other_table(col_in_other_table), ALTER TABLE tablename DROP
CONSTRAINT ucname UNIQUE (col), constraint_type constraintname; Example using RANK, showing the student details
CONSTRAINT ckname CHECK (conditions) and their rank according to the fees_paid, grouped by
); Rename Table
gender:
ALTER TABLE tablename RENAME TO
SELECT
Drop Table: newtablename;
student_id, first_name, last_name,
DROP TABLE tablename;
gender, fees_paid,
Indexes
Create Temporary Table: RANK() OVER (PARTITION BY gender ORDER
Create Index: BY fees_paid) AS rank_val
CREATE TEMPORARY TABLE tablename
CREATE INDEX indexname ON tablename FROM student;
(colname datatype);
(cols);
CASE Statement
Drop Index:
Alter Table Simple Case:
DROP INDEX indexname;
Add Column CASE name
ALTER TABLE tablename ADD columnname WHEN 'John' THEN 'Name John'
datatype; WHEN 'Steve' THEN 'Name Steve'
ELSE 'Unknown'
Drop Column END
ALTER TABLE tablename DROP COLUMN
columnname; Set Operators Searched Case:
UNION: Shows unique rows from two result sets.
SQL Cheat Sheet - MySQL - www.databasestar.com
CASE
WHEN name='John' THEN 'Name John' COUNT: Finds the number of records REPLACE(whole_string, string_to_replace,
WHEN name='Steve' THEN 'Name Steve' replacement_string): Replaces one string inside the
AVG: Finds the average of the numbers provided whole string with another string.
ELSE 'Unknown'
END MIN: Finds the lowest of the numbers provided SUBSTRING(string, start_position): Returns part of a
With Clause/Common Table Expression value, based on a position and length.
MAX: Finds the highest of the numbers provided
WITH queryname AS (
SELECT col1, col2 Common Functions
FROM firsttable)
LENGTH(string): Returns the length of the provided
SELECT col1, col2..
string
FROM queryname…;
INSTR(string, substring): Returns the position of the
Subqueries substring within the specified string.
Single Row:
SELECT id, last_name, salary CAST(expression AS datatype): Converts an
FROM employee expression into the specified data type.
WHERE salary = (
ADDDATE(input_date, days): Adds a number of days
SELECT MAX(salary)
to a specified date.
FROM employee
); NOW: Returns the current date, including time.
Multi Row
CEILING(input_val): Returns the smallest integer
SELECT id, last_name, salary
greater than the provided number.
FROM employee
WHERE salary IN ( FLOOR(input_val): Returns the largest integer less
SELECT salary than the provided number.
FROM employee
WHERE last_name LIKE 'C%' ROUND(input_val, [round_to]): Rounds a number to a
); specified number of decimal places.
TRUNCATE(input_value, num_decimals): Truncates
a number to a number of decimals.
Aggregate Functions
SUM: Finds a total of the numbers provided