Arunav Goswami
Data Science Consultant at almaBetter
Access a comprehensive MySQL cheat sheet covering essential commands, functions, joins, aggregation and security tips, ideal for interviews and quick reference
MySQL is a powerful, open-source relational database management system widely used in various applications, from web development to data analytics. Whether you're preparing for an interview, writing SQL queries, or defending against SQL injections, having a MySQL cheatsheet can be invaluable. This guide covers essential MySQL commands, functions, and best practices to streamline your workflow and improve your database management skills.
A MySQL cheat sheet is a handy reference during interviews that provides quick access to common commands, functions, and syntax used in MySQL. It serves as a quick guide for developers, database administrators, and anyone working with MySQL to execute tasks more efficiently. From basic operations to complex queries, this cheat sheet includes everything one needs to manage MySQL databases effectively.
This section lists essential MySQL commands commonly used for database management.
Connecting to MySQL:
mysql -u username -p |
Create Database:
CREATE DATABASE database_name; |
Select Database:
USE database_name; |
Show Databases:
SHOW DATABASES; |
Create Table:
CREATE TABLE table_name ( column1 datatype, column2 datatype, ... ); |
Describe table structure:
DESCRIBE table_name; |
Drop a table:
DROP TABLE table_name; |
Alter a table (add a column):
ALTER TABLE table_name ADD column_name datatype; |
Rename a table:
RENAME TABLE old_table_name TO new_table_name; |
Show Tables:
SHOW TABLES; |
Insert Data:
INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); |
Insert data into specific columns(extension in MySQL):
INSERT INTO table_name SET column1 = value1, column2 = value2; |
Select Data:
SELECT column1, column2, ... FROM table_name; |
Select all data from a table:
SELECT * FROM table_name; |
Update Data:
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition; |
Delete Data:
DELETE FROM table_name WHERE condition; |
Joins are used to combine rows from two or more tables based on related columns.
Inner Join:
SELECT columns FROM table1 INNER JOIN table2 ON table1.column = table2.column; |
Left Join (Left Outer Join):
SELECT columns FROM table1 LEFT JOIN table2 ON table1.column = table2.column; |
Right Join (Right Outer Join):
SELECT columns FROM table1 RIGHT JOIN table2 ON table1.column = table2.column; |
Full Join (Full Outer Join):
SELECT columns FROM table1 FULL OUTER JOIN table2 ON table1.column = table2.column; |
Cross Join:
SELECT columns FROM table1 CROSS JOIN table2; |
Aggregation in MySQL allows to summarize data across multiple rows into a single result. This is crucial for data analysis and reporting tasks.
COUNT(): Returns the number of rows that match a specified condition.
SELECT COUNT(*) FROM employees; |
SUM(): Returns the total sum of a numeric column.
SELECT SUM(salary) FROM employees; |
AVG(): Returns the average value of a numeric column.
SELECT AVG(salary) FROM employees; |
MIN(): Returns the smallest value in a set of values.
SELECT MIN(salary) FROM employees; |
MAX(): Returns the largest value in a set of values.
SELECT MAX(salary) FROM employees; |
The GROUP BY clause is used to group rows that have the same values in specified columns into summary rows, often used with aggregation functions.
Example:
SELECT department, SUM(salary) FROM employees GROUP BY department; |
The HAVING clause filters the result of aggregation functions, similar to WHERE, but applied after grouping.
Example:
SELECT department, COUNT(*) FROM employees GROUP BY department HAVING COUNT(*) > 5; |
Functions in MySQL perform operations on data and return a value.
CONCAT(): Concatenates two or more strings.
SELECT CONCAT('Hello', ' ', 'World'); |
SUBSTRING(): Extracts a substring from a string.
SELECT SUBSTRING('Hello World', 1, 5); |
LENGTH(): Returns the length of a string.
SELECT LENGTH('Hello'); |
ABS(): Returns the absolute value of a number.
SELECT ABS(-10); |
ROUND(): Rounds a number to a specified number of decimal places.
SELECT ROUND(123.456, 2); |
FLOOR(): Returns the largest integer less than or equal to a number.
SELECT FLOOR(123.456); |
NOW(): Returns the current date and time.
SELECT NOW(); |
CURDATE(): Returns the current date.
SELECT CURDATE(); |
DATEDIFF(): Returns the difference in days between two dates.
SELECT DATEDIFF('2024-12-31', '2024-01-01'); |
Understanding MySQL syntax is crucial for writing effective queries. Below are some common syntax rules:
SQL injection is a common web security vulnerability that allows attackers to interfere with the queries made to a database. Protecting the MySQL databases from such attacks is essential.
Union-Based SQL Injection:
SELECT * FROM users WHERE id = 1 UNION SELECT username, password FROM admin; |
Error-Based SQL Injection:
SELECT * FROM users WHERE id = 1 AND 1=1; |
Use Prepared Statements:
SELECT * FROM users WHERE username = ?; |
Create User:
CREATE USER 'username'@'host' IDENTIFIED BY 'password'; |
Grant Privileges:
GRANT ALL PRIVILEGES ON database_name.* TO 'username'@'host'; |
Show Grants:
SHOW GRANTS FOR 'username'@'host'; |
Delete a user:
DROP USER 'username'@'localhost'; |
Revoke Privileges:
REVOKE ALL PRIVILEGES ON database_name.* FROM 'username'@'host'; |
Backup Database:
mysqldump -u username -p database_name > backup.sql |
Restore Database:
mysql -u username -p database_name < backup.sql |
Create an index:
CREATE INDEX index_name ON table_name(column_name); |
Drop an index:
DROP INDEX index_name ON table_name; |
Start a transaction:
START TRANSACTION; |
Commit a transaction:
COMMIT; |
Rollback a transaction:
ROLLBACK; |
Show the current user:
SELECT USER(); |
Check the version of MySQL:
SELECT VERSION(); |
Exit MySQL:
EXIT; |
Having a MySQL cheat sheet is essential for both beginners and experienced users. It not only aids in quickly referencing common commands, joins, functions, and syntax but also helps in understanding and preventing SQL injection attacks. By keeping this cheat sheet handy, one can enhance their MySQL skills, improve database management efficiency, and prepare effectively for interviews.