Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
24 views

SQL Cheat Sheet

Uploaded by

Durga prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

SQL Cheat Sheet

Uploaded by

Durga prasad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Oracle SQL Cheat Sheet

SELECT Query Modifying Data Create Table


S E L E C T col1, col2
FROM table Insert INSERT INTO tablename Create Table CREATE TABLE tablename (
J O I N table2 O N table1.col = table2.col (col1, col2...) column_name
W HER E condition VA L U E S ( v a l 1 , v a l 2 ) ; d a t a _ t yp e ) ;
GROUP BY column_name
Insert from a
H AV IN G condition INSERT INTO tablename
Table
ORDER BY col1 ASC|DESC; (col1, col2...) Create Table with Constraints
S E L E C T col1, col2...

Insert Multiple INSERT CREATE TABLE tablename (


SELECT Keywords Rows I N T O tablename (col1, col2) column_name data_type N O T NULL,
VA LU E S (valA1, valB1) CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name I N T O tablename (col1, col2) CONSTRAINT fkname FOREIGN KEY (col)
duplicate results F R O M product; VA LU E S (valA2, valB2) R E F E R E N C E S other_table(col_in_other_table),
SELECT * FROM dual; CONSTRAINT ucname UNIQUE (col),
BETWEEN: Matches a SELECT product_name CONSTRAINT ckname CHECK (conditions)
value between two FROM product );
Update UPDATE tablename
other values (inclusive) WHERE price BETWEEN 50 AND 100; S E T col1 = val1
W H E R E condition;
Create Temporary CREATE GLOBAL TEMPORARY TABLE
SELECT product_name Table tablename (
IN: Matches to any of
FROM product Update with UPDATE t c o l n a m e datatype
the values in a list
WHERE category IN a Join S E T col1 = val1
('Electronics', 'Furniture'); ) ON COMMIT DELETE ROWS;
F R O M tablename t
INNER J OIN table x Drop Table
DROP TABLE tablename;
LIKE: Performs SELECT product_name O N t.id = x.tid
wildcard matches using FROM product W H E R E condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
W H E R E condition;
Add Column ALTER TABLE tablename
A D D c o l u m n n a m e datat ype;
Joins Indexes
Drop Column ALTER TABLE tablename
Create Index CREATE INDEX indexname DROP COLUMN columnname;
S E L E C T t1.*, t2.*
FROM t1 O N t ab l en am e (cols);
join_typ e t2 O N t1.col = t2.col; Modify Column ALTER TABLE tablename MODIFY
Drop Index DROP INDEX indexname; c o l u m n n a m e n e w d a t a t yp e ;
Table 1 Table 2

Rename Column ALTER TABLE tablename RENAME COLUMN


A A Set Operators currentname T O newname;
B B

