MySQL Practice Questions — DDL, DML, Joins & Aggregates (2026)

Sanjeev SharmaSanjeev Sharma
10 min read

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.

  1. If a database named Employee exists, write the MySQL command to start working in it.

  2. Write the MySQL command to open an already-existing database named LIBRARY.

  3. Write the MySQL command to display the name of the currently active database.

  4. Write the command to list all databases available on your MySQL server.

  5. Write the command to create a new database named School.

  6. Sharmila wants to make the database COMPANY active. Write the MySQL command for this.

  7. Write the command to permanently delete the database named Clients.

  8. 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.
  9. What is the difference between DROP DATABASE and USE?

  10. 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;
  1. Write an SQL query to create a table Menu with the following structure:
FieldTypeConstraint
ItemCodeVARCHAR(5)PRIMARY KEY
ItemNameVARCHAR(20)
CategoryVARCHAR(20)
PriceDECIMAL(5,2)
  1. Can a table have multiple primary keys? Can it have multiple foreign keys?

  2. In a Student table with columns RollNo, Name, and Address — which column should be the primary key, and why?

  3. Ms. Mirana wants to remove the entire table BACKUP along with its structure. What MySQL command should she use?

  4. Write the MySQL command to create the table STOCK:

ColumnTypeConstraint
IdVARCHAR(10)PRIMARY KEY
NameVARCHAR(30)
CompanyVARCHAR(30)
PriceDECIMAL(8,2)NOT NULL
  1. What is one similarity and one difference between CHAR and VARCHAR data types?

  2. Saumya created a table Product last week and forgot the structure. Which command should she use to view it?

  3. Roli wants to list the names of all tables in her database Gadgets. Which command should she use?

  4. Name the SQL commands used to:

    • Physically delete a table from the database.
    • Display the structure of a table.
  5. An attribute A of type VARCHAR(20) has the value "Amit". Attribute B of type CHAR(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 bytes

DDL — ALTER TABLE Commands

  1. Sahil created a table but later found he needed another column. Which command should he use?

  2. Simrita forgot to set the primary key when creating table Customer. Write the command to set CustID as the primary key now.

  3. Write SQL to remove the column Hobbies from table Student.

  4. Ms. Sharma forgot to include Game_Played (VARCHAR(30)) when creating Student. Write the command to add it now.

  5. Rashi wants to add column Hobbies (VARCHAR(50)) to table Student. She wrote: MODIFY TABLE Student Hobbies VARCHAR; Rewrite the correct statement.

  6. Ms. Shalini created table Employee with columns Ename, Department, Salary. She forgot to add a primary key column empid. Write the SQL to add empid as a primary key.

  7. Simrita wrongly set CUSTNAME as the primary key in table Customer. Write the command to remove the primary key constraint.

  8. Mr. Akshat wants to remove the NOT NULL constraint from the name field in table employees. 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

  1. Rama cannot change a column to NULL. What constraint did she specify when creating the table?

  2. Consider the RESULT table. Write the command to insert: 6, "Mohan", 500, "English", 73, "Second"

  3. How is NULL different from 0 (zero)?

  4. Rewrite the following SQL statement after correcting errors: INSERT IN STUDENT(RNO,MARKS) VALUE (5,78.5);

  5. Charvi is inserting "Sharma" into LastName of table Emp but gets an error: INSERT INTO Emp('Sharma') VALUES(LastName); Write the correct statement.

  6. 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 Language

DML — UPDATE and DELETE Commands

  1. What is the purpose of DROP TABLE? How is it different from DELETE?

  2. Write the command to increase the Price of all Products by 20 in the Product table.

  3. Write the UPDATE command to change "Sharma" to "Singh" in the LastName column of table Employee.

  4. What is the use of the UPDATE statement? How is it different from ALTER?

  5. Write the command to change BrandName to "Fit Trend India" for item with ICODE = "G101" in table GYM.

  6. Write the UPDATE statement to increase commission by 100.00 in the Commission column of table Emp.

  7. Consider the GARMENT table. 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

  1. 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.

  2. Sarthya wrote SELECT * FROM Result WHERE Grade = "Null"; to find students with no grade. This does not work. Write the correct query.

  3. In MySQL, Sumit gets 6 rows from SELECT ItemCode FROM ITEM and Fauzia gets 4 rows from the same table. Which extra keyword did Fauzia use?

  4. 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.

  5. Write queries on the student table 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 underscores

Common Mistakes

  • Writing = NULL instead of IS NULL in WHERE conditions — this is the most frequently tested SQL error.
  • Using ALTER TABLE to change data values (use UPDATE instead).
  • Forgetting that DROP TABLE removes structure and data, while DELETE FROM removes only rows.
  • Not enclosing string values in quotes in INSERT INTO.
  • Using MODIFY TABLE instead of ALTER TABLE ... MODIFY.

Best Practices

  • Always test UPDATE and DELETE with a SELECT using the same WHERE clause first.
  • Use IF NOT EXISTS and IF EXISTS in DDL scripts so they are safe to re-run.
  • Back up data before running any DROP or destructive DELETE.
  • Verify your query logic with EXPLAIN before running on large tables.
  • Keep practice consistent — solve at least one SQL question set per day when studying.

Key Takeaways

  • USE dbname opens a database; SHOW DATABASES lists all available databases on the server.
  • DROP TABLE removes the table and all data permanently; DELETE FROM table removes rows but keeps the table structure.
  • ALTER TABLE changes schema structure; UPDATE changes data values inside the table.
  • NULL means no value or unknown; it is not equal to zero, empty string, or the text "NULL".
  • IS NULL and IS NOT NULL are the only correct operators to test for absent values in SQL.
  • DISTINCT in a SELECT removes 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, use ADD to add a column, DROP to remove it, and MODIFY to change its type or constraint.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro

Related reading