MySQL Practice Questions — DDL, DML, Joins & Aggregates (2026)
Advertisement
Introduction
Why This Matters
Practice questions are the fastest path from theory to fluency. Reading about SQL syntax is not enough — you must write queries, make mistakes, debug them, and develop the intuition to match a question to the right command. These questions are modelled on school board exams, university assessments, and common SQL interview questions. Work through each section in order; later sections build on earlier ones.
DDL — Database-Related Commands
-
If a database named
Employeeexists, write the MySQL command to start working in it. -
Write the MySQL command to open an already-existing database named
LIBRARY. -
Write the MySQL command to display the name of the currently active database.
-
Write the command to list all databases available on your MySQL server.
-
Write the command to create a new database named
School. -
Sharmila wants to make the database
COMPANYactive. Write the MySQL command for this. -
Write the command to permanently delete the database named
Clients. -
Suggest suitable commands for the following:
- Display the list of all existing databases.
- Switch to the database named
City. - Remove the pre-existing database named
Clients.
-
What is the difference between
DROP DATABASEandUSE? -
What does
SELECT DATABASE()return when no database has been selected?
Answers to Selected Questions:
-- Q1 & Q2: Open an existing database
USE Employee;
USE LIBRARY;
-- Q3: Currently active database
SELECT DATABASE();
-- Q4: List all databases
SHOW DATABASES;
-- Q5: Create new database
CREATE DATABASE School;
-- Q7: Delete a database
DROP DATABASE Clients;DDL — Table-Related Commands (Excluding ALTER)
- Write an SQL query to create a table
Menuwith the following structure:
| Field | Type | Constraint |
|---|---|---|
| ItemCode | VARCHAR(5) | PRIMARY KEY |
| ItemName | VARCHAR(20) | |
| Category | VARCHAR(20) | |
| Price | DECIMAL(5,2) |
-
Can a table have multiple primary keys? Can it have multiple foreign keys?
-
In a
Studenttable with columns RollNo, Name, and Address — which column should be the primary key, and why? -
Ms. Mirana wants to remove the entire table
BACKUPalong with its structure. What MySQL command should she use? -
Write the MySQL command to create the table
STOCK:
| Column | Type | Constraint |
|---|---|---|
| Id | VARCHAR(10) | PRIMARY KEY |
| Name | VARCHAR(30) | |
| Company | VARCHAR(30) | |
| Price | DECIMAL(8,2) | NOT NULL |
-
What is one similarity and one difference between
CHARandVARCHARdata types? -
Saumya created a table
Productlast week and forgot the structure. Which command should she use to view it? -
Roli wants to list the names of all tables in her database
Gadgets. Which command should she use? -
Name the SQL commands used to:
- Physically delete a table from the database.
- Display the structure of a table.
-
An attribute A of type
VARCHAR(20)has the value"Amit". Attribute B of typeCHAR(20)has the value"Karanita". How many characters are occupied by A? By B?
Answers to Selected Questions:
-- Q1: Create Menu table
CREATE TABLE Menu (
ItemCode VARCHAR(5) PRIMARY KEY,
ItemName VARCHAR(20),
Category VARCHAR(20),
Price DECIMAL(5,2)
);
-- Q4: Remove table entirely
DROP TABLE BACKUP;
-- Q5: Create STOCK table
CREATE TABLE STOCK (
Id VARCHAR(10) PRIMARY KEY,
Name VARCHAR(30),
Company VARCHAR(30),
Price DECIMAL(8,2) NOT NULL
);
-- Q7: View table structure
DESC Product;
-- Q8: List all tables
USE Gadgets;
SHOW TABLES;
-- Q9 answers: DROP TABLE / DESC or DESCRIBE
-- Q10: VARCHAR(20) stores 4 bytes for "Amit"; CHAR(20) always stores 20 bytesDDL — ALTER TABLE Commands
-
Sahil created a table but later found he needed another column. Which command should he use?
-
Simrita forgot to set the primary key when creating table
Customer. Write the command to setCustIDas the primary key now. -
Write SQL to remove the column
Hobbiesfrom tableStudent. -
Ms. Sharma forgot to include
Game_Played(VARCHAR(30)) when creatingStudent. Write the command to add it now. -
Rashi wants to add column
Hobbies(VARCHAR(50)) to tableStudent. She wrote:MODIFY TABLE Student Hobbies VARCHAR;Rewrite the correct statement. -
Ms. Shalini created table
Employeewith columns Ename, Department, Salary. She forgot to add a primary key columnempid. Write the SQL to addempidas a primary key. -
Simrita wrongly set
CUSTNAMEas the primary key in tableCustomer. Write the command to remove the primary key constraint. -
Mr. Akshat wants to remove the
NOT NULLconstraint from thenamefield in tableemployees. Write the command.
Answers to Selected Questions:
-- Q1: Add a column
ALTER TABLE tablename ADD newcolumn DATATYPE(size);
-- Q2: Add primary key
ALTER TABLE Customer ADD PRIMARY KEY (CustID);
-- Q3: Remove a column
ALTER TABLE Student DROP Hobbies;
-- Q4: Add Game_Played
ALTER TABLE Student ADD Game_Played VARCHAR(30);
-- Q5: Correct version of Rashi's query
ALTER TABLE Student ADD Hobbies VARCHAR(50);
-- Q6: Add empid as primary key
ALTER TABLE Employee ADD empid INT PRIMARY KEY;
-- Q7: Remove primary key
ALTER TABLE Customer DROP PRIMARY KEY;
-- Q8: Remove NOT NULL (sets to allow NULLs)
ALTER TABLE employees MODIFY name VARCHAR(30) NULL;DML — INSERT INTO Commands
-
Rama cannot change a column to NULL. What constraint did she specify when creating the table?
-
Consider the
RESULTtable. Write the command to insert:6, "Mohan", 500, "English", 73, "Second" -
How is NULL different from 0 (zero)?
-
Rewrite the following SQL statement after correcting errors:
INSERT IN STUDENT(RNO,MARKS) VALUE (5,78.5); -
Charvi is inserting "Sharma" into
LastNameof tableEmpbut gets an error:INSERT INTO Emp('Sharma') VALUES(LastName);Write the correct statement. -
What is the full form of DDL and DML?
Answers:
-- Q2: Correct INSERT
INSERT INTO RESULT VALUES (6, 'Mohan', 500, 'English', 73, 'Second');
-- Q4: Corrected (IN -> INTO, VALUE -> VALUES)
INSERT INTO STUDENT (RNO, MARKS) VALUES (5, 78.5);
-- Q5: Corrected column/value order
INSERT INTO Emp (LastName) VALUES ('Sharma');
-- Q3: NULL means no value / unknown; 0 is a numeric value
-- Q1: NOT NULL constraint
-- Q6: DDL = Data Definition Language, DML = Data Manipulation LanguageDML — UPDATE and DELETE Commands
-
What is the purpose of
DROP TABLE? How is it different fromDELETE? -
Write the command to increase the Price of all Products by 20 in the
Producttable. -
Write the UPDATE command to change "Sharma" to "Singh" in the
LastNamecolumn of tableEmployee. -
What is the use of the UPDATE statement? How is it different from ALTER?
-
Write the command to change
BrandNameto "Fit Trend India" for item withICODE = "G101"in tableGYM. -
Write the UPDATE statement to increase commission by 100.00 in the
Commissioncolumn of tableEmp. -
Consider the
GARMENTtable. Write commands to:- Change the colour of garment with code 116 to "Orange".
- Increase the price of all XL garments by 10%.
- Delete the record with GCode "116".
Answers:
-- Q2: Increase all prices by 20
UPDATE Product SET Price = Price + 20;
-- Q3: Change a name
UPDATE Employee SET LastName = 'Singh' WHERE LastName = 'Sharma';
-- Q5: Update brand name
UPDATE GYM SET BrandName = 'Fit Trend India' WHERE ICODE = 'G101';
-- Q6: Increase commission
UPDATE Emp SET Commission = Commission + 100.00;
-- Q7a: Change colour
UPDATE GARMENT SET Colour = 'Orange' WHERE GCode = 116;
-- Q7b: Increase price by 10%
UPDATE GARMENT SET Price = Price + Price * 10 / 100 WHERE Size = 'XL';
-- Q7c: Delete record
DELETE FROM GARMENT WHERE GCode = 116;DML — SELECT Command Questions
-
Pooja wrote
SELECT * FROM Book WHERE Price = NULL;to find books with no price entered. Why does this query return no results? Write the correct query. -
Sarthya wrote
SELECT * FROM Result WHERE Grade = "Null";to find students with no grade. This does not work. Write the correct query. -
In MySQL, Sumit gets 6 rows from
SELECT ItemCode FROM ITEMand Fauzia gets 4 rows from the same table. Which extra keyword did Fauzia use? -
Mr. Tandon wants all employees ordered by ENAME ascending, then DEPT ascending. He wrote:
SELECT * FROM EMP ORDER BY NAME DESC, DEPT;Rewrite the correct query. -
Write queries on the
studenttable for:- All students whose name starts with "A".
- All students whose name ends with "Singh".
- All students whose name has "Kumar" anywhere in it.
- All students whose name has exactly 5 characters.
Answers:
-- Q1: IS NULL, not = NULL
SELECT * FROM Book WHERE Price IS NULL;
-- Q2: IS NULL, not = "Null"
SELECT * FROM Result WHERE Grade IS NULL;
-- Q3: Fauzia used DISTINCT
-- Q4: Correct ORDER BY
SELECT * FROM EMP ORDER BY ENAME ASC, DEPT ASC;
-- Q5 queries
SELECT * FROM student WHERE name LIKE 'A%';
SELECT * FROM student WHERE name LIKE '%Singh';
SELECT * FROM student WHERE name LIKE '%Kumar%';
SELECT * FROM student WHERE name LIKE '_____'; -- 5 underscoresCommon Mistakes
- Writing
= NULLinstead ofIS NULLin WHERE conditions — this is the most frequently tested SQL error. - Using
ALTER TABLEto change data values (useUPDATEinstead). - Forgetting that
DROP TABLEremoves structure and data, whileDELETE FROMremoves only rows. - Not enclosing string values in quotes in
INSERT INTO. - Using
MODIFY TABLEinstead ofALTER TABLE ... MODIFY.
Best Practices
- Always test
UPDATEandDELETEwith aSELECTusing the sameWHEREclause first. - Use
IF NOT EXISTSandIF EXISTSin DDL scripts so they are safe to re-run. - Back up data before running any
DROPor destructiveDELETE. - Verify your query logic with
EXPLAINbefore running on large tables. - Keep practice consistent — solve at least one SQL question set per day when studying.
Key Takeaways
USE dbnameopens a database;SHOW DATABASESlists all available databases on the server.DROP TABLEremoves the table and all data permanently;DELETE FROM tableremoves rows but keeps the table structure.ALTER TABLEchanges schema structure;UPDATEchanges data values inside the table.NULLmeans no value or unknown; it is not equal to zero, empty string, or the text "NULL".IS NULLandIS NOT NULLare the only correct operators to test for absent values in SQL.DISTINCTin aSELECTremoves duplicate values from the result set.LIKE 'A%'matches any string starting with A;LIKE '%A'matches strings ending with A;_matches exactly one character.- In
ALTER TABLE, useADDto add a column,DROPto remove it, andMODIFYto change its type or constraint.
Advertisement