C
UNION: Shows unique Add Constraint ALTER TABLE tablename ADD
D
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 ' o l d _ t a b l e _ n a m e ' ,
B exist in both result sets.
right table. B
'new_table_name';
C
EXCEPT: Shows rows that exist
RIGHT JOIN: show all records from A in the first result set but not
right table, and any matching records
B
A
the second. Window/Analytic Functions
from left table. B
function_name ( arguments ) O V E R
D Aggregate Functions ( [query_partition_clause]
[ O R D E R B Y order_by_clause
FULL JOIN: show all records from A SUM: Finds a total of the numbers provided
A [windowing_clause] ] )
both tables, whether there is a match COUNT: Finds the number of records
or not. B AVG: Finds the average of the numbers provided
B Example using RANK, showing the student details and their rank
MIN: Finds the lowest of the numbers provided
C 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,
LENGTH(string): Returns the length of the provided string RANK() OVER (
CASE Statement INSTR(string, substring, [start_position], [occurrence]): Returns the PARTITION BY gender ORDER BY fees_paid
position of the substring within the specified string. ) A S rank_val
Simple Case CASE name TO_CHAR(input_value, [fmt_mask], [nls_param]): Converts a date F R O M student;
W H E N 'John' THEN 'Name John' or a number to a string
W H E N 'Steve' T H E N 'Name Steve' TO_DATE(charvalue, [fmt_mask], [nls_date_lang]): Converts a
ELSE 'Unknown' string to a date value.
END TO_NUMBER(input_value, [fmt_mask], [nls_param]): Converts a
Subqueries
string value to a number. S E L E C T id, last_name, salary
CASE Single Row
Searched Case ADD_MONTHS(input_date, num_months): Adds a number of FROM employee
W H E N name='John' THEN 'Name John' months to a specified date. W H E R E salary = (
W H E N name='Steve' T H E N '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. S E L E C T id, last_name, salary
Common Table Expression ROUND(input_val, round_to): Rounds a number to a specified
Multi Row
FROM employee
number of decimal places. W HER E salary IN (
WITH queryname AS ( TRUNC(input_value, dec_or_fmt): Truncates a number or date to a S E LE C T salary
S E LE C T col1, col2 number of decimals or format. FROM employee
F R O M firsttable) REPLACE(whole_string, string_to_replace, [replacement_string]): WHERE last_name LIKE
S E L E C T col1, col2.. Replaces one string inside the whole string with another string. 'C%' ) ;
F R O M querynam e...; SUBSTR(string, start_position, [length]): Returns part of a value,
based on a position and length.
SQL Server Cheat Sheet
SELECT Query Modifying Data Create Table
S E L E C T col1, col2
FROM table Insert INSERT INTO tablename Create Table CREATE TABLE tablename (
J O I N table2 O N table1.col = table2.col (col1, col2...) column_name
W HER E condition VA L U E S ( v a l 1 , v a l 2 ) ; d a t a _ t yp e ) ;
GROUP BY column_name
Insert from a
H AV IN G condition INSERT INTO tablename
Table
ORDER BY col1 ASC|DESC; (col1, col2...) Create Table with Constraints
S E L E C T col1, col2...

Insert Multiple INSERT INTO tablename CREATE TABLE tablename (


SELECT Keywords Rows (col1, col2...) VA L U E S column_name data_type N O T NULL,
(valA1, valB1), CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name (valA2, valB2), CONSTRAINT fkname FOREIGN KEY (col)
duplicate results F R O M product; (valA3, valB3); R E F E R E N C E S other_table(col_in_other_table),
CONSTRAINT ucname UNIQUE (col),
CONSTRAINT ckname CHECK
BETWEEN: Matches a SELECT product_name
FROM product Update UPDATE tablename (conditions) ) ;
value between two
other values (inclusive) WHERE price BETWEEN 50 AND 100; S E T col1 = val1
W H E R E condition;
Create Temporary SELECT cols
SELECT product_name Table I N T O #t abl en a m e
IN: Matches to any of
FROM product Update with UPDATE t F R O M table;
the values in a list
WHERE category IN a Join S E T col1 = val1
('Electronics', 'Furniture'); F R O M tablename t
INNER J OIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name O N t.id = x.tid
wildcard matches using FROM product W H E R E condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
W H E R E condition;
Add Column ALTER TABLE tablename
A D D c o l u m n n a m e datat ype;
Joins Indexes
Drop Column ALTER TABLE tablename
Create Index CREATE INDEX indexname DROP COLUMN columnname;
S E L E C T t1.*, t2.*
FROM t1 O N t ab l en am e (cols);
join_typ e t2 O N t1.col = t2.col; Modify Column ALTER TABLE tablename ALTER COLUMN
Drop Index DROP INDEX indexname; c o l u m n n a m e n e w d a t a t yp e ;
Table 1 Table 2
Rename Column sp_rename
A A Set Operators 'table_name.old_column_name',
B B 'new_col um n_nam e', ' C O LU M N ' ;
UNION: Shows unique
C D
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 exist in both result sets. Rename Table
right table. B
RENAME TO newtablename;
C
MINUS: Shows rows that exist
RIGHT JOIN: show all records from A in the first result set but not
right table, and any matching records
B
A
the second. Window/Analytic Functions
from left table. B
function_name ( arguments ) O V E R
D Aggregate Functions ( [query_partition_clause]
[ O R D E R B Y order_by_clause
FULL JOIN: show all records from A SUM: Finds a total of the numbers provided
A [windowing_clause] ] )
both tables, whether there is a match COUNT: Finds the number of records
or not. B AVG: Finds the average of the numbers provided
B Example using RANK, showing the student details and their rank
MIN: Finds the lowest of the numbers provided
C MAX: Finds the highest of the numbers provided according to the fees_paid, grouped by gender:
D SELECT
student_id, first_name, last_name, gender, fees_paid,
Common Functions RANK() OVER (
CASE Statement PARTITION BY gender ORDER BY fees_paid
LEN(string): Returns the length of the provided string ) A S rank_val
Simple Case CASE name CHARINDEX(string, substring, [start_position], [occurrence]): F R O M student;
W H E N 'John' THEN 'Name John' Returns the position of the substring within the specified string.
W H E N 'Steve' T H E N '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 S E L E C T id, last_name, salary
Searched Case CASE provided number. Single Row
FLOOR(input_val): Returns the largest integer less than the FROM employee
W H E N name='John' THEN 'Name John'
provided number. W H E R E salary = (
W H E N name='Steve' T H E N 'Name Steve'
SELECT MAX(salary)
ELSE 'Unknown' ROUND(input_val, round_to, operation): Rounds a number to a
FROM employee
END specified number of decimal places.
);
REPLACE(whole_string, string_to_replace, replacement_string):
Replaces one string inside the whole string with another string.
S E L E C T id, last_name, salary
Common Table Expression SUBSTRING(string, start_position, [length]): Returns part of a Multi Row
FROM employee
value, based on a position and length.
W HER E salary IN (
W I T H q u e r yn a m e (col1, col2...) A S ( S E LE C T salary
S E LE C T col1, col2 FROM employee
F R O M firsttable) WHERE last_name LIKE
S E L E C T col1, col2.. 'C%' ) ;
F R O M querynam e...;
MySQL Cheat Sheet
SELECT Query Modifying Data Create Table
S E L E C T col1, col2
FROM table Insert INSERT INTO tablename Create Table CREATE TABLE tablename (
J O I N table2 O N table1.col = table2.col (col1, col2...) column_name
W HER E condition VA L U E S ( v a l 1 , v a l 2 ) ; d a t a _ t yp e ) ;
GROUP BY column_name
Insert from a
H AV IN G condition INSERT INTO tablename
Table
ORDER BY col1 ASC|DESC; (col1, col2...) Create Table with Constraints
S E L E C T col1, col2...

Insert Multiple I N S E RT I N T O tablename (col1, CREATE TABLE tablename (


SELECT Keywords Rows col2…) column_name data_type N O T NULL,
VALUES CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name (valA1, valB1), CONSTRAINT fkname FOREIGN KEY (col)
duplicate results F R O M product; (valA2, valB2), R E F E R E N C E S other_table(col_in_other_table),
(valA3, valB3); CONSTRAINT ucname UNIQUE (col),
BETWEEN: Matches a SELECT product_name CONSTRAINT ckname CHECK (conditions)
value between two FROM product UPDATE tablename );
Update
other values (inclusive) WHERE price BETWEEN 50 AND 100; S E T col1 = val1
W H E R E 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 S E T col1 = val1 );
('Electronics', 'Furniture'); F R O M tablename t
INNER J OIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name O N t.id = x.tid
wildcard matches using FROM product W H E R E condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
W H E R E condition;
Add Column ALTER TABLE tablename
A D D c o l u m n n a m e datat ype;
Joins Indexes
Drop Column ALTER TABLE tablename
Create Index CREATE INDEX indexname DROP COLUMN columnname;
S E L E C T t1.*, t2.*
FROM t1 O N t ab l en am e (cols);
join_typ e t2 O N t1.col = t2.col; Modify Column ALTER TABLE tablename CHANGE
Drop Index DROP INDEX indexname; c o l u m n n a m e n e w c o l u m n n a m e newdatat ype;
Table 1 Table 2

