#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
MySQL Tutorial · Data Analytics

MySQL Commands Cheat Sheet: Most-Used SQL Commands

This is a working cheat sheet: the MySQL commands you will type most often, grouped by what they act on, followed by one short session that uses them on a real table so you can see what each one prints. Everything was run in the mysql command-line client on MySQL 8.0.

The commands at a glance

TaskCommand
List databasesSHOW DATABASES;
Create and switchCREATE DATABASE college; then USE college;
Which database am I in?SELECT DATABASE();
List tablesSHOW TABLES;
See columnsDESCRIBE students;
See the full definitionSHOW CREATE TABLE students\G
Read rowsSELECT ... FROM ... WHERE ... ORDER BY ... LIMIT n;
SummariseSELECT col, COUNT(*) FROM t GROUP BY col;
Add rowsINSERT INTO t (cols) VALUES (...);
Change rowsUPDATE t SET col = v WHERE ...;
Remove rowsDELETE FROM t WHERE ...;
Change structureALTER TABLE t ADD COLUMN ...;
Remove a tableDROP TABLE t;
Users and accessCREATE USER, GRANT, REVOKE, SHOW GRANTS;
DiagnoseEXPLAIN SELECT ...;, SHOW PROCESSLIST;, SELECT VERSION();

In the command-line client, ending a statement with \G instead of ; prints each row vertically, which is easier to read for wide output.

One session, start to finish

CREATE TABLE students (
  id       INT AUTO_INCREMENT PRIMARY KEY,
  name     VARCHAR(40) NOT NULL,
  course   VARCHAR(20),
  city     VARCHAR(20),
  fee_paid DECIMAL(8,2) DEFAULT 0
);
INSERT INTO students (name, course, city, fee_paid) VALUES
('Aman','BCA','Noida',15000), ('Riya','B.Tech','Delhi',22000),
('Karan','BCA','Delhi',0), ('Neha','MCA','Noida',18000),
('Farhan','B.Tech','Lucknow',22000);

DESCRIBE students;
FieldTypeNullKeyDefaultExtra
idintNOPRINULLauto_increment
namevarchar(40)NONULL
coursevarchar(20)YESNULL
cityvarchar(20)YESNULL
fee_paiddecimal(8,2)YES0.00

Filter and sort

SELECT name, course FROM students WHERE city = 'Delhi' ORDER BY name;
namecourse
KaranBCA
RiyaB.Tech

Group and total

SELECT course, COUNT(*) AS students, SUM(fee_paid) AS collected
FROM students GROUP BY course ORDER BY collected DESC;
coursestudentscollected
B.Tech244000.00
MCA118000.00
BCA215000.00

Change data and structure

UPDATE students SET fee_paid = 5000 WHERE name = 'Karan';
ALTER TABLE students ADD COLUMN email VARCHAR(80) AFTER name;
DELETE FROM students WHERE id = 5;
SELECT id, name, email, fee_paid FROM students;
idnameemailfee_paid
1AmanNULL15000.00
2RiyaNULL22000.00
3KaranNULL5000.00
4NehaNULL18000.00

The new email column is NULL for existing rows because no default was given. AFTER name only controls where it appears in SELECT *.

Check what you built

SHOW CREATE TABLE students\G
Create Table: CREATE TABLE `students` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(40) NOT NULL,
  `email` varchar(80) DEFAULT NULL,
  `course` varchar(20) DEFAULT NULL,
  `city` varchar(20) DEFAULT NULL,
  `fee_paid` decimal(8,2) DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

Note AUTO_INCREMENT=6: deleting row 5 does not free its ID. The next insert gets 6.

Pitfalls worth remembering

For the categories behind these commands, see DDL, DML, DCL and TCL in MySQL. To go deeper into editing rows, read INSERT, UPDATE and DELETE with examples.

Become a Job-Ready Data Analyst

Master MySQL, Advanced Excel, Power BI, Tableau and Python with Uncodemy's Data Analytics Training in Noida — live projects, expert mentors and Dedicated Career & Placement Assistance.

Enroll in Data Analytics Course →