SQL Cheat Sheet
SQL Cheat Sheet
Skip to content
Español
In this SQL cheat sheet, we’ll look at sample SQL queries that can help you learn basic T-SQL
queries as quickly as possible.
Introduction
Transact-SQL (T-SQL) is an extension of the Structured Query Language (SQL) that is used to
manipulate and fetch data from the Microsoft SQL Server. Despite the clear and rigid
specifications of standard SQL, it does allow database vendors to add their extensions to set
them apart from other products. Moving from this idea, T-SQL had been developed by Microsoft
to program SQL Server and it has a common widely-usage. This SQL cheat will allow you to learn
SQL queries quickly and simply.
Pre-requisites
To practice the examples on this SQL Cheat Sheet, you can use the following query to create the
tables and also populate them with some synthetic data. Besides this option, you can use
this SQL Fiddle link, to practice exercises online.
1
9 Customer_Id INT,
10
11 Order_Total float,
12 Discount_Rate float,
13 Order_Date DATETIME)
14 GO
21 VALUES(N'Salvador', N'Philadelphia',
N'tyiptqo.wethls@chttw.org')
22
INSERT INTO [dbo].Online_Customers(Customer_Name,
23
Customer_City, Customer_Mail)
24
VALUES(N'Gilbert', N'San Diego', N'rrvyy.wdumos@lklkj.org')
25
INSERT INTO [dbo].Online_Customers(Customer_Name,
26 Customer_City, Customer_Mail)
41 GO
GO
Used to
fetch
data
SELEC SELECT col1,col2,col3,…,coln FROM from a
T Table_Name table
Used to
limit the
number
SELECT TOP(1) col1,col2,col3,…,coln of the
TOP FROM Table_Name result set
Used to
determin
e how
many
rows the
COUNT SELECT COUNT(col1) FROM result set
() Table_Name returns.
Used to
fetch
distinct
values of
the
specified
DISTIN SELECT DISTINCT col1 FROM column(s
CT Table_Name ).
Used to
fetch
matched
rows of
SELECT col1,col2,col3,…,coln FROM TableA
INNER Table_A INNER JOIN Table_B ON and
JOIN Table_A.col = Table_B.col TableB
Used to
fetch all
rows of
TableA
and
matched
SELECT col1,col2,col3,…,coln FROM rows
LEFT Table_A LEFT JOIN Table_B ON from
JOIN Table_A.col = Table_B.col TableB
Used to
fetch all
rows of
TableB
and
matched
SELECT col1,col2 FROM Table_A RIGHT rows
RIGHT JOIN Table_B ON Table_A.col = from
JOIN Table_B.col TableA
Used to
filter the
result set
SELECT col1,col2,col3,…,coln FROM of the
WHERE Table_Name WHERE col1=’col_value’ table
Used to
SELECT col1,col2,col3,…,coln FROM filter
Table_Name WHERE col1 LIKE wildcard
LIKE ‘col_value%’ patterns.
Used to
combine
two or
more
condition
s in
filtering.
The
matched
row(s)
must
satisfy
SELECT col1,col2,col3,…,coln FROM all
Table_Name WHERE col1 = col_value’ condition
AND AND col2=’col_value’ s
Used to
combine
two or
more
condition
s in
filtering.
The
matched
row(s) is
enough
to satisfy
one of
SELECT col1,col2,col3,…,coln FROM the
Table_Name WHERE col1 = col_value’ condition
OR OR col2=’col_value’ s.
Used to
filter the
result set
accordin
SELECT col1,col2,col3,…,coln FROM g to the
BETWE Table_Name WHERE col1 BETWEEN given
EN ‘col_value1’ AND ‘col_value2’ result set
In this syntax the Column_Name_1, Column_Name_2, ….., and Column_Name_N indicate the
column name of the tables and we should separate them with a comma (;)after placing the FROM
clause we need to write the table name which we want to fetch data.
1
If we want to retrieve all columns of the table we can use an asterisk sign (*) in the SELECT
statements.
1
Tip: Although using the asterisk sign (*) is included in our SQL Cheat Sheet, we recommend not
using it as much as possible as, because it may cause performance problems.
SQL Aliases
Aliases are the temporary names that we can give to table or column names. So that, we can
make them more readable and understandable. Using the aliases does not make any changes
neither to the original table name or its column names.
2 SELECT
6
As you can see, the column names of the result set have been changed after using aliases in the
above query because of the alias usage.
2 SELECT TOP(2)
2 SELECT
7
SQL COUNT() Function
In this part of the SQL Cheat Sheet, we will learn the COUNT() function. The SQL COUNT()
function returns the number of rows in the result set. For example, the following query will return
the number of rows in the Customer table.
2 SELECT
3 COUNT(Customer.Customer_City) AS [Row_Number]
The inner join allows joined tables to return rows that match each other.
The following query will return the customers who have only orders.
3 , o.Customer_Id AS [Customer_Id]
6 , o.Order_Total AS [Order_Total]
7 FROM Online_Customers AS oc
9 ON oc.Customer_Id = o.Customer_Id
10
The left join allows returning all rows from the left table and any matching records from the right
table.
The following query will return all customers and their matched orders of them.
3 , o.Customer_Id AS [Customer_Id]
6 , o.Order_Total AS [Order_Total]
7 FROM Online_Customers AS oc
9 ON oc.Customer_Id = o.Customer_Id
10
The right join allows returning all rows from the right table and any matching records from the
right table.
The following query will return all orders and their matched customers of them.
3 , o.Customer_Id AS [Customer_Id]
6 , o.Order_Total AS [Order_Total]
7 FROM Online_Customers AS oc
9 ON oc.Customer_Id = o.Customer_Id
10
SQL Server Basic Filter Operations
In this part of the SQL Cheat Sheet, we will look at how to filter out the data from a table for the
basic requirements.
Equal Operator (=): We use the equal operator to compare the values of the column with any
value. For example, if we want to return customers who are located in New York we can use the
following query.
1
2 SELECT
6 WHERE
7 Customer.Customer_City='New York'
Non-Equal Operator (<>): The Non-Equal operator works completely reverse of the equal
operator, and it returns all rows except rows equal to any value. For example, if we want to return
customers whose names are not equal to “Edward“, we can use the following query.
1
2 SELECT
6 WHERE
7 Customer.Customer_Name<> 'Ernest'
8
We can use the following operators in T-SQL to filter out the rows of the tables.
Operator Description
= Equals to
!= Not Equal
The percent wildcard (%) indicates zero or more characters. The following query returns the
customers whose name starts with an “S” character.
1
2 SELECT
6 WHERE
8
The underscore (_) wildcard indicates exactly one character in a string pattern.
1
2 SELECT
6 WHERE
2 SELECT
6 WHERE
8
SQL Server AND Operator
The AND operator is used to combine two or more two conditions in the where clause and it
enables to return of data that satisfies all the conditions criteria. For example, the following query
returns customers whose customer name is “Edward” and who live in the city of “Chicago”. For
any row to be included in the query’s result set, it must satisfy both conditions.
2 SELECT
6 WHERE
7 Customer.Customer_City ='Chicago'
8 AND Customer.Customer_Name='Edward'
2 SELECT
6 WHERE
7 Customer.Customer_City ='Chicago'
8 OR Customer.Customer_Name='Edward'
3 , o.Customer_Id AS [Customer_Id]
6 , o.Order_Total AS [Order_Total]
7 FROM Online_Customers AS oc
9 ON oc.Customer_Id = o.Customer_Id
11
Summary
In this article, we looked at a SQL Cheat Sheet that helps to learn the fundamentals of T-SQL
queries.
See more
Seamlessly integrate a powerful, SQL formatter into SSMS and/or Visual Studio with ApexSQL
Refactor. ApexSQL Refactor is a SQL query formatter but it can also obfuscate SQL, refactor
objects, safely rename objects and more – with nearly 200 customizable options
Esat Erkec
Esat Erkec is a SQL Server professional who began his career 8+ years ago as a Software Developer.
He is a SQL Server Microsoft Certified Solutions Expert.
Most of his career has been focused on SQL Server Database Administration and Development. His
current interests are in database administration and Business Intelligence. You can find him
on LinkedIn.
Related Posts:
1. In-Memory OLTP Series – Data migration guideline process on SQL Server 2016
2. How to create and configure a linked server in SQL Server Management Studio
3. Different ways to SQL delete duplicate rows from a SQL Table
4. Overview of Microsoft SQL Server Management Studio (SSMS)
5. Microsoft Clustering in SQL Server
2,426 Views
Follow us!
Popular
Solutions
Read a SQL Server transaction log
SQL Server database auditing techniques
How to recover SQL Server data from accidental UPDATE and DELETE operations
How to quickly search for SQL database data and objects
Synchronize SQL Server databases in different remote sources
Recover SQL data from a dropped table without backups
How to restore specific table(s) from a SQL Server database backup
Recover deleted SQL data from transaction logs
How to recover SQL Server data from accidental updates without backups
Automatically compare and synchronize SQL Server data
Open LDF file and view LDF file content
Quickly convert SQL code to language-specific client code
How to recover a single table from a SQL Server database backup
Recover data lost due to a TRUNCATE operation without backups
How to recover SQL Server data from accidental DELETE, TRUNCATE and DROP
operations
Reverting your SQL Server database back to a specific point in time
How to create SSIS package documentation
Migrate a SQL Server database to a newer version of SQL Server
How to restore a SQL Server database backup to an older version of SQL Server
Categories and tips
BI performance counters
SQL code smells rules
SQL Server wait types
© 2023 Quest Software Inc. ALL RIGHTS RESERVED. | GDPR | Terms of Use | Privacy