Esat Erkec
Statement details of the SQL left join

SQL Cheat Sheet for Newbies

February 21, 2023 by

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.

SQL Cheat Sheet

Command

Syntax

Description

SELECT

SELECT col1,col2,col3,…,coln FROM Table_Name

Used to fetch data from a table

TOP

SELECT TOP(1) col1,col2,col3,…,coln FROM Table_Name

Used to limit the number of the result set

ORDER BY

SELECT col1,col2,col3,…,coln FROM Table_Name ORDER BY col1,col2,col3 …,coln

Used to sort result set according to specified columns in the ascending or descending order

COUNT()

SELECT COUNT(col1) FROM Table_Name

Used to determine how many rows the result set returns.

DISTINCT

SELECT DISTINCT col1 FROM Table_Name

Used to fetch distinct values of the specified column(s).

INNER JOIN

SELECT col1,col2,col3,…,coln FROM Table_A INNER JOIN Table_B ON Table_A.col = Table_B.col

Used to fetch matched rows of TableA and TableB

LEFT JOIN

SELECT col1,col2,col3,…,coln FROM Table_A LEFT JOIN Table_B ON Table_A.col = Table_B.col

Used to fetch all rows of TableA and matched rows from TableB

RIGHT JOIN

SELECT col1,col2 FROM Table_A RIGHT JOIN Table_B ON Table_A.col = Table_B.col

Used to fetch all rows of TableB and matched rows from TableA

WHERE

SELECT col1,col2,col3,…,coln FROM Table_Name WHERE col1=’col_value’

Used to filter the result set of the table

LIKE

SELECT col1,col2,col3,…,coln FROM Table_Name WHERE col1 LIKE ‘col_value%’

Used to filter wildcard patterns.

IN

SELECT col1,col2,col3,…,coln FROM Table_Name WHERE col1 IN (‘col_value1′,’col_value2′,’col_value3′,…,’col_valuen’)

Used to filter multiple values ​​for a column.

AND

SELECT col1,col2,col3,…,coln FROM Table_Name WHERE col1 = col_value’ AND col2=’col_value’

Used to combine two or more conditions in filtering. The matched row(s) must satisfy all conditions

OR

SELECT col1,col2,col3,…,coln FROM Table_Name WHERE col1 = col_value’ OR col2=’col_value’

Used to combine two or more conditions in filtering. The matched row(s) is enough to satisfy one of the conditions.

BETWEEN

SELECT col1,col2,col3,…,coln FROM Table_Name WHERE col1 BETWEEN ‘col_value1’ AND ‘col_value2’

Used to filter the result set according to the given result set

SQL SELECT Statement

The SELECT statement is used to fetch data from any database table. The syntax of the SQL SELECT statement is:

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.

Getting data from tables with a Select statement

If we want to retrieve all columns of the table we can use an asterisk sign (*) in the SELECT statements.

Getting data from tables with a Select statement to use the asteriks sign.

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.

SQL Cheat Sheet.

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.

SQL TOP Statement

The SELECT TOP statement is used to limit the row numbers of the query resultset. For example, the following query will return only 2 rows of the customer table randomly.

Result set

SQL ORDER BY Clause

The ORDER BY statement is used to sort the fetched result set of the query in either ascending or descending according to one or more columns. For example, the following query will sort the result set of the query according to “CustomerName” in a descending manner.

How to use Order By statement in the SQL queries.

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.

Usage details of the SQL Count function in SQL Server

SQL DISTINCT Operator

The DISTINCT operator allows us to retrieve only distinct values of the specified columns in the queries. As we can the following query only returns the different values of the city names.

Usage details of the SQL DISTINCT operator in SQL Server

SQL Join Operations

Due to the nature of the relational database relationship approach, we do not store all data in one table. Because of this, we need to create a result set by combining the data we should get data from different tables. This is exactly the point, joining the tables will help us. Commonly, we use 3 different joining methods in SQL. Now let’s look at these 3 different join types which are involved in our SQL Cheat Sheet.

The inner join allows joined tables to return rows that match each other.

SQL inner join illustration

The following query will return the customers who have only orders.

Statement details of the SQL inner join

The left join allows returning all rows from the left table and any matching records from the right table.

SQL Left Join illustration

The following query will return all customers and their matched orders of them.

Statement details of the SQL left join

The right join allows returning all rows from the right table and any matching records from the right table.

SQL Right Join ilusturation

The following query will return all orders and their matched customers of them.

Statement details of the SQL right join

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.

Filtering data in SQL Server with equal operator

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.

Filtering data in SQL Server with non-equal operator

We can use the following operators in T-SQL to filter out the rows of the tables.

Operator

Description

=

Equals to

<>

Not Equal

!=

Not Equal

>

Greater than

>=

Greater than to equals to

<

Less than

<=

Less than or equal to

Tip: There is no difference between the “<>” sign and “!=”, we can use both of them as non-equal operators.

SQL Server LIKE Operator

The LIKE operator is a logical operator and filters the matched record in the table according to a specified string pattern.

The percent wildcard (%) indicates zero or more characters. The following query returns the customers whose name starts with an “S” character.

Percentage sign operator usage in SQL Server

The underscore (_) wildcard indicates exactly one character in a string pattern.

Underscore sign operator usage in SQL Server

SQL Server IN Keyword

We’ll now take a look at another operator from the SQL Cheat Sheet. The IN operator allows us to filter out multiple values in the WHERE clause. The values must be entered in parentheses and separated with a comma sign.

Table

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.

SQL Cheat Sheet details.

SQL Server OR Operator

The OR operator is used to combine two or more two conditions in the where clause and returns data that satisfies any of the conditions.

OR operator usage details in SQL Server

SQL Server BETWEEN Operator

The BETWEEN operator filters the values within a given range.

SQL Server BETWEEN operator usage details.

Summary

In this article, we looked at a SQL Cheat Sheet that helps to learn the fundamentals of T-SQL queries.

Esat Erkec
Latest posts by Esat Erkec (see all)
SQL commands, T-SQL

About 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. View all posts by Esat Erkec

168 Views