Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views8 pages

SQL Cheat Sheet Grid

This document is a comprehensive SQL cheat sheet that outlines various SQL commands categorized into Data Manipulation Language (DML), Data Definition Language (DDL), Data Control Language (DCL), and more. It includes syntax and examples for commands such as SELECT, INSERT, UPDATE, DELETE, and various functions for querying and manipulating data. Additionally, it covers joins, subqueries, aggregate functions, string functions, date and time functions, conditional expressions, set operations, and transaction control commands.

Uploaded by

singashu1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

SQL Cheat Sheet Grid

This document is a comprehensive SQL cheat sheet that outlines various SQL commands categorized into Data Manipulation Language (DML), Data Definition Language (DDL), Data Control Language (DCL), and more. It includes syntax and examples for commands such as SELECT, INSERT, UPDATE, DELETE, and various functions for querying and manipulating data. Additionally, it covers joins, subqueries, aggregate functions, string functions, date and time functions, conditional expressions, set operations, and transaction control commands.

Uploaded by

singashu1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL Cheat Sheet: Extracted Tables

1. Data Manipulation Language (DML) Commands


Comman
Description Syntax Example
d

Retrieves data SELECT column1, SELECT first_name,


SELECT from a column2 FROM last_name FROM
database table_name; customers;

INSERT INTO
Adds new INSERT INTO customers
table_name (column1,
INSERT records to a (first_name, last_name)
column2) VALUES
table VALUES ('Mary', 'Doe');
(value1, value2);

Modifies UPDATE table_name UPDATE employees SET


existing SET column1 = value1, employee_name = ‘John
UPDATE
records in a column2 = value2 Doe’, department =
table WHERE condition; ‘Marketing’;

Removes DELETE FROM DELETE FROM employees


DELETE records from a table_name WHERE WHERE employee_name
table condition; = ‘John Doe’;

2. Data Definition Language (DDL) Commands


Comman
Description Syntax Example
d

CREATE TABLE
employees
CREATE TABLE
(employee_id INT
table_name
PRIMARY KEY,
Creates new database (column1
CREATE first_name
objects datatype1,
VARCHAR(50),
column2
last_name
datatype2, ...);
VARCHAR(50), age
INT);

ALTER TABLE
ALTER TABLE
Adds/modifies/deletes table_name ADD
ALTER customers ADD email
table columns column_name
VARCHAR(100);
datatype;
Comman
Description Syntax Example
d

Deletes an existing DROP TABLE DROP TABLE


DROP
table table_name; customers;

Deletes all data from a


TRUNCAT TRUNCATE TABLE TRUNCATE TABLE
table without deleting
E table_name; customers;
the table itself

3. Data Control Language (DCL) Commands


Comman
Description Syntax Example
d

GRANT SELECT, INSERT GRANT SELECT,


Gives privileges
GRANT ON table_name TO INSERT ON employees
to users/roles
user_name; TO ‘John Doe’;

Removes REVOKE SELECT, REVOKE SELECT,


REVOKE privileges from INSERT ON table_name INSERT ON employees
users/roles FROM user_name; FROM ‘John Doe’;

4. Querying Data
Descriptio
Clause Syntax Example
n

SELECT first_name,
Retrieves SELECT column1, column2
SELECT last_name FROM
data FROM table_name;
customers;

SELECT * FROM
SELECT * FROM table_name
WHERE Filters rows customers WHERE age
WHERE condition;
> 30;

SELECT * FROM table_name


ORDER Sorts result
ORDER BY column_name DESC;
BY set
ASC

SELECT category,
SELECT column_name,
GROUP COUNT(*) FROM
Groups rows COUNT(*) FROM table_name
BY products GROUP BY
GROUP BY column_name;
category;

HAVING Filters SELECT column_name, SELECT category,


grouped COUNT(*) FROM table_name COUNT() FROM
Descriptio
Clause Syntax Example
n

products GROUP BY
GROUP BY column_name
results category HAVING
HAVING condition;
COUNT() > 5;

5. Join Commands
Join
Description Syntax Example
Type

