SQL Cheat Sheet Mysql
SQL Cheat Sheet Mysql
www.databasestar.com
SELECT Query Modifying Data Create Table
SELECT col1, col2
FROM table Insert INSERT INTO tablename Create Table CREATE TABLE tablename (
JOIN table2 ON table1.col = table2.col (col1, col2...) column_name data_type
WHERE condition VALUES (val1, val2); );
GROUP BY column_name Insert from a
HAVING condition INSERT INTO tablename
Table
ORDER BY col1 ASC|DESC; (col1, col2...) Create Table with Constraints
SELECT col1, col2...
A A
Set Operators Rename Column ALTER TABLE tablename CHANGE
COLUMN currentname TO newname;
B B
CASE Statement
RANK() OVER (
PARTITION BY gender ORDER BY fees_paid
Common Functions ) AS rank_val
Simple Case CASE name FROM student;
WHEN 'John' THEN 'Name John' LENGTH(string): Returns the length of the provided string
WHEN 'Steve' THEN 'Name Steve' INSTR(string, substring): Returns the position of the substring
ELSE 'Unknown' within the specified string.
END CAST(expression AS datatype): Converts an expression into the Subqueries
specified data type.
ADDDATE(input_date, days): Adds a number of days to a Single Row SELECT id, last_name, salary
Searched Case CASE FROM employee
specified date.
WHEN name='John' THEN 'Name John' WHERE salary = (
NOW: Returns the current date, including time.
WHEN name='Steve' THEN 'Name Steve' SELECT MAX(salary)
CEILING(input_val): Returns the smallest integer greater than
ELSE 'Unknown' FROM employee
the provided number.
END );
FLOOR(input_val): Returns the largest integer less than the
provided number.
Common Table Expression ROUND(input_val, [round_to]): Rounds a number to a specified
number of decimal places.
Multi Row SELECT id, last_name, salary
FROM employee
TRUNCATE(input_value, num_decimals): Truncates a number to WHERE salary IN (
WITH queryname AS ( SELECT salary
a number of decimals.
SELECT col1, col2 FROM employee
REPLACE(whole_string, string_to_replace, replacement_string):
FROM firsttable) WHERE last_name LIKE 'C%'
Replaces one string inside the whole string with another string.
SELECT col1, col2.. );
SUBSTRING(string, start_position): Returns part of a value,
FROM queryname...;
based on a position and length.