Addon - SQL Cheat Sheet
Addon - SQL Cheat Sheet
Explore this section to get hands on all the cheat sheet that help you in order
to create a database in SQL.
USE company;
This command selects the database named “company” for further operations.
This command deletes the database named “company” and all its associated
data.
This command inserts sample data into the “employees” table with values
for employee ID, first name, last name, department, and salary.
This command deletes the entire “employees” table along with all its data.
Reading/Querying Data in SQL
Explore this section to get the cheat sheet on how to use select, distinct and
other querying data in SQL.
This query will retrieve all columns from the employees table.
This query will return unique department names from the employees table.
This query will return employees whose salary is greater than 55000.00.
12. LIMIT: Limit The Number Of Rows Returned In The Result Set
This query will limit the result set to the first 3 rows.
This query will skip the first 2 rows and return the rest.
14. FETCH: Retrieve A Specified Number Of Rows From The Result Set
SELECT
first_name,
last_name,
CASE
WHEN salary > 55000 THEN 'High'
WHEN salary > 50000 THEN 'Medium'
ELSE 'Low'
END AS salary_category
FROM employees;
This query will categorize employees based on their salary into ‘High’,
‘Medium’, or ‘Low’.
UPDATE employees
SET salary = 55000.00
WHERE employee_id = 1;
This query will update the salary of the employee with employee_id 1 to
55000.00.
This query will delete the record of the employee with employee_id 5 from
the employees table.
Filtering Data in SQL
This query will retrieve all employees who work in the IT department.
This query will retrieve all employees whose first name starts with ‘J’.
This query will retrieve all employees who work in the HR or Finance
departments.
This query will retrieve all employees whose salary is between 50000 and
60000.
This query will retrieve all employees where the department is not assigned
(NULL).
23. ORDER BY: Sort The Result Set
This query will retrieve all employees sorted by salary in descending order.
SQL Operator
Here in this section we have added a cheat sheet for SQL Operators. So,
explore and learn how to use AND, OR, NOT and others oprtators.
This query will retrieve employees who work in the IT department and have
a salary greater than 60000.
25. OR: Specifies Multiple Conditions Where Any One Of Them Should
Be True
This query will retrieve employees who work in either the HR or Finance
department.
This query will retrieve employees who do not work in the IT department.
This query will retrieve employees whose salary is between 50000 and
60000.
This query will retrieve employees where the department is not assigned
(NULL).
31. ORDER BY: Sorts the Result Set in Ascending or Descending Order
This query will retrieve all employees sorted by salary in descending order.
32. GROUP BY: Groups Rows that have the Same Values into Summary
Rows
This query will find the minimum salary among all employees.
This query will find the maximum salary among all employees.
This query will group employees by department and count the number of
employees in each department.
This query will calculate the average salary for each department and return
only those departments where the average salary is greater than 55000.
Constraints in SQL
Constraints in SQL act as data quality guardrails, enforcing rules to ensure
accuracy, consistency, and integrity within your database tables.
43. NOT NULL: Ensures That a Column Does Not Contain NULL Values
first_name and last_name columns must have values and cannot be NULL.
age column must have a value of 18 or greater due to the CHECK constraint.
Joins in SQL
Explore different join types to seamlessly merge data from multiple tables in
your SQL queries.
45. INNER JOIN: Retrieves Records That Have Matching Values in Both
Tables
This query will retrieve records from both the employees and departments
tables where there is a match on the department_id column.
46. LEFT JOIN: Retrieves All Records from the Left Table and the
Matched Records from the Right Table
This query will retrieve all records from the employees table and only the
matching records from the departments table.
47. RIGHT JOIN: Retrieves All Records from the Right Table and the
Matched Records from the Left Table
This query will retrieve all records from the departments table and only the
matching records from the employees table.
48. FULL OUTER JOIN: Retrieves All Records When There Is a Match in
Either the Left or Right Table
This query will retrieve all records from both the employees and
departments tables, including unmatched records.
49. CROSS JOIN: Retrieves the Cartesian Product of the Two Tables
This query will retrieve all possible combinations of records from the
employees and departments tables.
In this example, the employees table is joined to itself to find employees and
their respective managers based on the manager_id column.
SQL Functions
In this section we have compiled SQL cheat sheet for SQL functions. It is
used for common tasks like aggregation, filtering, date/time manipulation,
and more!
This query uses the UPPER() scalar function to convert the first_name
column values to uppercase.
This query uses the CONCAT() string function to concatenate the first_name
and last_name columns into a single column called full_name.
SELECT SUBSTR(first_name, 1, 3) AS short_name FROM employees;
This query uses the SUBSTR() function to extract the first three characters of
the first_name column for each employee. The result is displayed in a new
column called short_name.
SELECT INSERT(full_name, 6, 0, 'Amazing ') AS modified_name
FROM (SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM
employees) AS employee_names;
This query first concatenates the first_name and last_name columns into a
single column called full_name. Then, it uses the INSERT() function to insert
the string ‘Amazing ‘ at the 6th position of the full_name column for each
employee. The modified names are displayed in a new column called
modified_name.
54. Date and Time Functions: Functions That Operate on Date and Time
Values
This query uses the CURRENT_DATE date function to retrieve the current
date.
Subqueries in SQL
This SQL cheat sheet explains how to nest queries for powerful data filtering
and manipulation within a single statement.
SELECT department_name
FROM departments
WHERE department_id IN (SELECT department_id FROM employees);
Views in SQL
Here in this SQL cheat sheet unveils how to create virtual tables based on
existing data for streamlined access.
Indexes in SQL
Speed up your SQL queries with our Indexes Cheat Sheet! Learn how to
create and optimize indexes to dramatically improve database performance.
62. CREATE INDEX: Create an Index on a Table
Transactions in SQL
Learn how to manage groups of database operations as a single unit for
reliable data updates.
BEGIN TRANSACTION;
COMMIT;
This statement saves all changes made during the current transaction.
ROLLBACK;
This statement undoes all changes made during the current transaction.