Select Modifying Data: SQL Cheat Sheet - SQL Server
Select Modifying Data: SQL Cheat Sheet - SQL Server
com
Create Table Modify Column UNION ALL: Shows all rows from two result sets.
ALTER TABLE tablename ALTER COLUMN
Create Table: INTERSECT: Shows rows that exist in both result
columnname newdatatype;
CREATE TABLE tablename ( sets.
column_name data_type Rename Column
); --SQL Server EXCEPT: Shows rows that exist in the first result set
sp_rename 'table_name.old_column_name', but not the second.
Create Table WIth Constraints:
'new_column_name', 'COLUMN';
CREATE TABLE tablename ( Analytic Functions
column_name data_type NOT NULL, Add Constraint function_name ( arguments ) OVER (
CONSTRAINT pkname PRIMARY KEY (col), ALTER TABLE tablename ADD CONSTRAINT [query_partition_clause]
CONSTRAINT fkname FOREIGN KEY (col) constraintname constrainttype (columns); [ORDER BY order_by_clause
REFERENCES
[windowing_clause] ] )
other_table(col_in_other_table), Drop Constraint
CONSTRAINT ucname UNIQUE (col), ALTER TABLE tablename DROP CONSTRAINT Example using RANK, showing the student details
CONSTRAINT ckname CHECK (conditions) constraintname; and their rank according to the fees_paid, grouped by
); gender:
ALTER TABLE tablename DROP SELECT
Drop Table: constraint_type constraintname; student_id, first_name, last_name,
DROP TABLE tablename;
gender, fees_paid,
Rename Table
Create Temporary Table: RANK() OVER (PARTITION BY gender ORDER
sp_rename 'old_table_name',
SELECT cols BY fees_paid) AS rank_val
'new_table_name';
INTO #tablename FROM student;
FROM table; Indexes
CASE Statement
Alter Table Create Index:
Simple Case:
CREATE INDEX indexname ON tablename
Add Column CASE name
(cols);
ALTER TABLE tablename ADD columnname WHEN 'John' THEN 'Name John'
datatype; Drop Index: WHEN 'Steve' THEN 'Name Steve'
DROP INDEX indexname; ELSE 'Unknown'
Drop Column END
ALTER TABLE tablename DROP COLUMN
columnname; Set Operators Searched Case:
CASE
WHEN name='John' THEN 'Name John' COUNT: Finds the number of records DATEDIFF(interval, date1, date2): Returns the
WHEN name='Steve' THEN 'Name Steve' difference between two dates in specified interval.
AVG: Finds the average of the numbers provided Date2>date1 is positive.
ELSE 'Unknown'
END MIN: Finds the lowest of the numbers provided
Date Format Codes
With Clause/Common Table Expression MAX: Finds the highest of the numbers provided
100: mon dd yyyy hh:mi AM (Default)
WITH queryname (col1, col2…) AS (
SELECT column1, column2 Common Functions 101: mm/dd/yyyy (US)
FROM firsttable)
LEN(string): Returns the length of the provided string 102: yyyy.mm.dd (ANSI)
SELECT col1, col2..
FROM queryname…; CHARINDEX(string, substring, [start_position], 103: dd/mm/yy (British)
[occurrence]): Returns the position of the substring
Subqueries within the specified string. 109: mon dd yyyy hh:mi:ss:mmm AM (Milliseconds)
Single Row:
SELECT id, last_name, salary CAST(expression AS type [(length)]): Converts an 110: mm-dd-yyyy (US)
FROM employee expression to another data type.
112: yyyymmdd (ISO)
WHERE salary = (
GETDATE: Returns the current date, including time.
SELECT MAX(salary) 114: hh:mi:ss:mmm
FROM employee CEILING(input_val): Returns the smallest integer
);
greater than the provided number.
Multi Row
FLOOR(input_val): Returns the largest integer less
SELECT id, last_name, salary
than the provided number.
FROM employee
WHERE salary IN ( ROUND(input_val, round_to, operation): Rounds a
SELECT salary number to a specified number of decimal places.
FROM employee
WHERE last_name LIKE 'C%' REPLACE(whole_string, string_to_replace,
); replacement_string): Replaces one string inside the
whole string with another string.