100% found this document useful (1 vote)
59 views

SQL 20

This document provides a cheat sheet for common Oracle SQL statements and functions. It summarizes SELECT queries, modifying data with INSERT, UPDATE, and DELETE statements, creating and altering tables, and using aggregate functions, joins, and window/analytic functions. Keywords like SELECT, DISTINCT, BETWEEN, IN, and LIKE are described for querying data, and syntax is shown for INSERT, UPDATE, DELETE, CREATE TABLE, ALTER TABLE, and other SQL statements.

Uploaded by

ttt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
59 views

SQL 20

This document provides a cheat sheet for common Oracle SQL statements and functions. It summarizes SELECT queries, modifying data with INSERT, UPDATE, and DELETE statements, creating and altering tables, and using aggregate functions, joins, and window/analytic functions. Keywords like SELECT, DISTINCT, BETWEEN, IN, and LIKE are described for querying data, and syntax is shown for INSERT, UPDATE, DELETE, CREATE TABLE, ALTER TABLE, and other SQL statements.

Uploaded by

ttt
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Oracle SQL Cheat Sheet

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...

CREATE TABLE tablename (


SELECT Keywords Insert Multiple
Rows
INSERT
INTO tablename (col1, col2) column_name data_type NOT NULL,
VALUES (valA1, valB1) CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name INTO tablename (col1, col2) CONSTRAINT fkname FOREIGN KEY (col)
duplicate results FROM product; VALUES (valA2, valB2) REFERENCES other_table(col_in_other_table),
SELECT * FROM dual; CONSTRAINT ucname UNIQUE (col),
CONSTRAINT ckname CHECK (conditions)
BETWEEN: Matches a SELECT product_name
Update UPDATE tablename );
value between two FROM product
other values (inclusive) WHERE price BETWEEN 50 AND 100; SET col1 = val1
WHERE condition;
Create Temporary CREATE GLOBAL TEMPORARY TABLE
SELECT product_name Table tablename (
IN: Matches to any of
FROM product Update with UPDATE t colname datatype
the values in a list
WHERE category IN a Join SET col1 = val1 ) ON COMMIT DELETE ROWS;
('Electronics', 'Furniture'); FROM tablename t
INNER JOIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name ON t.id = x.tid
wildcard matches using FROM product WHERE condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
WHERE condition;
Add Column ALTER TABLE tablename
ADD columnname datatype;
Joins Indexes
Drop Column ALTER TABLE tablename
Create Index CREATE INDEX indexname DROP COLUMN columnname;
SELECT t1.*, t2.*
ON tablename (cols);
FROM t1
join_type t2 ON t1.col = t2.col; Modify Column ALTER TABLE tablename MODIFY
Drop Index DROP INDEX indexname; columnname newdatatype;
Table 1 Table 2

A A
Set Operators Rename Column ALTER TABLE tablename RENAME COLUMN
currentname TO newname;
B B

C D UNION: Shows unique


Add Constraint ALTER TABLE tablename ADD
rows from two result sets. CONSTRAINT constraintname
INNER JOIN: show all matching A constrainttype (columns);
A
records in both tables. UNION ALL: Shows all
B B
rows from two result sets.
Drop Constraint ALTER TABLE tablename DROP
constraint_type constraintname;
LEFT JOIN: show all records from left A A
INTERSECT: Shows rows that
table, and any matching records from Rename Table sp_rename
B B exist in both result sets.
right table. 'old_table_name',
C
'new_table_name';
EXCEPT: Shows rows that exist
RIGHT JOIN: show all records from in the first result set but not
Window/Analytic Functions
A A
right table, and any matching records the second.
B B
from left table.
function_name ( arguments ) OVER (
Aggregate Functions
D
[query_partition_clause]
[ORDER BY order_by_clause
FULL JOIN: show all records from A A SUM: Finds a total of the numbers provided
[windowing_clause] ] )
both tables, whether there is a match COUNT: Finds the number of records
or not. B B AVG: Finds the average of the numbers provided
Example using RANK, showing the student details and their rank
C MIN: Finds the lowest of the numbers provided
according to the fees_paid, grouped by gender:
MAX: Finds the highest of the numbers provided
D
SELECT
Common Functions student_id, first_name, last_name, gender, fees_paid,

