0% found this document useful (0 votes)
5 views6 pages

Complete SQL Cheat Sheet Tables

This document is a comprehensive SQL commands cheat sheet that categorizes commands into Data Manipulation Language (DML), Data Definition Language (DDL), Data Control Language (DCL), and other SQL functionalities. It provides syntax and examples for various commands including SELECT, INSERT, UPDATE, DELETE, JOINs, aggregate functions, string functions, date & time functions, conditional expressions, set operations, and transaction control. This resource serves as a quick reference for SQL users to efficiently execute database operations.

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)
5 views6 pages

Complete SQL Cheat Sheet Tables

This document is a comprehensive SQL commands cheat sheet that categorizes commands into Data Manipulation Language (DML), Data Definition Language (DDL), Data Control Language (DCL), and other SQL functionalities. It provides syntax and examples for various commands including SELECT, INSERT, UPDATE, DELETE, JOINs, aggregate functions, string functions, date & time functions, conditional expressions, set operations, and transaction control. This resource serves as a quick reference for SQL users to efficiently execute database operations.

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/ 6

SQL Commands Cheat Sheet

Data Manipulation Language (DML)


Command Description Syntax Example
SELECT Retrieve data SELECT col1, col2 FROM SELECT first_name, last_name
table; FROM customers;
INSERT Add new record INSERT INTO table (col1, INSERT INTO customers
col2) VALUES (val1, val2); (first_name, last_name) VALUES
('Mary', 'Doe');
UPDATE Modify record UPDATE table SET col1 = UPDATE employees SET
val1 WHERE condition; department = 'Marketing'
WHERE employee_name = 'John
Doe';
DELETE Remove record DELETE FROM table DELETE FROM employees
WHERE condition; WHERE employee_name = 'John
Doe';

Data Definition Language (DDL)


Command Description Syntax Example
CREATE Create table/object CREATE TABLE table (col1 CREATE TABLE employees
datatype, col2 datatype); (employee_id INT PRIMARY KEY,
first_name VARCHAR(50));
ALTER Modify table ALTER TABLE table ADD ALTER TABLE customers ADD
col_name datatype; email VARCHAR(100);
DROP Delete table/object DROP TABLE table; DROP TABLE customers;
TRUNCATE Delete all records TRUNCATE TABLE table; TRUNCATE TABLE customers;

Data Control Language (DCL)


Command Description Syntax Example
GRANT Give access GRANT privileges ON table GRANT SELECT, INSERT ON
TO user; employees TO 'John Doe';
REVOKE Revoke access REVOKE privileges ON REVOKE SELECT, INSERT ON
table FROM user; employees FROM 'John Doe';
Query Clauses
Command Description Syntax Example
WHERE Filter rows SELECT * FROM table SELECT * FROM customers WHERE age >
WHERE condition; 30;
ORDER BY Sort results SELECT * FROM table ORDER SELECT * FROM products ORDER BY
BY column ASC|DESC; price DESC;
GROUP BY Group results SELECT column, COUNT(*) SELECT category, COUNT(*) FROM
FROM table GROUP BY products GROUP BY category;
column;
HAVING Filter grouped SELECT column, COUNT(*) SELECT category, COUNT(*) FROM
results FROM table GROUP BY products GROUP BY category HAVING
column HAVING condition; COUNT(*) > 5;

Joins
Command Description Syntax Example
INNER JOIN Matching rows in SELECT * FROM t1 INNER SELECT * FROM employees INNER JOIN
both tables JOIN t2 ON t1.col = t2.col; departments ON
employees.department_id =
departments.id;
LEFT JOIN All from left + SELECT * FROM t1 LEFT JOIN SELECT * FROM employees LEFT JOIN
matching right t2 ON t1.col = t2.col; departments ON
employees.department_id =
departments.id;
RIGHT JOIN All from right + SELECT * FROM t1 RIGHT SELECT * FROM employees RIGHT JOIN
matching left JOIN t2 ON t1.col = t2.col; departments ON
employees.department_id =
departments.department_id;
FULL JOIN All rows from both LEFT JOIN + RIGHT JOIN with SELECT * FROM employees LEFT JOIN
tables UNION departments ON employees.id =
departments.id UNION SELECT * FROM
employees RIGHT JOIN departments ON
employees.id = departments.id;
CROSS JOIN Cartesian product SELECT * FROM t1 CROSS SELECT * FROM employees CROSS JOIN
JOIN t2; departments;
SELF JOIN Join the same SELECT * FROM t1, t1 t2 SELECT * FROM employees t1,
table WHERE t1.col = t2.col; employees t2 WHERE t1.manager_id =
t2.employee_id;
NATURAL JOIN Join by common SELECT * FROM t1 NATURAL SELECT * FROM employees NATURAL
columns JOIN t2; JOIN departments;
Subqueries
Command Description Syntax Example
IN Value in subquery result SELECT * FROM table WHERE SELECT * FROM customers WHERE
col IN (subquery); city IN (SELECT city FROM suppliers);
ANY Compare to any value SELECT * FROM table WHERE SELECT * FROM products WHERE
val < ANY (subquery); price < ANY (SELECT unit_price FROM
supplier_products);
ALL Compare to all values SELECT * FROM table WHERE SELECT * FROM orders WHERE
val > ALL (subquery); order_amount > ALL (SELECT
total_amount FROM previous_orders);

