EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials MySQL Tutorial MySQL Cheat Sheet
Secondary Sidebar
MySQL Tutorial
  • Queries
    • MySQL Queries
    • MySQL Query Commands
    • SELECT in MySQL
    • MySQL INSERT IGNORE
    • MySQL having
    • ORDER BY in MySQL
    • MySQL Cheat Sheet
    • MySQL ORDER BY Random
    • MySQL ORDER BY DESC
    • MySQL GROUP BY
    • MySQL GROUP BY Count
    • MySQL GROUP BY month
    • MySQL WHERE Clause
    • MySQL WITH
    • MySQL FETCH
    • MySQL DDL
    • MySQL DML
    • MySQL WHERE IN Array
    • MySQL Fetch Array
    • MySQL ISNULL
    • MySQL Index Types
    • Mysql? Export Schema
    • Amazon RDS for MySQL
    • MySQL greatest
  • Basic
    • MySQL floor
    • MySQL DESCRIBE table
    • MySQL encryption
    • Introduction to MySQL
    • Is SQL Server a Database?
    • What is MySQL
    • Is MySQL Programming Language
    • MySQL Server
    • MySQL AB
    • MySQL Community Server
    • How To Install MySQL
    • MySQL Versions
    • MySQL OpenSource
    • MySQL GUI Tools
    • MySQL Grant
    • MySQL Error 1064
    • MySQL Drop Database
    • MySQL not equal
    • MySQL SELECT INTO Variable
    • MySQL Commands
    • MySQL Operators
    • What is MySQL Schema
    • Wildcards in MySQL
    • MySQL Constraints
    • MySQL Administration
    • MySQL Data Type
    • MYSQL COMMIT
    • MySQL FORMAT
    • Timestamp to Date in MySQL
    • MySQL DATEDIFF
    • MySQL?Incremental Backup
    • MySQL JSON Data Type
    • MySQL ENUM
    • MySQL Default Port
    • Cheat Sheet MySQL
  • Database
    • What is Data Modeling
    • What is Data Processing
    • DBMS Architecture
    • DBMS Keys
    • Careers in Database Administration
    • What is MySQL Database
    • MySQL Relational Database
    • How to Connect Database to MySQL
    • MySQL Database Repair
    • RDBMS Interview Questions
    • DBMS Interview Questions
  • Functions
    • MySQL Function
    • MySQL Aggregate Function
    • MySQL String functions
    • MySQL Date Functions
    • MySQL Window Functions
    • MySQL Math Functions
    • MySQL Boolean
    • Cursor in MySQL
    • Condition in MySQL
    • MySQL BETWEEN
    • Insert in MySQL
    • MySQL IFNULL()
    • MySQL TIMESTAMPDIFF()
    • MySQL COALESCE()
    • MySQL count()
    • MIN() in MySQL
    • MySQL Numeric
    • MySQL field()
    • MySQL FIND_IN_SET()
    • MySQL avg()
    • MySQL MAX() Function
    • MySQL BIN()
    • MySQL Concat
    • MySQL DECODE()
    • MySQL REGEXP_REPLACE()
    • MySQL Asynchronous
    • MySQL innodb_buffer_pool_size
    • MySQL key_buffer_size
    • MySQL TRUNCATE()
    • MySQL ROW_NUMBER()
    • NOT in MySQL
    • MySQL IN Operator
    • LIKE in MySQL
    • ANY in MySQL
    • MySQL NOT IN
    • MySQL CHECK Constraint
    • MySQL DISTINCT
    • MySQL ALL
    • MySQL Union
    • MySQL UNION ALL
    • MySQL EXISTS
    • MySQL ON DELETE CASCADE
    • MySQL REGEXP
    • MySQL Index
    • MySQL Add Index
    • MySQL REINDEX
    • MySQL UNIQUE INDEX
    • MySQL Clustered Index
    • MySQL? InnoDB Cluster
    • Table in MySQL
    • ALTER TABLE MySQL
    • MySQL Temporary Table
    • MySQL Clone Table
    • MySQL Repair Table
    • MySQL Lock Table
    • MySQL Optimize Table
    • TRUNCATE TABLE MySQL
    • MySQL Table Dump
    • MySQL Update Set
    • MySQL ALTER TABLE Add Column
    • MySQL RANK()
    • MySQL CTE
    • MySQL LAG()
    • MySQL GROUP_CONCAT()
    • MySQL EXTRACT()
    • MySQL REPLACE
    • MySQL AUTO_INCREMENT
    • MySQL SYSDATE()
    • MySQL NULLIF()
    • MySQL Substring
    • MySQL SUBSTRING_INDEX()
    • MySQL LOWERCASE
    • MySQL Row
    • MySQL NOW
    • MySQL CEIL
    • MySQL Alias
    • MySQL Trigger
    • MySQL SHOW Triggers
    • MySQL UPDATE Trigger
    • MySQL DELETE Trigger
    • MySQL AFTER UPDATE Trigger
    • MySQL Stored Procedure
    • ROLLUP in MySQL
    • MySQL? INSTR()
    • MySQL Subquery
    • MySQL Timestamp
    • MySQL? Hour()
    • MySQL MOD()
    • MySQL DATE_FORMAT()
    • ALTER Column in MySQL
    • MySQL Rename Column
    • MySQL Interval
    • MySQL CURDATE
    • MySQL BIT
    • MySQL Binlog
    • MySQL Average
    • MySQL TEXT
    • MySQL SHOW
    • MySQL Offset
    • MySQL Timezone
    • mysql_real_escape_string
    • MySQL Datetime
    • MySQL DATE_SUB()
    • MySQL FULLTEXT
    • MySQL DATE_ADD()
    • MySQL sum()
    • MySQL Merge
    • MySQL BigInt
    • MySQL ROUND
    • MySQL VARCHAR
    • MySQL Decimal
    • MySQL Limit
    • MySQL today()
    • MySQL WEEKDAY
    • MySQL Split
    • MySQL Create Function
    • MySQL BLOB
    • MySQL encode()
    • MySQL Primary Key
    • MySQL Foreign Key
    • Unique Key in MySQL
    • MySQL Drop Foreign Key
    • MySQL DROP TRIGGER
    • MYSQL Database
    • Delete Database MySQL
    • MySQL Root
    • MySQL Root Password
    • MySQL Client
    • MySQL Users
    • MySQL?User Permissions
    • MySQL add user
    • MySQL List User
    • MySQL Show Users
    • MySQL User Password
    • MySQL?Cardinality
    • MySQL Workbench
    • MySQL Backup
    • MySQL REVOKE
    • MySQL Dump
    • MySQL Cluster
    • MySQL Partitioning
    • MySQL Full Text Search
    • MySQL Admin Tool
    • MySQL Export Database
    • MySQL Export to CSV
  • Joins
    • Joins in MySQL
    • MySQL Outer Join
    • Left Outer Join in MySQL
    • MySQL Self Join
    • Natural Join in MySQL
    • MySQL DELETE JOIN
    • MySQL Update Join
    • MySQL Cross Join
  • Advanced
    • MySQL Formatter
    • MySQL TINYINT
    • MySQL Grant All Privileges
    • MySQL DROP TABLE
    • MySQL rename database
    • MySQL Flush Privileges
    • MySQL super Privilege
    • MySQL Character Set
    • MySQL Log File
    • MySQL Flush Log
    • Grant Privileges MySQL
    • MySQL WHILE LOOP
    • IF Statement in MySQL
    • MySQL CASE Statement
    • MySQL IF Function
    • MySQL IF EXISTS
    • MySQL UUID
    • Views in MySQL
    • MySQL Replication
    • MySQL Partition
    • Toad for MySQL
    • Navicat for MySQL
    • MySQL AES_Encrypt
    • MySQL Performance Tuning
    • MySQL Transaction
    • MySQL? sort_buffer_size
    • MySQL? Sync
    • MySQL? Query Cache
    • MySQL Collation
    • MySQL ODBC Driver
    • MySQL Partitioning
    • MySQL InnoDB
    • MySQL Float vs Decimal
    • MySQL Union vs Union All
  • Interview Questions
    • MySQL Interview Questions

Related Courses

MS SQL Certification Courses

Oracle Certification Courses

PL/SQL Certification Courses

MySQL Cheat Sheet

MySQL Cheat Sheet

Introduction to MySQL Cheat Sheet

MySQL Cheat Sheet is just a collection of all frequently used MySQL 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, modifying data in a table, getting a count of rows, and so on. 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.

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

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;

Make sure to double click on the DB from which list of tables is to 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 show 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 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;

  • To 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;

  • To 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;

  • To 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

This is a guide to MySQL Cheat Sheet. Here we discuss the list of some basic frequently used queries together forms a cheat sheet of MySQL. You may also have a look at the following articles to learn more –

  1. MySQL EXTRACT()
  2. MySQL Limit
  3. MySQL Index
  4. MySQL Alias
Popular Course in this category
MySQL Training Program (12 Courses, 10 Projects)
  12 Online Courses |  10 Hands-on Projects |  92+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

MS SQL Training (16 Courses, 11+ Projects)4.9
Oracle Training (14 Courses, 8+ Projects)4.8
PL SQL Training (4 Courses, 2+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more