EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Cheat Sheet
 

MySQL Cheat Sheet

Updated May 31, 2023

MySQL Cheat Sheet

 

 

Introduction to MySQL Cheat Sheet

MySQL Cheat Sheet is a collection of all frequently used queries on a single page. It can include queries for working with databases, tables, indexes, views, triggers, stored procedures, and stored functions. It might also include the basic queries to retrieve data from a table, search for data in a table, modify data in a table, get a count of rows, etc. A list of such queries together forms a cheat sheet of MySQL. Mostly the queries to CREATE, DROP, UPDATE, and INSERT are mentioned in the cheat sheet.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Queries of MySQL Cheat Sheet

Let’s have a look at some basic frequently used queries in MySQL.

1. Database-related queries

Queries that are most commonly used at the database level are below:

  • To create a DB:

The syntax will be:

CREATE DATABASE IF NOT EXISTS db_name;

Sample Query:

CREATE DATABASE IF NOT EXISTS trial;

Output:

MySQL Cheat Sheet output 1

New DB:

MySQL Cheat Sheet output 2

To Use a DB:

The syntax will be:

USE db_name;

Sample Query:

USE trial;

Output:

MySQL Cheat Sheet output 3

  • To Show all DB in the current server:

The syntax will be:

SHOW DATABASES;

Sample Query:

SHOW DATABASES;

Output:

MySQL Cheat Sheet output 4

All DB:

MySQL Cheat Sheet output 5

  • To drop a DB:

The syntax will be:

DROP DATABASE IF EXISTS db_name;

Sample Query:

DROP DATABASE IF EXISTS trial;

Output:

MySQL Cheat Sheet output 6

2. Table-related queries

Queries that are most commonly used at a table level are below:

  • To Show all tables in a DB:

The syntax will be:

SHOW TABLES;

Sample Query:

SHOW TABLES;

Double-click on the DB from which the list of tables will be pulled.

Output:

MySQL Cheat Sheet output 7

Table list in DB ‘world’:

MySQL Cheat Sheet output 8

  • To create a table:

The syntax will be:

CREATE TABLE IF NOT EXISTS table_name (
Column_list
);

Sample Query:

