Isha Rani
Data & AI Leader
@isharanimicrosoftleader
SQL Cheatsheet
Data Definition Language Commands/Clauses
SELECT Select data from database
FROM Specify table we're pulling from
CREATE ALTER WHERE Filter query to match a condition
AS Rename column or table with alias
CREATE DATABASE MyDatabase; ALTER TABLE MyTable JOIN Combime rows for two or more tables
DROP COLUMN col5; AND Combine query conditions. All must be met
OR Combine query conditions. One must be met
ALTER TABLE MyTable
CREATE TABLE MyTable ( LIMIT Limit rows returned. See also FETCH & TOP
ADD col5 int ;
id int ,
IN Specify multiple values when using WHERE
name varchar (10) ) ;
CASE Return value on a specified condition
IS NULL Return only rows with a NULL value
CREATE INDEX IndexName
ON TableName (col1) ;
DROP LIKE Search for patterns in column
COMMIT Write transaction to database
DROP DATABASE MyDatabase ; ROLLBACK Undo a transaction block
DROP TABLE MyTable ;
ALTER TABLE Add/Remove columns from table
UPDATE Update table data
Data Manipulation CREATE
DELETE
Create TABLE, DATABASE, INDEX or VIEW
Delete rows from table
Language INSERT
DROP
Add single row to table
Delete TABLE, DATABASE or INDEX
UPDATE GROUP BY
ORDER BY
Group data into logical sets
Set order of result. Use DESC to reverse order
UPDATE MyTable HAVING Same as WHERE but filters groups
SET col1 = 56 COUNT Count number of rows
WHERE col2 = 'something' ; SUM Return sum of column
AVG Return average of column
MIN Return min value of column
INSERT MAX Return max value of column
INSERT INTO MyTable (col1 , col2)
Order of
VALUES ( 'value1' , 'value2' ) Joins
Execution
DELETE FROM
WHERE
DELETE FROM MyTable
WHERE col1 = 'something' ; Left Join
GROUP BY
HAVING
SELECT SELECT
SELECT col1, col2 Right Join ORDER BY
FROM MyTable ;
LIMIT