MySQL Practical Questions
🔹 DDL (Data Definition Language) – 30 Questions
DDL commands involve creating, modifying, and deleting database structures.
- Create a database named
school_db
. - Delete a database named
test_db
. - Create a table
students
withid
,name
,age
, andclass
. - Add a
gender
column to thestudents
table. - Modify the
age
column toTINYINT(3)
. - Rename the
students
table topupils
. - Drop the
gender
column from thestudents
table. - Set the
id
column as the primary key instudents
. - Remove the primary key from the
students
table. - Create a unique constraint on the
email
column. - Drop the unique constraint on
email
. - Create a foreign key linking
class_id
instudents
toid
inclasses
. - Drop the foreign key constraint from
students
. - Create an index on the
name
column instudents
. - Drop the index from
name
. - Create a composite primary key using
id
andclass_id
. - Create a table
teachers
with ateacher_id
column as the primary key. - Set a default value of
5
for theage
column. - Alter the default value of
age
to6
. - Remove the default value from
age
. - Create a table
users
withid
asAUTO_INCREMENT
. - Drop the
users
table. - Create a table with a
CHECK
constraint to ensureage > 18
. - Drop the
CHECK
constraint from theage
column. - Create a table
orders
withcreated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
. - Change the column
student_name
tofull_name
. - Create a view
top_students
showing students with grades above90
. - Drop the view
top_students
. - Clone the structure of the
students
table intobackup_students
. - Create a temporary table named
temp_students
.
🔹 DML (Data Manipulation Language) – 30 Questions
DML commands involve inserting, updating, and deleting data.
- Insert a student into
students
. - Insert multiple students into
students
. - Insert a student without specifying
class
. - Insert a record with a
NULL
value inclass_id
. - Update the
age
of a student withid = 1
. - Increase the
age
of all students by 1 year. - Update students in
class_id = 3
toclass_id = 4
. - Delete a student with
id = 5
. - Delete all students where
age > 18
. - Delete all records from
students
but keep the structure. - Restore the deleted records using ROLLBACK.
- Insert a student with an id that already exists and observe the error.
- Insert a record violating a NOT NULL constraint and check the error.
- Use INSERT IGNORE to prevent errors for duplicate keys.
- Use REPLACE INTO to insert or update a record.
- Insert a record using DEFAULT values.
- Use a subquery in an INSERT INTO statement.
- Use ON DUPLICATE KEY UPDATE to update an existing record.
- Swap the age of two students.
- Insert a record into a table with a foreign key constraint.
- Delete a student and check if foreign key constraints prevent it.
- Use SET NULL on a foreign key constraint when deleting a parent record.
- Use CASCADE on a foreign key constraint when deleting a parent record.
- Restore data using ROLLBACK.
- Use SAVEPOINT and ROLLBACK TO SAVEPOINT in a transaction.
- Commit a transaction after inserting multiple records.
- Demonstrate an AUTO_INCREMENT reset.
- Insert a record with CURRENT_TIMESTAMP in a created_at column.
- Update the updated_at column automatically using ON UPDATE CURRENT_TIMESTAMP.
- Clone a table’s structure and data into a new table.
🔹 LIKE Operator – 10 Questions
- Retrieve students whose name starts with "A".
- Retrieve students whose name ends with "n".
- Retrieve students whose name contains "John".
- Find students whose name contains "o" in the second position.
- Retrieve students whose name starts with "J" and is followed by any two characters.
- Retrieve students whose name has exactly five letters.
- Retrieve students whose name starts with "S" and ends with "h".
- Retrieve students whose name does not start with "A".
- Retrieve students whose name contains "a" or "e".
- Retrieve students whose name starts with "A" and contains "o" later.
🔹 BETWEEN Operator – 10 Questions
- Retrieve students aged between 10 and 15.
- Retrieve students whose class_id is between 3 and 7.
- Retrieve students enrolled between '2022-01-01' and '2023-01-01'.
- Retrieve students with a score between 70 and 90.
- Retrieve students with an ID between 10 and 20.
- Retrieve students who joined between two specific dates.
- Retrieve students whose ages are NOT between 12 and 16.
- Retrieve students whose fees_paid is between 5000 and 10000.
- Retrieve students whose roll numbers are between 100 and 200.
- Retrieve students who joined in a particular month and year.
🔹 <, >, OR, AND Operators – 20 Questions
- Retrieve students whose age is greater than 15.
- Retrieve students whose age is less than 12.
- Retrieve students whose fees_paid is greater than 10000.
- Retrieve students whose marks are less than 35 (failed students).
- Retrieve students whose marks are greater than 80 and age is less than 18.
- Retrieve students whose marks are greater than 90 or age is less than 14.
- Retrieve students whose marks are between 60 and 80 and age is more than 15.
- Retrieve students whose age is less than 10 or greater than 18.
- Retrieve students who are in class_id = 5 and have marks greater than 85.
- Retrieve students in class_id = 7 or class_id = 9.
- Retrieve students whose fees are more than 5000 but less than 10000.
- Retrieve students whose marks are greater than 75 and fees are more than 8000.
- Retrieve students who are either in Class 1 or Class 2.
- Retrieve students who are NOT in Class 4.
- Retrieve students whose name starts with "A" and marks are greater than 90.
- Retrieve students whose marks are less than 50 and fees are more than 7000.
- Retrieve students who are younger than 12 or older than 17.
- Retrieve students who belong to Class 3 but do not have any pending fees.
- Retrieve students who either scored full marks or failed completely.
- Retrieve students whose roll numbers are even and greater than 50.
0 Comments