MySQL Important Commands (Class 12 CBSE) –
With Explanations
Database Commands
Command Explanation
CREATE DATABASE school; Creates a new database named 'school'.
SHOW DATABASES; Displays all existing databases.
USE school; Selects the database to work with.
DROP DATABASE school; Deletes the database permanently.
Table Commands
Command Explanation
CREATE TABLE student (...); Creates a new table 'student'.
SHOW TABLES; Shows all tables in the current database.
DESC student; Displays structure of table (columns, types, constraints).
DROP TABLE student; Deletes the table permanently.
Insert / Update / Delete
Command Explanation
INSERT INTO student VALUES (1, 'Aman', 85, 'A'); Inserts a complete row into the table.
INSERT INTO student (RollNo, Name, Marks) VALUES
Inserts
(2, values
'Riya', 75);
for selected columns.
UPDATE student SET Marks = 90 WHERE RollNo =Updates
1; Marks of RollNo 1.
DELETE FROM student WHERE RollNo = 2; Deletes record where RollNo = 2.
Select Queries
Command Explanation
SELECT * FROM student; Shows all records and columns.
SELECT Name, Marks FROM student; Displays only Name and Marks.
SELECT DISTINCT Grade FROM student; Shows unique grades.
SELECT * FROM student WHERE Marks > 80; Fetches students with marks > 80.
SELECT * FROM student WHERE Marks BETWEEN
Shows
60 AND
students
90; scoring between 60 and 90.
SELECT * FROM student WHERE Grade IN ('A','B');Shows students with Grade A or B.
SELECT * FROM student WHERE Name LIKE 'A%';Names starting with A.
SELECT * FROM student ORDER BY Marks DESC;Arranges students in descending order of marks.
Aggregate Functions
Command Explanation
SELECT COUNT(*) FROM student; Counts total number of records.
SELECT MAX(Marks), MIN(Marks) FROM student; Finds highest and lowest marks.
SELECT AVG(Marks) FROM student; Calculates average marks.
SELECT SUM(Marks) FROM student; Finds total of all marks.
Group By & Having
Command Explanation
SELECT Grade, COUNT(*) FROM student GROUP Groups
BY Grade;
students grade-wise.
SELECT Grade, AVG(Marks) FROM student GROUP
Groups
BY Grade
by grade
HAVING
and shows
AVG(Marks)
averages
> 70;
> 70.
Constraints
Command Explanation
PRIMARY KEY Uniquely identifies each record.
NOT NULL Column cannot be empty.
DEFAULT Assigns default value if none is given.
Alter Table
Command Explanation
ALTER TABLE student ADD COLUMN Age INT; Adds a new column Age.
ALTER TABLE student MODIFY Marks FLOAT; Changes data type of Marks to float.
ALTER TABLE student DROP COLUMN Age; Deletes column Age.
Joins
Command Explanation
INNER JOIN Combines rows where condition matches in both tables.
LEFT JOIN Shows all records from left table and matching from right table.
Other Useful Commands
Command Explanation
SELECT NOW(); Shows current date and time.
SELECT UPPER(Name) FROM student; Converts names to uppercase.
SELECT LOWER(Name) FROM student; Converts names to lowercase.
SELECT LENGTH(Name) FROM student; Displays length of names.