CASE Statement LENGTH(string): Returns the length of the provided string RANK() OVER (
INSTR(string, substring, [start_position], [occurrence]): Returns the PARTITION BY gender ORDER BY fees_paid
position of the substring within the specified string. ) AS rank_val
Simple Case CASE name TO_CHAR(input_value, [fmt_mask], [nls_param]): Converts a date FROM student;
WHEN 'John' THEN 'Name John' or a number to a string
WHEN 'Steve' THEN 'Name Steve' TO_DATE(charvalue, [fmt_mask], [nls_date_lang]): Converts a
ELSE 'Unknown'
END
string to a date value.
TO_NUMBER(input_value, [fmt_mask], [nls_param]): Converts a
Subqueries
string value to a number. SELECT id, last_name, salary
Single Row
Searched Case CASE ADD_MONTHS(input_date, num_months): Adds a number of FROM employee
WHEN name='John' THEN 'Name John' months to a specified date. WHERE salary = (
WHEN name='Steve' THEN 'Name Steve' SYSDATE: Returns the current date, including time. SELECT MAX(salary)
ELSE 'Unknown' CEIL(input_val): Returns the smallest integer greater than the FROM employee
END provided number. );
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
Multi Row SELECT id, last_name, salary
FROM employee
number of decimal places. WHERE salary IN (
WITH queryname AS ( TRUNC(input_value, dec_or_fmt): Truncates a number or date to a SELECT salary
SELECT col1, col2 number of decimals or format. FROM employee
FROM firsttable) REPLACE(whole_string, string_to_replace, [replacement_string]): WHERE last_name LIKE 'C%'
SELECT col1, col2.. Replaces one string inside the whole string with another string. );
FROM queryname...; SUBSTR(string, start_position, [length]): Returns part of a value,
based on a position and length.
SQL Server Cheat Sheet
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...

CREATE TABLE tablename (


SELECT Keywords Insert Multiple
Rows
INSERT INTO tablename
(col1, col2...) VALUES column_name data_type NOT NULL,
(valA1, valB1), CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name (valA2, valB2), CONSTRAINT fkname FOREIGN KEY (col)
duplicate results FROM product; (valA3, valB3); REFERENCES other_table(col_in_other_table),
CONSTRAINT ucname UNIQUE (col),
CONSTRAINT ckname CHECK (conditions)
BETWEEN: Matches a SELECT product_name
Update UPDATE tablename );
value between two FROM product
other values (inclusive) WHERE price BETWEEN 50 AND 100; SET col1 = val1
WHERE condition;
Create Temporary SELECT cols
SELECT product_name Table INTO #tablename
IN: Matches to any of
FROM product Update with UPDATE t FROM table;
the values in a list
WHERE category IN a Join SET col1 = val1
('Electronics', 'Furniture'); FROM tablename t
INNER JOIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name ON t.id = x.tid
wildcard matches using FROM product WHERE condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
WHERE condition;
Add Column ALTER TABLE tablename
ADD columnname datatype;
Joins Indexes
Drop Column ALTER TABLE tablename
Create Index CREATE INDEX indexname DROP COLUMN columnname;
SELECT t1.*, t2.*
ON tablename (cols);
FROM t1
join_type t2 ON t1.col = t2.col; Modify Column ALTER TABLE tablename ALTER COLUMN
Drop Index DROP INDEX indexname; columnname newdatatype;
Table 1 Table 2

Set Operators
A A Rename Column sp_rename
'table_name.old_column_name',
B B
'new_column_name', 'COLUMN';
C D UNION: Shows unique
rows from two result sets. Add Constraint ALTER TABLE tablename ADD
CONSTRAINT constraintname
INNER JOIN: show all matching A A constrainttype (columns);
records in both tables. UNION ALL: Shows all
B B
rows from two result sets.
Drop Constraint ALTER TABLE tablename
DROP CONSTRAINT constraintname;
LEFT JOIN: show all records from left A A
INTERSECT: Shows rows that
table, and any matching records from ALTER TABLE tablename
B B exist in both result sets. Rename Table
right table. RENAME TO newtablename;
C

MINUS: Shows rows that exist


