SQL Cheatsheet-1
SQL Cheatsheet-1
Subqueries
ELSE 'Unknown' string to a date value.
END TO_NUMBER(input_value, [fmt_mask], [nls_param]): Converts a
string value to a number.
Single Row SELECT id, last_name, salary
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.
SYSDATE: Returns the current date, including time. WHERE salary = (
WHEN name='Steve' THEN 'Name Steve' SELECT MAX(salary)
ELSE 'Unknown' CEIL(input_val): Returns the smallest integer greater than the
provided number. FROM employee
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
Multi Row SELECT id, last_name, salary
FROM employee
number of decimal places.
WHERE salary IN
TRUNC(input_value, dec_or_fmt): Truncates a number or date to a
WITH queryname AS ( SELECT salary
number of decimals or format.
( SELECT col1, FROM employee
REPLACE(whole_string, string_to_replace, [replacement_string]):
col2 FROM WHERE last_name LIKE 'C%'
Replaces one string inside the whole string with another string.
firsttable) );
SUBSTR(string, start_position, [length]): Returns part of a value,
SELECT col1, col2..
based on a position and length.
FROM queryname...;
SQL Server Cheat Sheet
SELECT Query Modifying Data Create Table
SELECT col1, col2
FROM table Insert INSERT INTO tablename CreateTable 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...
A A
Set Operators 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
table, and any matching records from INTERSECT: Shows rows that
right table. B B exist in both result sets. Rename Table ALTER TABLE tablename
RENAME TO newtablename;
C
Create Table
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
INSERT INTO tablename
HAVING condition Table
ORDER BY col1 ASC|DESC; (col1, col2...) Create Table with Constraints
SELECT col1, col2...
Subqueries
ELSE 'Unknown' NOW: Returns the current date, including time.
END CEIL(input_val): Returns the smallest integer greater than the
provided number.
Single Row SELECT id, last_name, salary
Searched Case CASE FLOOR(input_val): Returns the largest integer less than the
FROM employee
WHEN name='John' THEN 'Name John' provided number.
ROUND(input_val, [round_to]): Rounds a number to a specified WHERE salary = (
WHEN name='Steve' THEN 'Name Steve' SELECT MAX(salary)
number of decimal places.
ELSE 'Unknown' FROM employee
TRUNC(input_value, num_decimals): Truncates a number to a
END
number of decimals. );
REPLACE(whole_string, string_to_replace, replacement_string):
Common Table Expression Replaces one string inside the whole string with another string. Multi Row SELECT id, last_name, salary
FROM employee
SUBSTRING(string, [start_pos], [length]): Returns part of a value,
based on a position and length. WHERE salary IN
WITH queryname AS ( SELECT salary
( SELECT col1, FROM employee
col2 FROM WHERE last_name LIKE 'C%'
firsttable) );
SELECT col1, col2..
FROM queryname...;