Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SQL Cheat Sheet

SELECT TRUNCATE: Joins


TRUNCATE TABLE tablename;
SELECT col1, col2 SELECT t1.*, t2.*
FROM table UPDATE with Join: FROM t1
WHERE condition UPDATE t join_type t2 ON t1.col = t2.col;
GROUP BY cols SET col1 = val1
HAVING condition INNER JOIN: show all matching records in both
FROM tablename t
ORDER BY col; tables.
INNER JOIN table x ON t.id = x.tid
WHERE condition; LEFT JOIN: show all records from left table, and any
SELECT Keywords
matching records from right table.
INSERT Multiple Rows:
DISTINCT: Removes duplicate results --MySQL, SQL Server RIGHT JOIN: show all records from right table, and
INSERT INTO tablename (col1, col2…) any matching records from left table.
BETWEEN: Matches a value between two other
VALUES (valA1, valB1), (valA2, valB2),
values (inclusive)
(valA3, valB3); FULL JOIN: show all records from both tables,
IN: Matches a value to one of many values whether there is a match or not.
--Oracle
LIKE: Performs partial/wildcard matches INSERT CROSS JOIN: show all combinations of records from
INTO tablename (col1, col2) VALUES both tables.
Modifying Data (valA1, valB1)
SELF JOIN: join a table to itself. Used for hierarchical
INTO tablename (col1, col2) VALUES
INSERT: data.
(valA2, valB2)
INSERT INTO tablename (col1, col2...) SELECT p.*, c.*
SELECT * FROM dual;
VALUES (val1, val2); FROM yourtable p
MERGE: INNER JOIN yourtable c ON p.id =
INSERT From Table: MERGE INTO table_name c.parent_id;
INSERT INTO tablename (col1, col2…) USING table_name
SELECT col1, col2… ON (condition)
WHEN MATCHED THEN update_clause
UPDATE:
DELETE where_clause
UPDATE tablename SET col1 = val1
WHEN NOT MATCHED THEN insert_clause;
WHERE condition;

DELETE:
DELETE FROM tablename WHERE condition;
SQL Cheat Sheet

Create Table Alter Table Add Constraint


ALTER TABLE tablename ADD CONSTRAINT
Create Table: Add Column
constraintname constrainttype (columns);
CREATE TABLE tablename ( ALTER TABLE tablename ADD columnname
column_name data_type datatype; Drop Constraint
); --Oracle, SQL Server
Drop Column
ALTER TABLE tablename DROP CONSTRAINT
Create Table WIth Constraints: ALTER TABLE tablename DROP COLUMN
constraintname;
CREATE TABLE tablename ( columnname;
column_name data_type NOT NULL,
Modify Column --Oracle, SQL Server, MySQL
CONSTRAINT pkname PRIMARY KEY (col),
--Oracle ALTER TABLE tablename DROP
CONSTRAINT fkname FOREIGN KEY (col)
ALTER TABLE tablename MODIFY columnname constraint_type constraintname;
REFERENCES
other_table(col_in_other_table), newdatatype;
Rename Table
CONSTRAINT ucname UNIQUE (col), --Oracle
CONSTRAINT ckname CHECK (conditions) --SQL Server
ALTER TABLE tablename RENAME TO
); ALTER TABLE tablename ALTER COLUMN
newtablename;
columnname newdatatype;
Drop Table:
--SQL Server
DROP TABLE tablename; --MySQL
sp_rename 'old_table_name',
ALTER TABLE tablename CHANGE columnname
Create Temporary Table: 'new_table_name';
newcolumnname newdatatype; [MySQL]
--Oracle
CREATE GLOBAL TEMPORARY TABLE tname ( Rename Column --MySQL
colname data_type --Oracle ALTER TABLE tablename RENAME TO
) ON COMMIT DELETE ROWS; ALTER TABLE tablename RENAME COLUMN newtablename;
currentname TO newname;
--SQL Server
SELECT cols --SQL Server
Indexes
INTO #tablename sp_rename 'table_name.old_column_name', Create Index:
FROM table; 'new_column_name', 'COLUMN'; CREATE INDEX indexname ON tablename
(cols);
--MySQL --MySQL
CREATE TEMPORARY TABLE tablename ALTER TABLE tablename CHANGE COLUMN Drop Index:
(colname datatype); currentname TO newname; DROP INDEX indexname;
SQL Cheat Sheet

Set Operators CASE Statement Subqueries


Simple Case: Single Row:
UNION: Shows unique rows from two result sets.
CASE name SELECT id, last_name, salary
UNION ALL: Shows all rows from two result sets. WHEN 'John' THEN 'Name John' FROM employee
WHEN 'Steve' THEN 'Name Steve' WHERE salary = (
INTERSECT: Shows rows that exist in both result ELSE 'Unknown' SELECT MAX(salary)
sets. END FROM employee
);
MINUS: Shows rows that exist in the first result set Searched Case:
but not the second. (Oracle, MySQL) CASE Multi Row
WHEN name='John' THEN 'Name John' SELECT id, last_name, salary
EXCEPT: Shows rows that exist in the first result set FROM employee
WHEN name='Steve' THEN 'Name Steve'
but not the second. (SQL Server) WHERE salary IN (
ELSE 'Unknown'
END SELECT salary
FROM employee
WHERE last_name LIKE 'C%'
Analytic Functions
);
function_name ( arguments ) OVER ( With Clause/Common Table Expression
[query_partition_clause]
--Oracle, MySQL
[ORDER BY order_by_clause
WITH queryname AS (
[windowing_clause] ] ) Aggregate Functions
SELECT col1, col2
Example using RANK, showing the student details FROM firsttable) SUM: Finds a total of the numbers provided
and their rank according to the fees_paid, grouped by SELECT col1, col2..
FROM queryname…; COUNT: Finds the number of records
gender:
SELECT
--SQL Server AVG: Finds the average of the numbers provided
student_id, first_name, last_name,
gender, fees_paid, WITH queryname (col1, col2…) AS (
MIN: Finds the lowest of the numbers provided
RANK() OVER (PARTITION BY gender ORDER SELECT column1, column2
BY fees_paid) AS rank_val FROM firsttable) MAX: Finds the highest of the numbers provided
FROM student; SELECT col1, col2..
FROM queryname…;

You might also like