Sunteți pe pagina 1din 3

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

UNION: Shows unique rows from two result sets.


SQL Cheat Sheet - SQL Server - ​www.databasestar.com

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.

SUBSTRING(string, start_position, [length]): Returns


Aggregate Functions part of a value, based on a position and length.

SUM: Finds a total of the numbers provided

S-ar putea să vă placă și