CREATE TABLE IF NOT EXISTS SAMPLE (
Id INT,
Emp VARCHAR (50),
JOB VARCHAR (50);

Output:

output 9

  • To ADD a new column to an existing table:

The syntax will be:

ALTER TABLE TABLE_NAME
ADD COLUMN COLUMN_NAME;

Sample Query:

ALTER TABLE SAMPLE
ADD COLUMN TITLE VARCHAR (50);

Output:

output 10

  • To DROP a new column to an existing table:

The syntax will be:

ALTER TABLE TABLE_NAME
DROP COLUMN COLUMN_NAME;

Sample Query:

ALTER TABLE SAMPLE
DROP COLUMN TITLE;

Output:

output 11

  • To show a column of a table:

The syntax will be:

DESCRIBE TABLE_NAME;

Sample Query:

DESCRIBE SAMPLE;

Output:

output 12

  • To display details of a column in a table:

The syntax will be:

DESCRIBE TABLE_NAME COLUMN_NAME;

Sample Query:

DESCRIBE SAMPLE Id;

Output:

output 13

  • To drop a table:

The syntax will be:

DROP TABLE IF EXISTS TABLE_NAME;

Sample Query:

DROP TABLE IF EXISTS SAMPLE;

Output:

output 14

3. Index-related queries

Queries that are most commonly used at the indexes level are below:

  • To create an index:

The syntax will be:

CREATE INDEX index_name
ON table_name (column_name);

Sample Query:

CREATE INDEX TRIAL
ON COUNTRY (CODE,NAME);

Output:

output 15

  • To drop an index:

The syntax will be:

DROP INDEX Index_name
ON table_name;

Sample Query:

DROP INDEX TRIAL
ON COUNTRY;

Output:

output 16

4. Triggers-related queries

Queries that are most commonly used on triggers are below:

  • To create the trigger:

The syntax will be:

DELIMITER $$
CREATE TRIGGER trigger_name
trigger_time trigger_event
ON table_name FOR EACH ROW
BEGIN
-- statements
END$$
DELIMITER ;
Sample Query:
DELIMITER $$
CREATE TRIGGER Updated_Sales_Data
AFTER UPDATE
ON InitialSales FOR EACH ROW
BEGIN
IFOLD.qty<>new.qtyTHEN
INSERT INTO Sales Updates(sales_Id,InitialQuantity, UpdatedQuantity)
VALUES(old.prodId, old.qty, new.qty);
END IF;
END$$
DELIMITER;

Output:

output 17

  • To drop a trigger:

The syntax will be:

DROP TRIGGER trigger_name;

Sample Query:

DROP TRIGGER Updated_Sales_Data;

Output:

output 18

5. Stored procedure-related queries

Queries that are most commonly used upon stored procedures are below:

  • To create a stored procedure:

Syntax will be

DELIMITER $$
CREATE PROCEDURE procedure_name(parameter_list)
BEGIN
body;
END $$
DELIMITER ;
  • To drop a stored procedure:

The syntax will be:

DROP PROCEDURE IF EXISTS procedure_name;

6. Stored Functions related queries

Queries that are most commonly used upon stored functions are below:

  • To create a stored function:
Syntax will be
DELIMITER $$
CREATE FUNCTION function_name(parameter_list)
RETURNS datatype
NOT DETERMINISTIC
BEGIN
-- statements
END $$
DELIMITER ;
  • To drop a stored function:

The syntax will be:

DROP FUNCTION IF EXISTS function_name;

7. Queries to retrieve data from the table

Queries that are most commonly used to retrieve data from a table are below:

  • To select all data:
SELECT * FROM table_name;
  • Select specific columns only:
SELECT COLUMN1, COLUMN2 …
FROM TABLE_NAME;
  • To select only distinct values from a column:
SELECT DISTINCT COLUMN_NAME
FROM TABLE_NAME;
  • Retrieve data depending upon a “WHERE” condition:
SELECT COLUMN_NAMES
FROM table_name
WHERE CONDITION;
  • To select data based on multiple tables: we have options to use JOIN, INNER JOIN, LEFT JOIN, RIGHT JOIN, etc.

INNER JOIN:

SELECT COLUMN_NAMES
FROM table_1_name
INNER JOIN table_2_name
ON coindition;

LEFT JOIN

SELECT COLUMN_NAMES
FROM table_1_name
LEFT JOIN table_2_name
ON coindition;

RIGHT JOIN:

SELECT COLUMN_NAMES
FROM table_1_name
RIGHT JOIN table_2_name
ON condition;
  • To retrieve the count of rows in a table:
SELECT COUNT (*)
FROM TABLE_NAME;
  • To retrieve data in a specific sorted order:
SELECT COLUMN_NAMES
FROM table_name
ORDER BY column_name ASC|DESC;
  • ASC for ascending order
  • DESC for descending order
  • To retrieve data in a grouped class:
SELECT COLUMN_NAMES
FROM table_name
GROUP BY column_names ;
  • To filter out data with the ‘having’ condition:
SELECT COLUMN_NAMES
FROM table_name
GROUP BY column_name
HAVING condition ;

8. Queries to modify data in the table

Queries that are most commonly used to modify data in a table are below:

  • To insert data into a table:
  • One row at a time:
INSERT INTO table_name(column_names)
VALUES(value);
  • Multiple rows on a single go:
INSERT INTO table_name(column_names)
VALUES(value1),
(value2),
(value3);
  • Update data of all rows in a table:
UPDATE TABLE_NAME
SET COLUMN_NAME = VALUE;
  • Update based on a given condition:
UPDATE TABLE_NAME
SET COLUMN_NAME = VALUE;
WHERE CONDITION;
  • To DELETE all rows from a table:
DELETE FROM TABLE_NAME;
  • To delete rows based on a condition:
DELETE FROM TABLE_NAME
WHERE CONDITION;

Recommended Articles

We hope that this EDUCBA information on “MySQL Cheat Sheet” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

  1. MySQL EXTRACT()
  2. Examples of MySQL Limit
  3. MySQL Index
  4. MySQL Alias

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW