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






Select Query /

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

Subquery /

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

Insert Delete Update /

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

Data Set /

EXCEPT 1INTERSECT 1
UNION ALL 1Union 3

Store Procedure Function /

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

Transact SQL /

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

Trigger /

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

Table /

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

Table Joins /

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

View /

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

Index /

CLUSTERED INDEX 2Create Index 6
Drop Index 1DROP_EXISTING 1

Database /

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

Transaction /

Commit Transaction 3Transaction Roll back 4
XACT_ABORT 1

Cursor /

Declare CURSOR 3Fetch Data 2
Fetch_Status 1Open cursor 2

Constraints /

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

Sequence /

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

Data Type /

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

XML /

XML column 2

Date Timezone /

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

Aggregate Functions /

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

Analytical Functions /

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

Math Functions /

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

String Functions /

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

System /

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