SELECT * FROM
SELECT * FROM employees
Returns table1 INNER JOIN
INNER INNER JOIN departments ON
matching table2 ON
JOIN employees.department_id =
rows table1.column =
departments.id;
table2.column;

SELECT * FROM
All rows SELECT * FROM employees LEFT
table1 LEFT JOIN
LEFT from left + JOIN departments ON
table2 ON
JOIN matching employees.department_id =
table1.column =
right departments.id;
table2.column;

SELECT * FROM
All rows SELECT * FROM employees
table1 RIGHT JOIN
RIGHT from right + RIGHT JOIN departments ON
table2 ON
JOIN matching employees.department_id =
table1.column =
left departments.department_id;
table2.column;

SELECT * FROM
All rows with table1 FULL JOIN
FULL (LEFT JOIN + RIGHT JOIN with
matches table2 ON
JOIN UNION)
from both table1.column =
table2.column;

SELECT * FROM
CROSS Cartesian SELECT * FROM employees
table1 CROSS JOIN
JOIN product CROSS JOIN departments;
table2;

SELECT * FROM
SELECT * FROM employees t1,
table1 t1, table1
SELF Joins a table employees t2 WHERE
t2 WHERE
JOIN to itself t1.employee_id =
t1.column =
t2.employee_id;
t2.column;
Join
Description Syntax Example
Type

Matches
SELECT * FROM
NATURA columns SELECT * FROM employees
table1 NATURAL
L JOIN with same NATURAL JOIN departments;
JOIN table2;
names

6. Subqueries
Comman
Description Syntax Example
d

SELECT column(s)
Matches any SELECT * FROM customers
FROM table
IN value in WHERE city IN (SELECT city
WHERE value IN
subquery FROM suppliers);
(subquery);

SELECT column(s) SELECT * FROM products


Compare to
FROM table WHERE price < ANY (SELECT
ANY any in
WHERE value < unit_price FROM
subquery
ANY (subquery); supplier_products);

SELECT column(s) SELECT * FROM orders WHERE


Compare to
FROM table order_amount > ALL (SELECT
ALL all in
WHERE value > total_amount FROM
subquery
ALL (subquery); previous_orders);

7. Aggregate Functions
Functio
Description Syntax Example
n

SELECT
COUNT( Counts non- SELECT COUNT(age)
COUNT(column_name) FROM
) null rows FROM employees;
table_name;

SELECT
SELECT SUM(column_name)
SUM() Total sum SUM(revenue) FROM
FROM table_name;
sales;

Average SELECT AVG(column_name) SELECT AVG(price)


AVG()
value FROM table_name; FROM products;

Minimum SELECT MIN(column_name) SELECT MIN(price)


MIN()
value FROM table_name; FROM products;
Functio
Description Syntax Example
n

Maximum SELECT MAX(column_name) SELECT MAX(price)


MAX()
value FROM table_name; FROM products;

8. String Functions
Descript
Function Syntax Example
ion

SELECT
SELECT CONCAT(string1,
Combines CONCAT(first_name, ' ',
CONCAT() string2, ...) AS result
strings last_name) FROM
FROM table_name;
employees;

SELECT SELECT
Extracts SUBSTRING(string FROM SUBSTRING(product_na
SUBSTRING()
substring start FOR length) AS me FROM 1 FOR 5)
result FROM table_name; FROM products;

SELECT
SELECT
CHAR_LENGT String CHAR_LENGTH(string)
CHAR_LENGTH(product_
H() length AS length FROM
name) FROM products;
table_name;

Converts
SELECT
to SELECT UPPER(string)
UPPER() UPPER(first_name) FROM
uppercas FROM table_name;
employees;
e

Converts
SELECT
to SELECT LOWER(string)
LOWER() LOWER(last_name)
lowercas FROM table_name;
FROM employees;
e

