You are on page 1of 3

SQL Cheat Sheet - Oracle - ​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. FETCH
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 SELF JOIN: join a table to itself. Used for hierarchical
LIKE: Performs partial/wildcard matches INTO tablename (col1, col2) VALUES data.
(valA1, valB1) SELECT p.*, c.*
INTO tablename (col1, col2) VALUES FROM yourtable p
(valA2, valB2) INNER JOIN yourtable c ON p.id =
SELECT * FROM dual; c.parent_id;
SQL Cheat Sheet - Oracle - ​www.databasestar.com

Create Table Modify Column UNION ALL: Shows all rows from two result sets.
ALTER TABLE tablename MODIFY columnname
Create Table: INTERSECT: Shows rows that exist in both result
newdatatype;
CREATE TABLE tablename ( sets.
column_name data_type Rename Column
); ALTER TABLE tablename RENAME 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
CONSTRAINT ucname UNIQUE (col), constraintname; Example using RANK, showing the student details
CONSTRAINT ckname CHECK (conditions) and their rank according to the fees_paid, grouped by
); ALTER TABLE tablename DROP gender:
constraint_type constraintname; SELECT
Drop Table:
student_id, first_name, last_name,
DROP TABLE tablename; Rename Table
gender, fees_paid,
ALTER TABLE tablename RENAME TO
Create Temporary Table: RANK() OVER (PARTITION BY gender ORDER
newtablename;
CREATE GLOBAL TEMPORARY TABLE tname ( BY fees_paid) AS rank_val
FROM student;
colname data_type Indexes
) ON COMMIT DELETE ROWS;
Create Index: CASE Statement
CREATE INDEX indexname ON tablename
Simple Case:
Alter Table (cols);
CASE name
Add Column Drop Index: WHEN 'John' THEN 'Name John'
ALTER TABLE tablename ADD columnname DROP INDEX indexname; WHEN 'Steve' THEN 'Name Steve'
datatype; ELSE 'Unknown'
END
Drop Column
ALTER TABLE tablename DROP COLUMN Set Operators Searched Case:
columnname;
UNION: Shows unique rows from two result sets.
SQL Cheat Sheet - Oracle - ​www.databasestar.com

CASE
WHEN name='John' THEN 'Name John' COUNT: Finds the number of records ROUND(input_val, round_to): Rounds a number to a
WHEN name='Steve' THEN 'Name Steve' specified number of decimal places.
AVG: Finds the average of the numbers provided
ELSE 'Unknown'
TRUNC(input_value, dec_or_fmt): Truncates a
END MIN: Finds the lowest of the numbers provided number or date to a number of decimals or format.
With Clause/Common Table Expression MAX: Finds the highest of the numbers provided REPLACE(whole_string, string_to_replace,
WITH queryname AS ( [replacement_string]): Replaces one string inside the
SELECT col1, col2 Common Functions whole string with another string.
FROM firsttable)
LENGTH(string): Returns the length of the provided SUBSTR(string, start_position, [length]): Returns part
SELECT col1, col2..
string of a value, based on a position and length.
FROM queryname…;
INSTR(string, substring, [start_position],
Subqueries Common Format Masks
[occurrence]): Returns the position of the substring
Single Row: within the specified string. YYYY: 4 digit year
SELECT id, last_name, salary
FROM employee TO_CHAR(input_value, [fmt_mask], [nls_param]): YY: 2 digit year
WHERE salary = ( Converts a date or a number to a string
SELECT MAX(salary) MM: Month (01 to 12)
TO_DATE(charvalue, [fmt_mask], [nls_date_lang]):
FROM employee
Converts a string to a date value. MON: Abbreviated month name
);
TO_NUMBER(input_value, [fmt_mask], [nls_param]): D: Day of week (1 to 7)
Multi Row
Converts a string value to a number.
SELECT id, last_name, salary DAY: Name of day
FROM employee ADD_MONTHS(input_date, num_months): Adds a
WHERE salary IN ( DD: Day of month (01 to 31)
number of months to a specified date.
SELECT salary
FROM employee SYSDATE: Returns the current date, including time. DY: Abbreviated day name
WHERE last_name LIKE 'C%'
); CEIL(input_val): Returns the smallest integer greater HH: Hour of day (01 to 12)
than the provided number.
MI: Minute of hour (00 to 59)
FLOOR(input_val): Returns the largest integer less
SS: Second of minute (00 to 59)
Aggregate Functions than the provided number.

SUM: Finds a total of the numbers provided

You might also like