Aggregate Functions
Command Description Syntax Example
COUNT() Count rows SELECT COUNT(col) FROM SELECT COUNT(age) FROM
table; employees;
SUM() Sum values SELECT SUM(col) FROM table; SELECT SUM(revenue) FROM sales;
AVG() Average value SELECT AVG(col) FROM table; SELECT AVG(price) FROM products;
MIN() Minimum value SELECT MIN(col) FROM table; SELECT MIN(price) FROM products;
MAX() Maximum value SELECT MAX(col) FROM table; SELECT MAX(price) FROM products;
String Functions
Command Description Syntax Example
CONCAT() Combine strings SELECT CONCAT(str1, str2) SELECT CONCAT(first_name, ' ',
FROM table; last_name) FROM employees;
SUBSTRING() Extract substring SELECT SUBSTRING(str FROM SELECT
start FOR len) FROM table; SUBSTRING(product_name FROM
1 FOR 5) FROM products;
CHAR_LENGTH() String length SELECT CHAR_LENGTH(str) SELECT
FROM table; CHAR_LENGTH(product_name)
FROM products;
UPPER() To uppercase SELECT UPPER(str) FROM table; SELECT UPPER(first_name) FROM
employees;
LOWER() To lowercase SELECT LOWER(str) FROM table; SELECT LOWER(last_name) FROM
employees;
TRIM() Trim spaces SELECT TRIM(str) FROM table; SELECT TRIM(full_name) FROM
customers;
LEFT() Left part of string SELECT LEFT(str, n) FROM table; SELECT LEFT(product_name, 5)
FROM products;
RIGHT() Right part of string SELECT RIGHT(str, n) FROM SELECT RIGHT(order_number, 4)
table; FROM orders;
REPLACE() Replace substring SELECT REPLACE(str, old, new) SELECT REPLACE(description,
FROM table; 'old', 'new') FROM
product_descriptions;

Date & Time Functions


Command Description Syntax Example
CURRENT_DATE() Today's date SELECT CURRENT_DATE(); SELECT CURRENT_DATE();
CURRENT_TIME() Current time SELECT CURRENT_TIME(); SELECT CURRENT_TIME();
CURRENT_TIMESTAMP() Current date and SELECT CURRENT_TIMESTAMP(); SELECT
time CURRENT_TIMESTAMP();
DATE_PART() Extract part of date SELECT DATE_PART('part', date); SELECT DATE_PART('year',
'2024-04-11');
DATE_ADD() Add to date SELECT DATE_ADD(date, SELECT DATE_ADD('2024-04-11',
INTERVAL n unit); INTERVAL 1 DAY);
DATE_SUB() Subtract from date SELECT DATE_SUB(date, SELECT DATE_SUB('2024-04-11',
INTERVAL n unit); INTERVAL 1 DAY);
EXTRACT() Extract date part SELECT EXTRACT(PART FROM SELECT EXTRACT(YEAR FROM
date); '2024-04-11');
TO_CHAR() Format date SELECT TO_CHAR(date, 'format'); SELECT TO_CHAR('2024-04-11',
'YYYY-MM-DD');
TIMESTAMPDIFF() Time difference SELECT TIMESTAMPDIFF(unit, SELECT TIMESTAMPDIFF(DAY,
date1, date2); '2024-04-10', '2024-04-11');
DATEDIFF() Day difference SELECT DATEDIFF(date1, date2); SELECT DATEDIFF('2024-04-11',
'2024-04-10');
Conditional Expressions
Command Description Syntax Example
CASE Conditional logic SELECT col, CASE WHEN SELECT order_id, CASE WHEN
cond THEN val ELSE val total_amount > 1000 THEN 'High'
END FROM table; ELSE 'Low' END AS status FROM
orders;
IF() Inline condition SELECT IF(cond, true_val, SELECT name, IF(age > 50, 'Senior',
false_val) FROM table; 'Junior') FROM employees;
COALESCE() First non-null SELECT COALESCE(val1, SELECT COALESCE(first_name,
val2) FROM table; middle_name) FROM employees;
NULLIF() Return null if equal SELECT NULLIF(val1, val2) SELECT NULLIF(total_amount,
FROM table; discounted_amount) FROM orders;

Set Operations
Command Description Syntax Example
UNION Combine unique rows SELECT col FROM table1 SELECT first_name FROM
UNION SELECT col FROM customers UNION SELECT
table2; first_name FROM employees;
INTERSECT Common rows SELECT col FROM table1 SELECT first_name FROM
INTERSECT SELECT col customers INTERSECT SELECT
FROM table2; first_name FROM employees;
EXCEPT Rows in first not second SELECT col FROM table1 SELECT first_name FROM
EXCEPT SELECT col FROM customers EXCEPT SELECT
table2; first_name FROM employees;

Transaction Control
Command Description Syntax Example
BEGIN Start transaction BEGIN; BEGIN;
COMMIT Save changes COMMIT; COMMIT;
ROLLBACK Undo changes ROLLBACK; ROLLBACK;
SAVEPOINT Set rollback point SAVEPOINT sp_name; SAVEPOINT before_update;
ROLLBACK TO Rollback to point ROLLBACK TO sp_name; ROLLBACK TO before_update;
SET TRANSACTION Set transaction mode SET TRANSACTION SET TRANSACTION ISOLATION
ISOLATION LEVEL LEVEL; LEVEL READ COMMITTED;

You might also like