RIGHT JOIN: show all records from in the first result set but not
Window/Analytic Functions
A A
right table, and any matching records the second.
B B
from left table.
D
Aggregate Functions function_name ( arguments ) OVER (
[query_partition_clause]
SUM: Finds a total of the numbers provided [ORDER BY order_by_clause
FULL JOIN: show all records from A A
COUNT: Finds the number of records [windowing_clause] ] )
both tables, whether there is a match
or not. B B AVG: Finds the average of the numbers provided
MIN: Finds the lowest of the numbers provided Example using RANK, showing the student details and their rank
C according to the fees_paid, grouped by gender:
MAX: Finds the highest of the numbers provided
D
SELECT
student_id, first_name, last_name, gender, fees_paid,
Common Functions
CASE Statement
RANK() OVER (
PARTITION BY gender ORDER BY fees_paid
LEN(string): Returns the length of the provided string ) AS rank_val
Simple Case CASE name CHARINDEX(string, substring, [start_position], [occurrence]): FROM student;
WHEN 'John' THEN 'Name John' Returns the position of the substring within the specified string.
WHEN 'Steve' THEN 'Name Steve' CAST(expression AS type [(length)]): Converts an expression to
ELSE 'Unknown' another data type.
END GETDATE: Returns the current date, including time. Subqueries
CEILING(input_val): Returns the smallest integer greater than the
provided number. Single Row SELECT id, last_name, salary
Searched Case CASE FROM employee
FLOOR(input_val): Returns the largest integer less than the
WHEN name='John' THEN 'Name John' WHERE salary = (
provided number.
WHEN name='Steve' THEN 'Name Steve' SELECT MAX(salary)
ROUND(input_val, round_to, operation): Rounds a number to a
ELSE 'Unknown' FROM employee
specified number of decimal places.
END );
REPLACE(whole_string, string_to_replace, replacement_string):
Replaces one string inside the whole string with another string.
Common Table Expression SUBSTRING(string, start_position, [length]): Returns part of a
value, based on a position and length.
Multi Row SELECT id, last_name, salary
FROM employee
WHERE salary IN (
WITH queryname (col1, col2...) AS ( SELECT salary
SELECT col1, col2 FROM employee
FROM firsttable) WHERE last_name LIKE 'C%'
SELECT col1, col2.. );
FROM queryname...;
MySQL Cheat Sheet
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...

CREATE TABLE tablename (


SELECT Keywords Insert Multiple
Rows
INSERT INTO tablename (col1,
col2…) column_name data_type NOT NULL,
VALUES CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name (valA1, valB1), CONSTRAINT fkname FOREIGN KEY (col)
duplicate results FROM product; (valA2, valB2), REFERENCES other_table(col_in_other_table),
(valA3, valB3); CONSTRAINT ucname UNIQUE (col),
CONSTRAINT ckname CHECK (conditions)
BETWEEN: Matches a SELECT product_name
Update UPDATE tablename );
value between two FROM product
other values (inclusive) WHERE price BETWEEN 50 AND 100; SET col1 = val1
WHERE condition;
Create Temporary CREATE TEMPORARY TABLE
SELECT product_name Table tablename (
IN: Matches to any of
FROM product Update with UPDATE t colname datatype
the values in a list
WHERE category IN a Join SET col1 = val1 );
('Electronics', 'Furniture'); FROM tablename t
INNER JOIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name ON t.id = x.tid
wildcard matches using FROM product WHERE condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
WHERE condition;
Add Column ALTER TABLE tablename
ADD columnname datatype;
Joins Indexes
Drop Column ALTER TABLE tablename
Create Index CREATE INDEX indexname DROP COLUMN columnname;
SELECT t1.*, t2.*
ON tablename (cols);
FROM t1
join_type t2 ON t1.col = t2.col; Modify Column ALTER TABLE tablename CHANGE
Drop Index DROP INDEX indexname; columnname newcolumnname newdatatype;
Table 1 Table 2

A A
Set Operators Rename Column ALTER TABLE tablename CHANGE
COLUMN currentname TO newname;
B B

C D UNION: Shows unique


Add Constraint ALTER TABLE tablename ADD
rows from two result sets. CONSTRAINT constraintname
INNER JOIN: show all matching A constrainttype (columns);
A
records in both tables. UNION ALL: Shows all
B B
rows from two result sets.
Drop Constraint ALTER TABLE tablename DROP
constraint_type constraintname;
LEFT JOIN: show all records from left A A
INTERSECT: Shows rows that
table, and any matching records from ALTER TABLE tablename
B B exist in both result sets. Rename Table
right table. RENAME TO newtablename;
C

MINUS: Shows rows that exist


RIGHT JOIN: show all records from in the first result set but not
Window/Analytic Functions
A A
right table, and any matching records the second.
B B
from left table.
D function_name ( arguments ) OVER (

Aggregate Functions [query_partition_clause]


[ORDER BY order_by_clause
FULL JOIN: show all records from A A
[windowing_clause] ] )
both tables, whether there is a match
SUM: Finds a total of the numbers provided
or not. B B
COUNT: Finds the number of records Example using RANK, showing the student details and their rank
C AVG: Finds the average of the numbers provided according to the fees_paid, grouped by gender:
MIN: Finds the lowest of the numbers provided
D
MAX: Finds the highest of the numbers provided SELECT
student_id, first_name, last_name, gender, fees_paid,

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.
PostgreSQL Cheat Sheet
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...

