SQL Cheat Sheet: A Comprehensive Guide

SQL or Structured Query Language is a standard language used to manage and manipulate relational databases. It allows users to create, modify, and retrieve data from databases. SQL is widely used by developers, analysts, and data scientists worldwide. This cheat sheet is designed to provide a quick reference for the most commonly used SQL statements.

Pre-requisites

Before diving into the cheat sheet, it is important to have some understanding of databases, tables, and SQL syntax. Familiarity with basic programming concepts like variables, operators, and functions can also be helpful.

SQL Cheat Sheet

The following are the most commonly used SQL statements:

1. SQL SELECT Statement

The SELECT statement is used to retrieve data from one or more tables in a database. It is the most commonly used SQL statement. The basic syntax of the SELECT statement is as follows:

SELECT column_name(s) FROM table_name;

For example, to retrieve all data from a table called “employees,” we can use the following query:

SELECT * FROM employees;

2. SQL Aliases

An alias is a temporary name assigned to a table or column. It is used to make the output more readable. The basic syntax for using an alias is as follows:

SELECT column_name AS alias_name FROM table_name;

For example, to retrieve the names of all employees from the “employees” table and display them as “Name,” we can use the following query:

SELECT employee_name AS Name FROM employees;

3. SQL TOP Statement

The TOP statement is used to limit the number of rows returned in a query result. It is commonly used to retrieve the top or bottom “n” rows from a table. The basic syntax of the TOP statement is as follows:

SELECT TOP n column_name(s) FROM table_name;

For example, to retrieve the top 5 employees from the “employees” table, we can use the following query:

SELECT TOP 5 * FROM employees;

4. SQL ORDER BY Clause

The ORDER BY clause is used to sort the result set by one or more columns in ascending or descending order. The basic syntax of the ORDER BY clause is as follows:

SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC|DESC;

For example, to retrieve all employees from the “employees” table and sort them by their salary in descending order, we can use the following query:

SELECT * FROM employees ORDER BY salary DESC;

5. SQL COUNT() Function

The COUNT() function is used to count the number of rows in a table or a specific column. The basic syntax of the COUNT() function is as follows:

SELECT COUNT(column_name) FROM table_name;

For example, to count the number of employees in the “employees” table, we can use the following query:

SELECT COUNT(*) FROM employees;

6. SQL DISTINCT Operator

The DISTINCT operator is used to retrieve unique values from a column. The basic syntax of the DISTINCT operator is as follows:

SELECT DISTINCT column_name FROM table_name;

For example, to retrieve all unique job titles from the “employees” table, we can use the following query:

SELECT DISTINCT job_title FROM employees;

7. SQL Join Operations

Join operations are used to combine data from two or more tables based on a related column. There are different types of join operations, including INNER JOIN, LEFT JOIN, RIGHT JOIN, and FULL OUTER JOIN. The basic syntax of the INNER JOIN operation is as follows:

SELECT column_name(s) FROM table1 INNER JOIN table2 ON table1.column_name = table2.column_name;

For example, to retrieve all orders and their corresponding customer names from the “orders” and “customers” tables, we can use the following query:

SELECT orders.order_id, customers.customer_name FROM orders INNER JOIN customers ON orders.customer_id = customers.customer_id;

8. SQL Basic Filter Operations

Filter operations are used to retrieve data that meets a specific condition. The basic filter operators in SQL include the following:

= (equal to)
< (less than)
(greater than)

<= (less than or equal to)
= (greater than or equal to)

<> or != (not equal to)

For example, to retrieve all employees from the “employees” table whose salary is greater than 50000, we can use the following query:

SELECT * FROM employees WHERE salary > 50000;

9. SQL Server LIKE Operator

The LIKE operator is used to search for a pattern in a column. It is commonly used with wildcard characters, such as % (any number of characters) and _ (a single character). The basic syntax of the LIKE operator is as follows:

SELECT column_name(s) FROM table_name WHERE column_name LIKE pattern;

For example, to retrieve all employees from the “employees” table whose last name starts with “S,” we can use the following query:

SELECT * FROM employees WHERE last_name LIKE 'S%';

10. SQL Server IN Keyword

The IN keyword is used to specify multiple values in a WHERE clause. The basic syntax of the IN keyword is as follows:

SELECT column_name(s) FROM table_name WHERE column_name IN (value1, value2, ...);

For example, to retrieve all employees from the “employees” table whose job title is either ‘Manager’ or ‘Director,’ we can use the following query:

SELECT * FROM employees WHERE job_title IN ('Manager', 'Director');

11. SQL Server AND Operator

The AND operator is used to retrieve data that meets multiple conditions. The basic syntax of the AND operator is as follows:

SELECT column_name(s) FROM table_name WHERE condition1 AND condition2;

For example, to retrieve all employees from the “employees” table whose job title is ‘Manager’ and whose salary is greater than 50000, we can use the following query:

SELECT * FROM employees WHERE job_title = 'Manager' AND salary > 50000;

12. SQL Server OR Operator

The OR operator is used to retrieve data that meets at least one of multiple conditions. The basic syntax of the OR operator is as follows:

SELECT column_name(s) FROM table_name WHERE condition1 OR condition2;

For example, to retrieve all employees from the “employees” table whose job title is either ‘Manager’ or ‘Director,’ we can use the following query:

SELECT * FROM employees WHERE job_title = 'Manager' OR job_title = 'Director';

13. SQL Server BETWEEN Operator

The BETWEEN operator is used to retrieve data that falls within a range of values. The basic syntax of the BETWEEN operator is as follows:

SELECT column_name(s) FROM table_name WHERE column_name BETWEEN value1 AND value2;

For example, to retrieve all employees from the “employees” table whose salary is between 50000 and 70000, we can use the following query:

SELECT * FROM employees WHERE salary BETWEEN 50000 AND 70000;

This SQL cheat sheet provides a quick reference for the most commonly used SQL statements. Understanding these statements is essential for managing and manipulating relational databases.

Leave a Reply