Show Menu
Cheatography

MSSQL basic Statements Cheat Sheet by

Because I never recall how to write anything after "UPDATE".

SELECT ... JOIN ... WHERE

SELECT
a.COL1
AS 'a1',
 
a.COL2
AS 'a2',
 
c.COL1
AS 'c1'
FROM
TBL1 a
(WHATEVER) JOIN
TBL2 b
ON b.X = a.Y
(WHATEVER) JOIN
TBL3 c
ON c.K = b.K
WHERE
a.K = b.K
AND
c.Z = 'foobar'
GROUP BY
a.COL1,
-- basically specify
 
a.COL2,
-- all selected columns
 
c.COL1
-- when "­GROUP BY"
ORDER BY
b.Q

("su­b-q­uer­y")

WHERE
COL1 IN (
 
SELECT
COL1
 
FROM
TBL337 x
 
(WHATEVER) JOIN
...
 
WHERE
x.Z = 'sub'
 
)
 

CTE

WITH
TEMP
AS (
 
SELECT
foo
 
FROM
tbl1
 
UNION ALL
 
SELECT
b.foo
 
FROM
TEMP a
 
INNER JOIN
tbl1 b
 
ON
b.foo
= a.foo
)
SELECT
*
FROM
TEMP
OPTION
(MAXRE­CURSION 5)

UPDATE ... JOIN ... WHERE

UPDATE
TBL1
SET
COL1 = 'GOD',
 
COL2 = 'AWFUL'
FROM
TBL1 a
(WHATEVER) JOIN
TBL2 b
ON b.X = a.Y
WHERE
b.Q
IN ('bull', 'shit')
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Web Programming Cheat Sheet
          SQL Server Cheat Sheet
          SQL Cheat Sheet