TechAid24

Your Technical Consultant

SQL Server Cheat Sheet for Programmers & Engineers – Essential Commands & Queries

Posted on by Anonymous Guest 4 minutes read

The SQL Server Cheat Sheet provides you with the syntax of all basic data types, type casting, and functions, shows you how to create tables, views, and data related to different conditions, and has examples. So, let’s start SQL Server Cheat Sheet.

 

What is a Database?

To get introduced to SQL, first, we need to know about Data and Databases. Data is basically a collection of facts related to some object. A Database is a collection of small units of data arranged in a systematic manner. 

 

What is SQL?

After getting the concept of data and databases, we can finally learn about SQL. SQL (Structured Query Language) is basically the language that we (the user) use to communicate with the Databases and get our required interpretation of data out of it. It is used for storing, manipulating and retrieving data out of a database.

 

Coloumn Type
PurposeData TypeExample
Integersint(M)int(5)
Floati­ng-­point (real) numbersfloat(M,D)float(­12,3)
Double­-pr­ecision floati­ng-­pointdouble­(M,D)double­(20,3)
Dates and timestimest­amp(M)timest­amp(8) (for YYYYMMDD)
Fixed-­length stringschar(M)char(10)
Variab­le-­length stringsvarchar(M)varcha­r(20)
A large amount of textblobblob
Values chosen from a listenum(‘­val­ue1­’,v­alu­e2’­,…)enum(‘­app­les­’,’­ora­nge­s’,­’ba­nanas’)

 

Mathem­atical Functions
WhatHow
Count rows per groupCOUNT(­column | *)
Average value of groupAVG(co­lumn)
Minumum value of groupMIN(co­lumn)
Maximum value of groupMAX(co­lumn)
Sum values in a groupSUM(co­lumn)
Absolute valueabs(nu­mber)
Rounding numbersround(­number)
Largest integer not greaterfloor(­number)
Smallest integer not smallerceilin­g(n­umber)
Square rootsqrt(n­umber)
nth powerpow(ba­se,­exp­onent)
random number n, 0<n < 1rand()
sin (similar cos, etc.)sin(nu­mber)

 

String Functions
WhatHow
Compare stringsstrcmp­(st­rin­g1,­str­ing2)
Convert to lower caselower(­string)
Convert to upper caseupper(­string)
Left-trim whitespace (similar right)ltrim(­string)
Substring of stringsubstr­ing­(st­rin­g,i­nde­x1,­index2)
Encrypt passwordpasswo­rd(­string)
Encode stringencode­(st­rin­g,key)
Decode stringdecode­(st­rin­g,key)
Get datecurdate()
Get timecurtime()
Extract day name from date stringdaynam­e(s­tring)
Extract day number from date stringdayofw­eek­(st­ring)
Extract month from date stringmonthn­ame­(st­ring)

 

Date Functions
DATEADD (datepart, number , date)
DATEDIFF (datepart , start , end)
DATENAME (datepart, date)
DATEPART (datepart, date)
DAY (date)
GETDATE()
GETUTC­DATE()
MONTH(­date)
YEAR(date)

 

Date Parts
Yearyy, yyyy
Quarterqq, q
Daydd, d
Day of Yeardy, y
Monthmm, m
Weekwk, ww
Hourhh
Minutemi, n
Secondss, s
Milisecondms

 

Type Conversion
CAST (expre­ssions AS datatype)
CONVERT (datatype , expres­sion)

 

Ranking Functions
RANKNTILE
DENSE_RANKROW_NUMBER

 

Gruoping Functions
AVGMAX
BINARY­_CH­ECKSUMMIN
CHECKSUMSUM
CHECKS­UM_AVGSTDEV
COUNTSTDEVP
COUNT_BIGVAR
GROUPINGVARP

 

Table Functios
ALTERDROP
CREATETRUNCATE

 

Create a Trigger
CREATE TRIGGER name
ON
table
FOR
DELETE, INSERT, UPDATE
AS
— Comments
SELECT * FROM table
GO

 

Create a View
CREATE VIEW name
AS
— Comments
SELECT FROM table*
GO

 

Create Table
CREATE TABLE table (
column1 type [[NOT] NULL]
[AUTO_­INC­REM­ENT],
column2 type [[NOT] NULL]
[AUTO_­INC­REM­ENT],
…
other options,
PRIMARY KEY (column(s)) );

 

Insert Data
INSERT INTO table VALUES
(list of values);
INSERT INTO table SET
column­1=v­alue1,
column­2=v­alue2,
…
column­k=v­aluek;
INSERT INTO table (colum­n1,­col­umn­2,…)
VALUES (value1­,value2…);

 

Insert­/Select
INSERT INTO *table (colum­n1,­col­umn­2,…)
SELECT statem­ent;*
(See below)

 

Delete data

DELETE FROM table
[WHERE condit­ion(s)];

(Omit WHERE to delete all data)

 

Updating Data
UPDATE table SET
column­1=v­alue1,
column­2=v­alue2,
…
column­k=v­aluek
[WHERE condit­ion(s)];

 

Insert column
ALTER TABLE table ADD COLUMN
column type options;

 

Delete Column
ALTER TABLE table
DROP COLUMN column;

 

Delete table (Care­ful!)
DROP TABLE [IF EXISTS] table;

 

Create an Index
CREATE UNIQUE INDEX name
ON
table (columns)

 

Create a Stored Procedure
CREATE PROCEDURE name
@variable AS datatype = value
AS
— Comments
SELECT FROM table*
GO
SQL Server Cheat Sheet

Tags:

Anonymous Guest

An anonymous author is a writer who conceals their true identity when publishing content, often for privacy, security, or confidentiality reasons. This approach is often used in technical articles to focus on the information being presented rather than the author's personal identity.

Leave a Reply

Your email address will not be published. Required fields are marked *