Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

MySQL Cheatsheet | Revise the Concepts Quickly

Ujjwal Abhishek
Ujjwal Abhishek

that1. Creation and Working with databases

  1. Create a database
CREATE DATABASE [IF NOT EXISTS] database_name;

Here, IF NOT EXISTS is optional. 

   2. Use a database

USE database_name;

   3. Drop a database permanently from the server

DROP DATABASE [IF EXISTS] database_name;

   4. Show all databases in the MySQL server

SHOW DATABASES;

2. Working With Tables

  1. Create a new table
CREATE TABLE table_name(
column_definition,
....,
);

    2. Show all tables in a database

SHOW TABLES;

    3. Show the columns of a table

DESCRIBE table_name; 

    4. Show the information of a column in a table

DESCRIBE table_name column_name

    5. Delete a table

DROP TABLE [IF EXISTS] table_name;

    6. Add a new row into a table

INSERT INTO table_name(column1, column2, column3,...)
VALUES(value1, value2, value3, ...);

    7. Add a new column into a table

ALTER TABLE table_name
ADD [COLUMN] column_name;

    8. Drop a column from a table

ALTER TABLE table_name
DROP [COLUMN] column_name;

    9. Delete all rows from a table

DELETE FROM table_name;

    10. Delete rows with conditions

DELETE FROM table_name
WHERE conditions;

Working with Indexes

  1. Create an Index
CREATE INDEX index_name
ON table_name (column_list);

    2. Create a unique index

CREATE UNIQUE INDEX index_name 
ON table_name (column_list);

    3. Drop an Index

DROP INDEX index_name;

Querying data from tables

  1. Select all the columns from a table
SELECT * FROM table_name;

    2. Select data from some specified columns

SELECT column_list FROM table_name;

    3. Select data up to a specified number of rows

SELECT * FROM table_name
LIMIT count_of_rows;

    4. Select distinct data from columns

SELECT DISTINCT column_name FROM table_name;

    5. Change the column name of the output table

SELECT column_name AS alias_name
FROM table_name;

    6. Select data with conditions using WHERE clause

SELECT column_list
FROM table_name
WHERE conditions;

    7. Select data with conditions using the HAVING clause

SELECT column_list
FROM table_name
HAVING conditions;

    8. Grouping rows 

SELECT column_list
FROM table_name
GROUP BY column1, column2, column3, ...;

    9. Selecting data using pattern

SELECT column_name FROM table_name
WHERE column_name LIKE searching_pattern;

Updating data in a table

  1. Update data for all rows
UPDATE table_name
SET column1 = value1,
 column2 = value2,
 .......;

    2. Update rows with conditions

UPDATE 
table_name


SET column1 = value1,
.........
WHERE conditions;

Selecting rows using join

  1. Using Cross Join
SELECT column_name
FROM table_name
WHERE conditions;

    2. Using Left Join

SELECT column_list
FROM table1 
LEFT JOIN table2 ON condition;

    3. Using Right Join

SELECT column_list
FROM table1 
RIGHT JOIN table2 ON condition;

    4. Using Inner Join

SELECT column_list
FROM table1 
INNER JOIN table2 ON condition;

Sorting selected data 

  1. Sort data of a column of output table in ascending order
SELECT * FROM table_name 
ORDER BY column_name ASC;

Here, ASC is optional, by default the data is sorted in ascending order.    

    2. Sort data of a column of output table in descending order

SELECT * FROM table_name 
ORDER BY column_name DESC;

Some MySQL functions

  1. To count some data
SELECT COUNT(column_name) 
FROM table_name
WHERE condition;

    2. To sum the data 

SELECT SUM(column_name) 
FROM table_name
WHERE condition;

    3. To calculate the average

SELECT AVG(column_name) 
FROM table_name
WHERE condition;

Comment in MySQL

  1. To comment the single line
SELECT * FROM table_name
-- WHERE conditions;

Here, --is used to comment the line.

    2. To comment multiple lines

SELECT * FROM table_name
/*ORDER BY column_name ASC
WHERE conditions*/;

Here, all lines between /* and */ are commented out.

My SQL Operators

  1. Arithmetic Operators
OperatorDescription
+Addition
-Subtraction
*Multiplication
/Division
%Modulo

    2. Binary Operators

OperatorDescription
|Bitwise OR
&Bitwise AND 
^Bitwise exclusive OR

    3. Logical Operators

OperatorDescription
ANDReturns that satisfy all the conditions separated by AND
ORReturns that satisfy at least one condition separated by AND
NOTReturns that do not satisfy the conditions after NOT
Ujjwal Abhishek
Ujjwal Abhishek
Ujjwal is final-year CSE Undergraduate, a competitive coder, and a web developer passionate about problem-solving and data structures and algorithms.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet