![]() |
VOOZH | about |
This SQL Cheat Sheet is your quick-reference guide for working with relational databases. It helps you recall key commands, logic, and query patterns to manage, manipulate, and control data efficiently.
Explore this section to get hands on all the cheat sheet that help you in order to create a database in SQL.
CREATE DATABASE company;This command creates a new database named "company."
USE company;This command selects the database named "company" for further operations.
ALTER DATABASE company;This command Modify database attributes named "company".
DROP DATABASE company;This command Deletes the database named "company".
Here in this SQL cheat sheet we have listed down all the cheat sheet that help to create, insert, alter data in table.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50),
salary DECIMAL(10, 2)
);
This command creates a table named "employees" with columns for employee ID, first name, last name, department, and salary. The employee_id column is set as the primary key.
INSERT INTO employees (employee_id, first_name, last_name, department, salary)
VALUES
(1, 'John', 'Doe', 'HR', 50000.00),
(2, 'Jane', 'Smith', 'IT', 60000.00),
(3, 'Alice', 'Johnson', 'Finance', 55000.00),
(4, 'Bob', 'Williams', 'IT', 62000.00),
(5, 'Emily', 'Brown', 'HR', 48000.00);
This command inserts sample data into the "employees" table with values for employee ID, first name, last name, department, and salary.
ALTER TABLE employees
ADD COLUMN new_column INT;
This command adds a new column named "new_column" of integer type to the existing "employees" table.
DROP TABLE employees;This command deletes the entire "employees" table along with all its data.
Explore this section to get the cheat sheet on how to use select, distinct and other querying data in SQL.
SELECT * FROM employees;This query will retrieve all columns from the employees table.
SELECT DISTINCT department FROM employees;This query will return unique department names from the employees table.
SELECT * FROM employees WHERE salary > 55000.00;This query will return employees whose salary is greater than 55000.00.
SELECT * FROM employees LIMIT 3;This query will limit the result set to the first 3 rows.
SELECT * FROM employees LIMIT 10000 OFFSET 2;This query retrieves all rows from the "employees" table, skipping the first 2 rows and limiting the result to 10,000 rows.
SELECT * FROM employees FETCH FIRST 3 ROWS ONLY;
This query will fetch the first 3 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'.
Get a cheat sheet on how to update or manipulate data in SQL by exploring this section.
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.
DELETE FROM employees
WHERE employee_id = 5;
This query will delete the record of the employee with employee_id 5 from the employees table.
SELECT * FROM employees
WHERE department = 'IT';
This query will retrieve all employees who work in the IT department.
SELECT * FROM employees
WHERE first_name LIKE 'J%';
This query will retrieve all employees whose first name starts with 'J'.
SELECT * FROM employees
WHERE department IN ('HR', 'Finance');
This query will retrieve all employees who work in the HR or Finance departments.
SELECT * FROM employees
WHERE salary BETWEEN 50000 AND 60000;
This query will retrieve all employees whose salary is between 50000 and 60000.
SELECT * FROM employees
WHERE department IS NULL;
This query will retrieve all employees where the department is not assigned (NULL).
SELECT * FROM employees
ORDER BY salary DESC;
This query will retrieve all employees sorted by salary in descending order.
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.
SELECT * FROM employees
WHERE department = 'IT' AND salary > 60000;
This query will retrieve employees who work in the IT department and have a salary greater than 60000.
SELECT * FROM employees
WHERE department = 'HR' OR department = 'Finance';
This query will retrieve employees who work in either the HR or Finance department.
SELECT * FROM employees
WHERE NOT department = 'IT';
This query will retrieve employees who do not work in the IT department.
SELECT * FROM employees
WHERE first_name LIKE 'J%';
This query will retrieve employees whose first name starts with 'J'.
SELECT * FROM employees
WHERE department IN ('HR', 'Finance');
This query will retrieve employees who work in the HR or Finance departments.
SELECT * FROM employees
WHERE salary BETWEEN 50000 AND 60000;
This query will retrieve employees whose salary is between 50000 and 60000.
SELECT * FROM employees
WHERE department IS NULL;
This query will retrieve employees where the department is not assigned (NULL).
SELECT * FROM employees
ORDER BY salary DESC;
This query will retrieve all employees sorted by salary in descending order.
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query will group employees by department and count the number of employees in each department.
Get an hands in aggregation data in SQL. Here you will find cheat sheet for how to count numbers, sum of numbers and more.
SELECT COUNT(*) FROM employees;This query will count the total number of employees.
SELECT SUM(salary) FROM employees;This query will calculate the total salary of all employees.
SELECT AVG(salary) FROM employees;This query will calculate the average salary of all employees.
SELECT MIN(salary) FROM employees;This query will find the minimum salary among all employees.
SELECT MAX(salary) FROM employees;This query will find the maximum salary among all employees.
SELECT department, COUNT(*) AS employee_count
FROM employees
GROUP BY department;
This query will group employees by department and count the number of employees in each department.
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 55000;
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 act as data quality guardrails, enforcing rules to ensure accuracy, consistency, and integrity within your database tables.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
employee_id is designated as the primary key, ensuring that each employee record has a unique identifier.
CREATE TABLE departments (
department_id INT PRIMARY KEY,
department_name VARCHAR(50)
);
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
department_id INT,
FOREIGN KEY (department_id) REFERENCES departments(department_id)
);
department_id column in the employees table is a foreign key that references the department_id column in the departments table, establishing a relationship between the two tables.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE
);
email column must contain unique values for each employee.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
first_name VARCHAR(50) NOT NULL,
last_name VARCHAR(50) NOT NULL
);
first_name and last_name columns must have values and cannot be NULL.
CREATE TABLE employees (
employee_id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
age column must have a value of 18 or greater due to the CHECK constraint.
Explore different join types to seamlessly merge data from multiple tables in your SQL queries.
SELECT * FROM employees
INNER JOIN departments ON employees.department_id = departments.department_id;
This query will retrieve records from both the employees and departments tables where there is a match on the department_id column.
SELECT * FROM employees
LEFT JOIN departments ON employees.department_id = departments.department_id;
This query will retrieve all records from the employees table and only the matching records from the departments table.
SELECT * FROM employees
RIGHT JOIN departments ON employees.department_id = departments.department_id;
This query will retrieve all records from the departments table and only the matching records from the employees table.
SELECT * FROM employees
FULL OUTER JOIN departments ON employees.department_id = departments.department_id;
This query will retrieve all records from both the employees and departments tables, including unmatched records.
SELECT * FROM employees
CROSS JOIN departments;
This query will retrieve all possible combinations of records from the employees and departments tables.
SELECT e1.first_name, e2.first_name
FROM employees e1, employees e2
WHERE e1.employee_id = e2.manager_id;
In this example, the employees table is joined to itself to find employees and their respective managers based on the manager_id column.
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!
SELECT UPPER(first_name) AS upper_case_name FROM employees;This query uses the UPPER() scalar function to convert the first_name column values to uppercase.
SELECT AVG(salary) AS average_salary FROM employees;This query uses the AVG() aggregate function to calculate the average salary of all employees.
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM employees;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.
SELECT CURRENT_DATE AS current_date FROM dual;This query uses the CURRENT_DATE date function to retrieve the current date.
SELECT SQRT(25) AS square_root FROM dual;This query uses the SQRT() mathematical function to calculate the square root of 25.
This SQL cheat sheet explains how to nest queries for powerful data filtering and manipulation within a single statement.
SELECT first_name, last_name
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
In this example, the subquery (SELECT MAX(salary) FROM employees) returns a single row containing the maximum salary, and it's used to filter employees who have the maximum salary.
SELECT department_name
FROM departments
WHERE department_id IN (SELECT department_id FROM employees);
In this example, the subquery (SELECT department_id FROM employees) returns multiple rows containing department IDs, and it's used to filter department names based on those IDs.
SELECT first_name, last_name
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees WHERE department = e.department);
In this example, the subquery (SELECT AVG(salary) FROM employees WHERE department = e.department) is correlated with the outer query by referencing the department column from the outer query. It calculates the average salary for each department and is used to filter employees whose salary is greater than the average salary of their respective department.
SELECT first_name, last_name
FROM employees
WHERE department_id IN (
SELECT department_id
FROM departments
WHERE department_name = 'IT'
);
In this example, the subquery (SELECT department_id FROM departments WHERE department_name = 'IT') is nested within the outer query. It retrieves the department ID for the IT department, which is then used in the outer query to filter employees belonging to the IT department.
Here in this SQL cheat sheet unveils how to create virtual tables based on existing data for streamlined access.
CREATE VIEW high_paid_employees AS
SELECT *
FROM employees
WHERE salary > 60000;
This query creates a views named high_paid_employees that contains all employees with a salary greater than 60000.
DROP VIEW IF EXISTS high_paid_employees;This query drops the high_paid_employees view if it exists.
Speed up your SQL queries with our Indexes Cheat Sheet! Learn how to create and optimize indexes to dramatically improve database performance.
CREATE INDEX idx_department ON employees (department);This query creates an index named idx_department on the department column of the employees table.
DROP INDEX IF EXISTS idx_department;This query drops the idx_department index if it exists.
Learn how to manage groups of database operations as a single unit for reliable data updates.
BEGIN TRANSACTION;This statement starts a new transaction.
COMMIT;This statement saves all changes made during the current transaction.
ROLLBACK;This statement undoes all changes made during the current transaction.
In the last we have complied all the imprtant queries under the one advanced SQL cheat sheet.
CREATE PROCEDURE get_employee_count()
BEGIN
SELECT COUNT(*) FROM employees;
END;
This query creates a stored procedure named get_employee_count that returns the count of employees.
CREATE TRIGGER before_employee_insert
BEFORE INSERT ON employees
FOR EACH ROW
BEGIN
SET NEW.creation_date = NOW();
END;
This query creates a trigger named before_employee_insert that sets the creation_date column to the current date and time before inserting a new employee record.
CREATE FUNCTION calculate_bonus(salary DECIMAL) RETURNS DECIMAL
BEGIN
RETURN salary * 0.1; -- 10% bonus
END;
This query creates a user-defined function named calculate_bonus that calculates the bonus based on the salary.
WITH high_paid_employees AS (
SELECT * FROM employees WHERE salary > 60000
)
SELECT * FROM high_paid_employees;
This query uses a common table expression named high_paid_employees to retrieve all employees with a salary greater than 60000.