CREATE TABLE tablename (


SELECT Keywords Insert Multiple
Rows
INSERT INTO tablename
(col1, col2...) VALUES column_name data_type NOT NULL,
(valA1, valB1), CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name (valA2, valB2), CONSTRAINT fkname FOREIGN KEY (col)
duplicate results FROM product; (valA3, valB3); REFERENCES other_table(col_in_other_table),
CONSTRAINT ucname UNIQUE (col),
CONSTRAINT ckname CHECK (conditions)
BETWEEN: Matches a SELECT product_name
Update UPDATE tablename );
value between two FROM product
other values (inclusive) WHERE price BETWEEN 50 AND 100; SET col1 = val1
WHERE condition;
Create Temporary CREATE TEMP TABLE tablename (
SELECT product_name Table colname datatype
IN: Matches to any of
FROM product Update with UPDATE t );
the values in a list
WHERE category IN a Join SET col1 = val1
('Electronics', 'Furniture'); FROM tablename t
INNER JOIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name ON t.id = x.tid
wildcard matches using FROM product WHERE condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
WHERE condition;
Add Column ALTER TABLE tablename ADD COLUMN
columnname datatype;
Joins Indexes
Drop Column ALTER TABLE tablename DROP COLUMN
Create Index CREATE INDEX indexname columnname;
SELECT t1.*, t2.*
ON tablename (cols);
FROM t1
join_type t2 ON t1.col = t2.col; Modify Column ALTER TABLE tablename ALTER COLUMN
Drop Index DROP INDEX indexname; columnname TYPE newdatatype;
Table 1 Table 2

A A
Set Operators Rename Column ALTER TABLE tablename RENAME COLUMN
currentname TO newname;
B B

C D UNION: Shows unique


Add Constraint ALTER TABLE tablename ADD CONSTRAINT
rows from two result sets. constraintname constrainttype
INNER JOIN: show all matching A (columns);
A
records in both tables. UNION ALL: Shows all
B B
rows from two result sets.
Drop Constraint ALTER TABLE tablename DROP
constraint_type constraintname;
LEFT JOIN: show all records from left A A
INTERSECT: Shows rows that
table, and any matching records from Rename Table ALTER TABLE tablename
B B exist in both result sets.
right table. RENAME TO newtablename;
C

EXCEPT: Shows rows that exist


RIGHT JOIN: show all records from in the first result set but not
Window/Analytic Functions
A A
right table, and any matching records the second.
B B
from left table.
function_name ( arguments ) OVER (
Aggregate Functions
D
[query_partition_clause]
[ORDER BY order_by_clause
FULL JOIN: show all records from A A SUM: Finds a total of the numbers provided
[windowing_clause] ] )
both tables, whether there is a match COUNT: Finds the number of records
or not. B B AVG: Finds the average of the numbers provided
Example using RANK, showing the student details and their rank
C MIN: Finds the lowest of the numbers provided
according to the fees_paid, grouped by gender:
MAX: Finds the highest of the numbers provided
D
SELECT

Common Functions
student_id, first_name, last_name, gender, fees_paid,

CASE Statement
RANK() OVER (
LENGTH(string): Returns the length of the provided string PARTITION BY gender ORDER BY fees_paid
POSITION(string IN substring): Returns the position of the ) AS rank_val
Simple Case CASE name FROM student;
substring within the specified string.
WHEN 'John' THEN 'Name John'
CAST(expression AS datatype): Converts an expression into the
WHEN 'Steve' THEN 'Name Steve'
specified data type.
ELSE 'Unknown'
END
NOW: Returns the current date, including time.
CEIL(input_val): Returns the smallest integer greater than the
Subqueries
provided number. SELECT id, last_name, salary
Single Row
Searched Case CASE FLOOR(input_val): Returns the largest integer less than the FROM employee
WHEN name='John' THEN 'Name John' provided number. WHERE salary = (
WHEN name='Steve' THEN 'Name Steve' ROUND(input_val, [round_to]): Rounds a number to a specified SELECT MAX(salary)
ELSE 'Unknown' number of decimal places. FROM employee
END TRUNC(input_value, num_decimals): Truncates a number to a );
number of decimals.

Common Table Expression SELECT id, last_name, salary


REPLACE(whole_string, string_to_replace, replacement_string): Multi Row
Replaces one string inside the whole string with another string. FROM employee
SUBSTRING(string, [start_pos], [length]): Returns part of a value, WHERE salary IN (
WITH queryname AS ( based on a position and length. SELECT salary
SELECT col1, col2 FROM employee
FROM firsttable) WHERE last_name LIKE 'C%'
SELECT col1, col2.. );
FROM queryname...;

You might also like