Rename Column ALTER TABLE tablename CHANGE


A A Set Operators COLUMN currentname TO newname;
B B

C
UNION: Shows unique Add Constraint ALTER TABLE tablename ADD
D
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 exist in both result sets. Rename Table
right table. B
RENAME TO newtablename;
C
MINUS: Shows rows that exist
RIGHT JOIN: show all records from A in the first result set but not
right table, and any matching records
B
A
the second. Window/Analytic Functions
from left table. B
function_name ( arguments ) O V E R
D
( [query_partition_clause]
Aggregate Functions [ O R D E R B Y 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
AVG: Finds the average of the numbers provided according to the fees_paid, grouped by gender:
C
MIN: Finds the lowest of the numbers provided
D SELECT
MAX: Finds the highest of the numbers provided
student_id, first_name, last_name, gender, fees_paid,
RANK() OVER (
CASE Statement PARTITION BY gender ORDER BY fees_paid
Common Functions ) A S rank_val
Simple Case CASE name F R O M student;
W H E N 'John' THEN 'Name John' LENGTH(string): Returns the length of the provided string
W H E N 'Steve' T H E N '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.
S E L E C T id, last_name, salary
ADDDATE(input_date, days): Adds a number of days to a Single Row
Searched Case CASE
specified date. FROM employee
W H E N name='John' THEN 'Name John' W H E R E salary = (
NOW: Returns the current date, including time.
W H E N name='Steve' T H E N 'Name Steve' SELECT MAX(salary)
CEILING(input_val): Returns the smallest integer greater than
ELSE 'Unknown' FROM employee
END the provided number.
FLOOR(input_val): Returns the largest integer less than the );
provided number.
S E L E C T id, last_name, salary
Common Table Expression ROUND(input_val, [round_to]): Rounds a number to a specified
number of decimal places.
Multi Row
FROM employee
TRUNCATE(input_value, num_decimals): Truncates a number to W HER E salary IN (
WITH queryname AS ( S E LE C T salary
a number of decimals.
S E LE C T col1, col2 FROM employee
REPLACE(whole_string, string_to_replace, replacement_string):
F R O M firsttable) WHERE last_name LIKE
Replaces one string inside the whole string with another string.
S E L E C T col1, col2.. 'C%' ) ;
SUBSTRING(string, start_position): Returns part of a value,
F R O M querynam e...;
based on a position and length.
PostgreSQL Cheat Sheet
SELECT Query Modifying Data Create Table
S E L E C T col1, col2
FROM table Insert INSERT INTO tablename Create Table CREATE TABLE tablename (
J O I N table2 O N table1.col = table2.col (col1, col2...) column_name
W HER E condition VA L U E S ( v a l 1 , v a l 2 ) ; d a t a _ t yp e ) ;
GROUP BY column_name
Insert from a
H AV IN G condition INSERT INTO tablename
Table
ORDER BY col1 ASC|DESC; (col1, col2...) Create Table with Constraints
S E L E C T col1, col2...

Insert Multiple INSERT INTO tablename CREATE TABLE tablename (


SELECT Keywords Rows (col1, col2...) VA L U E S column_name data_type N O T NULL,
(valA1, valB1), CONSTRAINT pkname PRIMARY KEY (col),
DISTINCT: Removes SELECT DISTINCT product_name (valA2, valB2), CONSTRAINT fkname FOREIGN KEY (col)
duplicate results F R O M product; (valA3, valB3); R E F E R E N C E S other_table(col_in_other_table),
CONSTRAINT ucname UNIQUE (col),
CONSTRAINT ckname CHECK (conditions)
BETWEEN: Matches a SELECT product_name
FROM product Update UPDATE tablename );
value between two
other values (inclusive) WHERE price BETWEEN 50 AND 100; S E T col1 = val1
W H E R E 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 S E T col1 = val1
('Electronics', 'Furniture'); F R O M tablename t
INNER J OIN table x Drop Table DROP TABLE tablename;
LIKE: Performs SELECT product_name O N t.id = x.tid
wildcard matches using FROM product W H E R E condition;
_ or % WHERE product_name
Delete DELETE FROM tablename
Alter Table
LIKE '%Desk%";
W H E R E condition;
Add Column ALTER TABLE tablename ADD COLUMN
c o l u m n n a m e d a t a t yp e ;
Joins Indexes
Drop Column ALTER TABLE tablename DROP COLUMN
Create Index CREATE INDEX indexname c o l um nnam e;
S E L E C T t1.*, t2.*
FROM t1 O N t ab l en am e (cols);
join_typ e t2 O N t1.col = t2.col; Modify Column ALTER TABLE tablename ALTER COLUMN
Drop Index DROP INDEX indexname; col um nnam e T Y P E newdatatype;
Table 1 Table 2

Rename Column ALTER TABLE tablename RENAME COLUMN


A A Set Operators currentname T O newname;
B B

C
UNION: Shows unique Add Constraint ALTER TABLE tablename ADD CONSTRAINT
D
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 exist in both result sets.
right table. B
RENAME TO newtablename;
C
EXCEPT: Shows rows that exist
RIGHT JOIN: show all records from A in the first result set but not
right table, and any matching records
B
A
the second. Window/Analytic Functions
from left table. B
function_name ( arguments ) O V E R
D Aggregate Functions ( [query_partition_clause]
[ O R D E R B Y order_by_clause
FULL JOIN: show all records from A SUM: Finds a total of the numbers provided
A [windowing_clause] ] )
both tables, whether there is a match COUNT: Finds the number of records
or not. B AVG: Finds the average of the numbers provided
B Example using RANK, showing the student details and their rank
MIN: Finds the lowest of the numbers provided
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 RANK() OVER (
CASE Statement 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 ) A S rank_val
Simple Case CASE name F R O M student;
substring within the specified string.
W H E N 'John' THEN 'Name John'
CAST(expression AS datatype): Converts an expression into the
W H E N 'Steve' T H E N '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. S E L E C T id, last_name, salary
Single Row
Searched Case CASE FLOOR(input_val): Returns the largest integer less than the FROM employee
W H E N name='John' THEN 'Name John' provided number. W H E R E salary = (
W H E N name='Steve' T H E N '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.
REPLACE(whole_string, string_to_replace, replacement_string): S E L E C T id, last_name, salary
Common Table Expression Replaces one string inside the whole string with another string.
Multi Row
FROM employee
SUBSTRING(string, [start_pos], [length]): Returns part of a value, W HER E salary IN (
WITH queryname AS ( based on a position and length. S E LE C T salary
S E LE C T col1, col2 FROM employee
F R O M firsttable) WHERE last_name LIKE
S E L E C T col1, col2.. 'C%' ) ;
F R O M querynam e...;

You might also like