SQL Server / T-SQL examples (example source code) Organized by topic





Select Query /

ALL 1AND 1ANY 1AS 3
Between AND 5Bitwise Operator 1Case 9Column Alias 4
Compare Date 2Data Type 1Date Calculation 1Date Compare 1
DESC 1DISTINCT 3EXISTS 5Group BY 9
HAVING 6IN 6IS NULL 2ISNULL 6
LIKE 10Logical Operator 7Math Operator 2MAXRECURSION 1
NOT 4NULL 3Operator Precedence 2OR 2
Order Columns 5Order 11Query Batch 1Query Columns 6
Quotation Mark 1Select Into 3Select 13String Compare 1
Top percent 4Top With Ties 1Top 7Where 9
With 2

Subquery /

Correlated subquery 6Nested Subquery 2RTRIM 4Subqueries ALL 1
Subqueries ANY 2Subqueries Exists 4Subqueries IN 5Subqueries 4
Subquery Aggregate Functions 2Subquery From 1

Insert Delete Update /

Batch Insert 2BULK INSERT 1Delete 6Insert Image 1
Insert Select 4Insert 5Update 15

Data Set /

EXCEPT 1INTERSECT 1UNION ALL 1Union 3

Store Procedure Function /

Create Function 5Create Procedure 13Default Parameter 3Drop Procedure 1
EXEC 6EXECUTE 2Function Call 2Function Parameters 1
Function Return 7Procedure Call 3Procedure Parameters 2Recursive function 1
Scalar Functions 1

Transact SQL /

Assign Value 4ATAN 1Break 2COALESCE 1
Code Block 2Continue 2Declare Variables 5Delete Data 1
Dynamic SQL 4Error 4GOTO 1Handle Error 1
If 7Insert Data 6LOG 2Object Property 1
Print 1Query Data 2RAISERROR 2Return 2
RowCount 3schemas 1Select Data 1Select Variables 2
Set Variables 4Statement 1Table Variable 5Update Data 5
Variable Scope 2Variables 1Version 1WAITFOR 2
While 5

Trigger /

Alter Trigger 1Create Trigger 3Disable Trigger 1Enable Disable Trigger 1
inserted table 1INSTEAD OF Trigger 3Trigger after 2Trigger for Insert 1
Trigger for update 4Trigger Value 2UPDATE function 1

Table /

Add Column 3Alter Table 2Column Data Type 1Column Index 1
Computed Column 2Copy Data 1Create Table 1Derived Columns 1
Drop Column 2Drop Table 3Rename Column 1Rename Table 1
Sub query 2Table Alias 1Table Copy 1Table Existence 1
Table Properties 1Temporary Table 3TRUNCATE TABLE 2

Table Joins /

ANSI Join Syntax 3Cross Join 3INNER JOIN 4Left Join 2
LEFT OUTER JOIN 1RIGHT JOIN 1RIGHT OUTER JOIN 2Self Join 1
SQL Server Join Syntax 2Table Join 10

View /

Alter View 2Create View 13Drop View 2ENCRYPTION 1
Filter view 2Inline view 2Nested View 1Query View 1
SCHEMABINDING 1Updatable View 2View Existance 1

Index /

CLUSTERED INDEX 2Create Index 6Drop Index 1DROP_EXISTING 1

Database /

Create Database 3Database Info 1Database Option 7databases 2
DROP DATABASE 2USE 1

Transaction /

Commit Transaction 3Transaction Roll back 4XACT_ABORT 1

Cursor /

Declare CURSOR 3Fetch Data 2Fetch_Status 1Open cursor 2

Constraints /

Add Primary Key 2CASCADE 1Check Options 6Check value range 3
Column Constraints 5Constraints Basics 2CREATE RULE 1Default Value 3
Disable constraint 1Foreign Key 4Maintain Constaints 1Nullable 3
Primary key 3Unique 3UniqueIdentifier 1

Sequence /

IDENTITY 12IDENTITYCOL 1IDENT_CURRENT 2NEWID 1
Procedure Call 3Procedure Existance 1Procedure Permission 1Procedure with Parameters 13
Procedure 1

Data Type /

Bit 2Cast 21char 1Convert 8
Data Type Convert 1datetime 1decimal 7float 1
int 3Money 1nchar Data Type 1Number Format 2
text 1varchar 3

XML /

XML column 2

Date Timezone /

CURRENT_TIMESTAMP 1Date Data Type 2Date Format 12DATEADD 18
DATEDIFF 19DATENAME 14DATEPART 13Datetime 3
Day 4GetDate 4GETUTCDATE 2Int 1
ISDATE 6Month 4Year 4

Aggregate Functions /

Aggregate Function Basics 2AVG 4Count 6COUNT_BIG 2
MAX 3MIN 2SUM 2

Analytical Functions /

COMPUTE 5Cube 2DENSE RANK 2NTile 1
Over 1RANK 2Row Number 3STDEV 4
VAR 3VARP 2WITH ROLLUP 2

Math Functions /

ABS 2ACOS 2ASIN 1CEILING 3
COS 2COT 1DEGREES 2EXP 1
FLOOR 3ISNUMERIC 3Log10 2mod 1
PI 1Power 2RADIANS 1RAND 1
ROUND 9SIGN 1SIN 1SQRT 1
SQUARE 1TAN 1

String Functions /

ASCII 2CHAR 3CHARINDEX 5COLLATE 1
DATALENGTH 3Difference 4Escape 1LEFT 4
LEN 4Lower 2LTRIM 2NCHAR 2
PATINDEX 2QUOTENAME 1Regular Expressions 5REPLACE 3
REPLICATE 4REVERSE 2RIGHT 4SOUNDEX 2
Space 2STR 6STUFF 2Substring 4
UNICODE 1UPPER 3

System /

columns 2DATABASEPROPERTYEX 1DB_ID 1Error_Message 1
HOST_NAME 1INFORMATION_SCHEMA 3LOCK_TIMEOUT 1LOGIN 1
OBJECT_ID 1QUOTED_IDENTIFIER 2Row Count 2ROWCOUNT 1
SHOWPLAN_TEXT 2sp_addmessage 2sp_altermessage 1sp_dboption 1
sp_dropmessage 1sp_helpconstraint 1sp_helpindex 1sp_helptext 1
sp_unbindefault 1STATISTICS 1SUSER_SNAME 1sys.sql_modules 1
sys.syscacheobjects 1sys.types 1sys.views 1sys.XML_schema_collections 1
syscomments 1sysdepends 1sysmessages 2sysobjects 2
sysusers 1TOTAL_ERRORS 1TOTAL_READ 1USER 1
USER_NAME 1views 1