0% found this document useful (0 votes)
8 views3 pages

Ultimate SQL Cheat Sheet

The document is a comprehensive SQL cheat sheet covering various commands and concepts including database and table commands, data types, DML, DDL, DCL commands, joins, aggregate functions, string and date functions, conditional expressions, subqueries, and transactions. It provides syntax examples for each command type and function. This resource serves as a quick reference for SQL users to perform database operations efficiently.

Uploaded by

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

Ultimate SQL Cheat Sheet

The document is a comprehensive SQL cheat sheet covering various commands and concepts including database and table commands, data types, DML, DDL, DCL commands, joins, aggregate functions, string and date functions, conditional expressions, subqueries, and transactions. It provides syntax examples for each command type and function. This resource serves as a quick reference for SQL users to perform database operations efficiently.

Uploaded by

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

Ultimate SQL Cheat Sheet (Merged)

DATABASE COMMANDS

CREATE DATABASE IF NOT EXISTS db_name;


USE db_name;
SHOW DATABASES;
DROP DATABASE IF EXISTS db_name;

TABLE COMMANDS

CREATE TABLE IF NOT EXISTS table_name (


column1 datatype constraints,
column2 datatype constraints
);

SHOW TABLES;
DESCRIBE table_name;
RENAME TABLE old_name TO new_name;
DROP TABLE IF EXISTS table_name;

DATA TYPES

INT, VARCHAR(n), TEXT, DATE, FLOAT, DOUBLE, BOOLEAN, AUTO_INCREMENT, PRIMARY KEY, NOT
NULL, DEFAULT, UNIQUE

DML COMMANDS

-- SELECT
SELECT column1, column2 FROM table_name WHERE condition ORDER BY column_name ASC|DESC;

-- INSERT
INSERT INTO table_name (col1, col2) VALUES ('val1', 'val2');

-- UPDATE
UPDATE table_name SET col1 = 'val' WHERE condition;

-- DELETE
DELETE FROM table_name WHERE condition;

DDL COMMANDS

CREATE TABLE, ALTER TABLE (ADD, DROP, MODIFY), DROP TABLE, TRUNCATE TABLE

DCL COMMANDS
Ultimate SQL Cheat Sheet (Merged)

GRANT SELECT, INSERT ON table TO user;


REVOKE SELECT, INSERT ON table FROM user;

JOINS

-- INNER JOIN
SELECT * FROM A INNER JOIN B ON A.id = B.a_id;

-- LEFT JOIN
SELECT * FROM A LEFT JOIN B ON A.id = B.a_id;

-- RIGHT JOIN
SELECT * FROM A RIGHT JOIN B ON A.id = B.a_id;

-- FULL JOIN (via UNION)


SELECT * FROM A LEFT JOIN B ON A.id = B.a_id
UNION
SELECT * FROM A RIGHT JOIN B ON A.id = B.a_id;

AGGREGATE FUNCTIONS

COUNT(), SUM(), AVG(), MIN(), MAX()

-- Example:
SELECT COUNT(*) FROM students;
SELECT AVG(age) FROM students;

GROUP BY / HAVING

SELECT column, COUNT(*) FROM table GROUP BY column;


SELECT column, COUNT(*) FROM table GROUP BY column HAVING COUNT(*) > 1;

STRING FUNCTIONS

CONCAT(), SUBSTRING(), LENGTH(), UPPER(), LOWER(), TRIM(), LEFT(), RIGHT(), REPLACE()

-- Example:
SELECT CONCAT(first_name, ' ', last_name) FROM employees;

DATE FUNCTIONS

CURRENT_DATE(), CURRENT_TIME(), CURRENT_TIMESTAMP(), DATE_ADD(), DATE_SUB(), DATEDIFF()

-- Example:
SELECT DATEDIFF('2024-05-01', '2024-04-25') AS diff;
Ultimate SQL Cheat Sheet (Merged)

CONDITIONAL EXPRESSIONS

-- CASE
SELECT name, age,
CASE
WHEN age >= 18 THEN 'Adult'
ELSE 'Minor'
END AS status FROM students;

-- IF
SELECT name, IF(age > 50, 'Senior', 'Junior') FROM employees;

SUBQUERIES & SET OPS

-- IN, ANY, ALL


SELECT * FROM table WHERE column IN (SELECT col FROM other_table);

-- UNION, INTERSECT, EXCEPT


SELECT col FROM A UNION SELECT col FROM B;

TRANSACTIONS

BEGIN;
-- SQL operations
COMMIT;
ROLLBACK;
SAVEPOINT sp1;
ROLLBACK TO sp1;

You might also like