Select Modifying Data: SQL Cheat Sheet - Mysql
Select Modifying Data: SQL Cheat Sheet - Mysql
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:
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.