SQL Cheat-Sheet Entri Elevate PDF
SQL Cheat-Sheet Entri Elevate PDF
CHEAT
SHEET
COMPREHENSIVE
Data Manipulation Language DML Commands
INSERT The INSERT command adds new INSERT INTO table_name INSERT INTO customers
records to a table. (column1, column2) VALUES (first_name, last_name)
(value1, value2); VALUES ('Mary', 'Doe');
UPDATE The UPDATE command is used UPDATE table_name SET column1 UPDATE employees SET
to modify existing records in a = value1, column2 = value2 employee_name = ‘John Doe’,
table. WHERE condition; department = ‘Marketing’;
DELETE The DELETE command removes DELETE FROM table_name WHERE DELETE FROM employees WHERE
records from a table. condition; employee_name = ‘John Doe’;
ALTER The ALTER command adds, ALTER TABLE table_name ALTER TABLE customers ADD
deletes, or modifies columns in ADD column_name datatype; email VARCHAR(100);
an existing table.
DROP The DROP command is used to DROP TABLE table_name; DROP TABLE customers;
drop an existing table in a
database.
GRANT The GRANT command is used to GRANT SELECT, INSERT ON GRANT SELECT, INSERT ON
give specific privileges to users table_name TO user_name; employees TO ‘John Doe’;
or roles.
REVOKE The REVOKE command is used REVOKE SELECT, INSERT ON REVOKE SELECT, INSERT ON
to take away privileges table_name FROM employees FROM ‘John
previously granted to users or user_name; Doe’;
roles.
SELECT Statement The SELECT statement is the SELECT column1, column2 SELECT first_name,
primary command used to FROM table_name; last_name FROM customers;
retrieve data from a database
WHERE Clause The WHERE clause is used to SELECT * FROM table_name SELECT * FROM customers
filter rows based on a specified WHERE condition; WHERE age > 30;
condition.
ORDER BY Clause The ORDER BY clause is used to SELECT * FROM table_name SELECT * FROM products
sort the result set in ascending ORDER BY column_name ORDER BY price DESC;
or descending order based on a ASC|DESC;
specified column.
GROUP BY Clause The GROUP BY clause groups SELECT column_name, SELECT category, COUNT(*)
rows based on the values in a COUNT(*) FROM table_name FROM products GROUP BY
specified column. It is often GROUP BY column_name; category;
used with aggregate functions
like COUNT, SUM, AVG, etc.
HAVING Clause The HAVING clause filters SELECT column_name, SELECT category, COUNT(*)
grouped results based on a COUNT(*) FROM table_name FROM products GROUP BY
specified condition. GROUP BY column_name category HAVING COUNT(*)
HAVING condition; >5;
Joining Commands
INNER JOIN The INNER JOIN command SELECT * FROM table1 SELECT * FROM employees
returns rows with matching INNER JOIN table2 ON INNER JOIN departments ON
values in both tables. table1.column = employees.department_id =
table2.column; departments.id;
LEFT JOIN/LEFT OUTER JOIN The LEFT JOIN command SELECT * FROM table1 LEFT SELECT * FROM employees LEFT
returns all rows from the left JOIN table2 ON JOIN departments ON
table (first table) and the table1.column = employees.department_id =
matching rows from the right
table2.column; departments.id;
table (second table).
RIGHT JOIN/RIGHT OUTER The RIGHT JOIN command SELECT * FROM table1 SELECT *
JOIN returns all rows from the right RIGHT JOIN table2 ON FROM employees
table (second table) and the table1.column = RIGHT JOIN departments
matching rows from the left
table2.column; ON employees.department_id =
table (first table).
departments.department_id;
FULL JOIN/FULL OUTER JOIN The FULL JOIN command SELECT * FROM table1 FULL SELECT * FROM employees
returns all rows when there is a JOIN table2 ON LEFT JOIN departments ON
match in either the left table or table1.column = employees.employee_id =
the right table.
table2.column; departments.employee_id
UNION SELECT * FROM
employees RIGHT JOIN
departments ON
employees.employee_id =
departments.employee_id;
CROSS JOIN The CROSS JOIN command SELECT * FROM table1 SELECT * FROM employees
combines every row from the CROSS JOIN table2; CROSS JOIN departments;
first table with every row from
the second table, creating a
Cartesian product.
SELF JOIN The SELF JOIN command joins SELECT * FROM table1 t1, SELECT * FROM employees t1,
a table with itself. table1 t2 WHERE t1.column employees t2
= t2.column; WHERE t1.employee_id =
t2.employee_id;
NATURAL JOIN The NATURAL JOIN SELECT * FROM table1 SELECT * FROM employees
command matches columns NATURAL JOIN table2; NATURAL JOIN departments;
with the same name in both
tables.
Subqueries in SQL
ANY The ANY command is used to SELECT column(s) FROM SELECT * FROM products
compare a value to any value table WHERE value < ANY WHERE price < ANY (SELECT
returned by a subquery. It can (subquery); unit_price FROM
be used with comparison supplier_products);
operators like =, >, <, etc.
ALL The ALL command is used to SELECT column(s) FROM SELECT * FROM orders
compare a value to all values table WHERE value > ALL WHERE order_amount > ALL
returned by a subquery. It (subquery); (SELECT total_amount FROM
can be used with comparison
previous_orders);
operators like =, >, <, etc.
COUNT() The COUNT command counts SELECT COUNT(column_name) SELECT COUNT(age) FROM
the number of rows or non-null FROM table_name; employees;
values in a specified column.
SUM() The SUM command is used to SELECT SUM(column_name) SELECT SUM(revenue) FROM
calculate the sum of all values in FROM table_name; sales;
a specified column.
AVG() The AVG command is used to SELECT AVG(column_name) SELECT AVG(price) FROM
calculate the average (mean) of FROM table_name; products;
all values in a specified column.
MIN() The MIN command returns the SELECT MIN(column_name) SELECT MIN(price) FROM
minimum (lowest) value in a FROM table_name; products;
specified column.
MAX() The MAX command returns the SELECT MAX(column_name) SELECT MAX(price) FROM
maximum (highest) value in a FROM table_name; products;
specified column.
String Functions in SQL
TRIM() The TRIM command removes SELECT TRIM([LEADING | SELECT TRIM(TRAILING ' '
specified prefixes or suffixes (or TRAILING | BOTH] FROM full_name) AS
whitespace by default) from a characters FROM string) trimmed_full_name FROM
string. AS trimmed_string FROM customers;
table_name;
DATE_SUB Example
SELECT
DATE_SUB('2024-04-11',
INTERVAL 1 DAY) AS
new_date;
EXTRACT() The EXTRACT command SELECT EXTRACT(part FROM SELECT EXTRACT(YEAR FROM
extracts a specific part (e.g., date_expression) AS '2024-04-11') AS
year, month, day) from a date or extracted_part; extracted_part;
time.
UNION The UNION operator SELECT column1, column2 FROM SELECT first_name, last_name
combines the result sets of table1 FROM customers
two or more SELECT UNION UNION
statements into a single
SELECT column1, column2 FROM SELECT first_name, last_name
result set.
table2; FROM employees;
INTERSECT The INTERSECT operator SELECT column1, column2 FROM SELECT first_name, last_name
returns the common rows table1 FROM customers
that appear in both result INTERSECT INTERSECT
sets.
SELECT column1, column2 FROM SELECT first_name, last_name
table2; FROM employees;
EXCEPT The EXCEPT operator SELECT column1, column2 FROM SELECT first_name, last_name
returns the distinct rows table1 EXCEPT SELECT FROM customers
from the left result set that column1, column2 FROM EXCEPT
are not present in the right
table2; SELECT first_name, last_name
result set.
FROM employees;
ROLLBACK;
SAVEPOINT The SAVEPOINT command SAVEPOINT BEGIN TRANSACTION;
is used to set a point within savepoint_n
a transaction to which you ame; INSERT INTO employees (name, age) VALUES ('Carol',
can later roll back.
28);
SAVEPOINT before_update;
ROLLBACK TO before_update;