SQL Cheat Sheet (Download in PDF or Image Format)

SQL Cheat Sheet

This article features a fantastic SQL Cheat Sheet with the most commonly used SQL Commands. View it or download in the PDF or Image (JPG or PNG) format.

Our SQL Cheat Sheet enables you to revise almost the entire set of SQL Commands quickly. It is highly recommended to go through this cheat sheet if you are:

  • Preparing for your SQL exam
  • Going for an interview
  • Or you an absolute beginner who quickly wants to have a hold on the different SQL Commands.

Download SQL Cheat Sheet in PDF Format

Download SQL Cheat Sheet in PNG Format

Download SQL Cheat Sheet in JPG Format

Fetching Data from a Single Table

All of the demonstrated queries are fetching data from a table named abc.

Fetching data in columns, col_1, col_2 and col_3 from the table

Fetching data in all the columns of the table

Fetching data in columns, col_1, col_2 and col_3 from the table after filtering rows according to a condition.

Fetching only distinct rows from the table

Fetching rows in either Ascending or Descending order

Fetching n number of rows after skipping offset number of rows from the start

Using the aggregate function for grouping of rows

Fetching filtered rows using having clause

Fetching data from Multiple Tables

The queries are demonstrated for the two tabled named table1 and table2.

Note. Colum names are represented by col_1 and col_2 but any of these two columns may belong to either of the tables. table1.col_1, table2.col_1, can be used to fetch the data in columns if both of the tables contain the same column named col_1.

Applying inner join on table1 and table2

Left joining table1 with table2

Right joining table1 with table2

Applying Full Outer Join

Fetching rows generated by performing cartesian product

OR

Self Joining table1 on itself by applying INNER JOIN

SQL Operators

Fetching combined rows selected using two queries

Fetching the intersecting rows of the two queries

Subtracting the rows fetched using a query from another query

Fetching rows having column value matching or not with a given particular pattern

OR

Fetching rows having column value in a given list

Fetching rows having column value in a given range

Fetching rows having column values either NULL or not NULL

OR

Tables Management Commands

Following commands are illustrated with the database table named info.

Creating a new table named info with three different columns id, name and age.

Delete or drop a database table

Adding a new table column

Deleting a particular column from a given table

Adding a constraint to a given table

Deleting a constraint from a given table

Renaming a particular table

Renaming a particular table column

Removing all table data (Empty a database table)

Modifying Table Data

Inserting a single row in a table named info.

Inserting multiple rows into a table in a single query.

Inserting all rows from table2 into table1

Updating a particular column value in all of the table rows.

Updating multiple columns values in a single or multiple rows according to a given condition.

Deleting all of the table rows.

Deleting all of the table rows that satisfy a condition.

SQL Constraints

Creating table1 with three columns col_1, col_2 and col_3 such that col_1 and col_2 are the primary keys.

Creating a new table and setting a foreign key col_2 that refers to a column in another table.

Creating a table with UNIQUE value columns, col_1 and col_2

Creating a table with VALUE IN RANGE constraint

Creating a table with a column that should not contain NULL values

Views Management

Creating a view of two columns col_1 and col_2 of table1.

Creating a view along with the CHECK option.

Recursive View creation

The first select statement is the anchor part while the second select statement is the recursive part in the following query.

Temporary View Creation

Deleting a particular View

Index Management

Creating an index on the col_1 and col_2 of table1.

Creating a UNIQUE index on the col_1 and col_2 of table1.

Deleting a given index

Triggers Management

A Single query is written to demonstrate the creation of all the different possible triggers. It created a create trigger_1 on table1. You have to replace WHEN, EVENT and TYPE with the terms specified after the query.

WHEN

BEFORE to invoke the trigger before the event occurs and AFTER to invoke the trigger after the event occurs.

EVENT

INSERT, UPDATE or DELETE to perform the insertion, updation and deletion actions respectively.

TYPE

It can be FOR EACH ROW and FOR EACH STATEMENT

Deleting a given Trigger

SQL Functions

You can use these functions within queries to perform some sort of mathematical calculations for the table data or to perform some scalar operations. All of these function returns a single value, not a resultset. Further, you can use these functions in the queries to get resultsets, mostly, by applying conditions using WHERE or HAVING clause.

Aggregate Functions

  • AVG()
  • COUNT()
  • FIRST()
  • LAST()
  • MAX()
  • MIN()
  • SUM()

As their names suggest, these functions return the average, count, first, last, maximum, minimum and sum values of a list.

Scalar Functions

  • UCASE()
  • LCASE()
  • MID()
  • LEN()
  • ROUND()
  • NOW()
  • FORMAT()

These function also works as their name suggests.

I hope you find this guide useful. If so, do share it with others who are willing to learn SQL. If you have any questions related to this article, feel free to ask them in the comments section.

Also, don’t forget to Subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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