SELECT
SELECT TRIM(TRAILING '
Removes TRIM(LEADING/TRAILING/
TRIM() ' FROM full_name) FROM
spaces BOTH 'x' FROM string)
customers;
FROM table_name;

SELECT
Left SELECT LEFT(string, n)
LEFT() LEFT(product_name, 5)
substring FROM table_name;
FROM products;

RIGHT() Right SELECT RIGHT(string, n) SELECT


substring FROM table_name; RIGHT(order_number, 4)
Descript
Function Syntax Example
ion

FROM orders;

SELECT
SELECT REPLACE(string, REPLACE(description,
Replaces
REPLACE() 'old', 'new') FROM 'old_string', 'new_string')
substring
table_name; FROM
product_descriptions;

9. Date and Time Functions


Function Description Syntax Example

Returns SELECT
CURRENT_DATE() —
current date CURRENT_DATE();

Returns SELECT
CURRENT_TIME() —
current time CURRENT_TIME();

Returns SELECT
CURRENT_TIMESTA
current CURRENT_TIMESTAM —
MP()
timestamp P();

SELECT SELECT
Extracts part
DATE_PART() DATE_PART('part', DATE_PART('year',
of date
date) FROM table; '2024-04-11');

SELECT
Adds/ SELECT
DATE_ADD() / DATE_ADD('2024-
subtracts DATE_ADD(date,
DATE_SUB() 04-11', INTERVAL
date INTERVAL n unit);
1 DAY);

SELECT
Extracts SELECT
EXTRACT(YEAR
EXTRACT() year/month/d EXTRACT(part FROM
FROM '2024-04-
ay date);
11');

SELECT
SELECT
Formats TO_CHAR('2024-
TO_CHAR() TO_CHAR(date,
date/time 04-11', 'YYYY-MM-
'format');
DD');

TIMESTAMPDIFF() Difference in SELECT SELECT


timestamps TIMESTAMPDIFF(unit, TIMESTAMPDIFF(D
t1, t2); AY, '2024-04-10',
Function Description Syntax Example

'2024-04-11');

SELECT
SELECT
Difference in DATEDIFF('2024-
DATEDIFF() DATEDIFF(date1,
dates 04-11', '2024-04-
date2);
10');

10. Conditional Expressions


Expressio Descriptio
Syntax Example
n n

CASE WHEN total_amount >


CASE WHEN cond
Conditional 1000 THEN 'High' ELSE 'Low'
CASE THEN result ELSE
logic END AS order_status FROM
default END
orders;

SELECT name, IF(age > 50,


If condition IF(condition, true,
IF() 'Senior', 'Junior') AS category
is true false)
FROM employees;

SELECT COALESCE(first_name,
COALESCE First non- COALESCE(val1,
middle_name) FROM
() null value val2, ...)
employees;

Null if SELECT NULLIF(total_amount,


NULLIF(expr1,
NULLIF() values are discounted_amount) AS diff
expr2)
equal FROM orders;

11. Set Operations


Operatio
Description Syntax Example
n

SELECT col FROM t1 SELECT first_name FROM


Combines
UNION UNION SELECT col customers UNION SELECT
result sets
FROM t2; first_name FROM employees;

SELECT first_name FROM


Returns SELECT col FROM t1
INTERSEC customers INTERSECT
common INTERSECT SELECT
T SELECT first_name FROM
rows col FROM t2;
employees;

EXCEPT Left result SELECT col FROM t1 SELECT first_name FROM


not in right EXCEPT SELECT col customers EXCEPT SELECT
Operatio
Description Syntax Example
n

FROM t2; first_name FROM employees;

12. Transaction Control


Command Description Syntax Example

Saves all After INSERT and


COMMIT COMMIT;
changes UPDATE operations

Undoes After unwanted


ROLLBACK ROLLBACK;
changes changes

Creates SAVEPOINT
SAVEPOINT SAVEPOINT name;
rollback point before_update;

ROLLBACK TO
ROLLBACK TO Rolls back to ROLLBACK TO
SAVEPOINT
SAVEPOINT point SAVEPOINT name;
before_update;

Sets SET TRANSACTION SET TRANSACTION


SET
transaction ISOLATION LEVEL ISOLATION LEVEL READ
TRANSACTION
properties LEVEL; COMMITTED;

This document is based on the SQL Cheat Sheet provided by DBVIS.COM

You might also like