MYSQL Worksheet
MYSQL Worksheet
MYSQL Worksheet
My SQL Worksheet-2
(DDL – Table Related commands excluding Alter table)
1. Write an SQL query to create the table 'Menu' with the following structure:
2. Can a table have multiple primary keys? Can it have multiple foreign keys?
3. In a Student table, out of Roll Number, Name, Address which column can be set as Primary key and
why?
4. Ms. Mirana wants to remove the entire content of a table "BACKUP" alongwith its structure to release
the storage space. What MySql statement should she use ?
5. Write MySql command to create the Table STOCK including its Constraints.
Table STOCK :
6. Write one similarity and one difference between CHAR and VARCHAR data types.
7. Saumya had previously created a table named „Product‟ in a database using MySQL. Later on she
forgot the table structure. Suggest her suitable MySQL command through which she can check the
structure of the already created table.
8. Roli wants to list the names of all the tables in her database named „Gadgets‟. Which command (s)
she should use to get the desired result.
9. Name the SQL commands used to :
(i) Physically delete a table from the database.
(ii) Display the structure of a table.
10. Write one similarity and one difference between UNIQUE and PRIMARY KEY constraints.
11. An attribute A of datatype varchar(20) has the value “Amit” . The attribute B of datatype char(20)
1
has value ”Karanita” . How many characters are occupied in attribute A ? How many characters are
occupied in attribute B?
12. Mrs. Sharma is the classteacher of Class „XII A‟ She wants to create a table „Student‟
to store details of her class.
i) Which of the following can be the attributes of Student table?
a) RollNo b) “Amit” c) Name d) 25
ii) Name the Primary key of the table „Student‟. State reason for choosing it.
13. Write SQL query to create a table „Player‟ with the following structure:
14. Anita has created the following table with the name „Order‟.
(i) What is the data type of columns OrderId and OrderDate in the table Order ?
(ii) Anita is now trying to insert the following row :
2
i. Name the column that might have a Primary Key constraint. Justify your answer.
ii. Name the column that might have a Unique constraint. Justify your answer.
17. “ABC” Event Management Company requires data of events that are to be organized. Write SQL query
to create a table „Event‟ with the following structure :
My SQL Worksheet-3
(DDL – Alter Table commands)
1. Sahil created a table in Mysql. Later on he found that there should have been another column in the
table. Which command should he use to add another column to the table?
2. While creating a table 'Customer' Simrita forgot to set the primary key for the table. Give the
statement which she should write now to set the column 'CustiD' as the primary key of the table?
3. Kuhu has already created a table „Hospital‟ as shown below:
Now she wants to add a new column „Address‟ to the above given table. Suggest suitable MySQL
command for the same.
4. Write SQL command to remove column named „Hobbies‟ from a table named „Student‟.
5. While creating the table Student last week, Ms. Sharma forgot to include the column Game_Played.
3
Now write a command to insert the Game_Played column with VARCHAR data type and 30 size into
the Student table?
6. Kunal created the following table with the name „Friends‟ :
Table : Friends
My SQL Worksheet-4
(DML – INSERT INTO commands)
1. Rama is not able to change a value in a column to NULL. What constraint did she specify when she
created the table?
2. Consider the table RESULT given below.
Add a new row for a new item in GYM with the details: "G107", "Vibro exerciser” ,21000, “GTCFitness"
6. What is meant by NULL value in MySQL?
7. Rewrite the following SQL statement after correcting error(s). Underline the corrections made.
INSERT IN STUDENT(RNO,MARKS) VALUE (5,78.5);
8. Rewrite the following SQL statement after correcting error(s). Underline the corrections made.
INSERT IN EMP(EMPNO, SALES) VALUE (100, 20078.50);
4
9. Charvi is inserting “Sharma” in the “LastName” column of the “Emp” table but an error is being
displayed. Write the correct SQL statement.
INSERT INTO Emp(„Sharma‟)VALUES(LastName) ;
10. Anita has created the following table with the name „Order‟.
(i) What is the data type of columns OrderId and OrderDate in the table Order ?
(ii) Anita is now trying to insert the following row :
Write the command To increase the Price of all the Products by 20.
3. Write the UPDATE command to change “Sharma” to “Singh” in the “LastName” column in the
Employee table.
4. What is the use of UPDATE statement in SQL ? How is it different from ALTER statement?
5. Consider the following table named "GYM"
5
Write command To change the Brandname to "Fit Trend India" of the item, whose ICODE as "G101 ".
6. Write the UPDATE statement in MySQL to increase commission by 100.00 in the „„Commission‟‟ column
in the „Emp‟ table.
7. Write two examples of DML commands of SQL.
8. In a database there are two tables „CD‟ and „TYPE‟ as shown below :
Write SQL statement to change the name of Singer „„Sonvi Kumar‟‟ to „„Sonvi Mehra‟‟ in all the places
wherever it occurs in CD table.
9. Consider the following table named “GARMENT”.
1) Write command To change the colour of garment with code as 116 to “Orange”.
2) Write command to increase the price of all XL garments by 10%
3) Write command to delete the record with GCode “116”
10. In a Database, there are two tables given below :
6
Write SQL command to change the JOBID to 104 of the Employee with ID as E4 in the table
„EMPLOYEE‟.
11. In Marks column of „Student‟ table, for Rollnumber 2, the Class Teacher entered the marks as 45.
However there was a totaling error and the student has got her marks increased by 5. Which MySQL
command should she use to change the marks in „Student‟ table.
12. Chhavi has created a table named Orders, she has been asked to increase the value of a column
named salesamount by 20. She has written the following query for the same.
Alter table Orders Add salesamount =salesamount+20;
Is it the correct query?Justify.
13. Consider the following table:
Table: PharmaDB
My SQL Worksheet-6
(DML – SELECT command)
1. Pooja, a students of class XI, created a table "Book". Price is a column of this table. To find the details
of books whose prices have not been entered she wrote the following query:
Select * from Book where Price = NULL;
3. Consider the table TEACHER given below. Write commands in SQL for (1) to (3) and output for (4)
7
i. To display all information about teachers of PGT category.
ii. To list the names of female teachers of Hindi department.
iii. To list names, departments and date of hiring of all the teachers in ascending order of date of
joining
iv. SELECT DISTINCT(category) FROM teacher;
4. The ltem_No and Cost columna of a table "ITEMS" are given below:
Based on this information, find the output of the following queries:
a) SELECT COST +100 FROM ITEMS WHERE ITEM_NO > 103;
5. Consider the table Projects given below. Write commands in SOL for i) to iii) and output for iv)
7. Sarthya, a student of class XI, created a table "RESULT". Grade is one of the column of this table. To
find the details of students whose Grades have not been entered, he wrote the following MySql query,
which did not give the desired result.
SELECT * FROM Result WHERE Grade= "Null";
Help Sarthya to run the query by removing the errors from the query and write the correct Query.
8. Consider the table RESULT given below. Write commands in MySql for (i) to (ii)
8
9. Mr. Janak is using a table with following columns :
Name , Class , Course_Id, Course_name
He needs to display names of students, who have not been assigned any stream or have been assigned
Course_name that ends with "economics". He wrote the following command, which did not give the
desired result.
SELECT Name, Class FROM Students WHERE Course name = NULL OR Course name="%economics";
Help Mr. J anak to run the query by removing the error and write the correct query.
10. Consider the Table SHOPPE given below. Write
command in MySql for (i) to (ii)
11. What is used in the SELECT clause to return all the columns in the table?
12. In MySQL, Sumit and Fauzia are getting the following outputs of ItemCodes for SELECT statements
used by them on a table named ITEM.(Both have used the SELECT statements on the same table
ITEM).
Sumit‟s Output Fauzia‟s Output
101 101
102 102
101 105
105 107
101
107
Which extra keyword has Fauzia used with SELECT statement to get the above output?
13. Consider the table „PERSONS‟ given below. Write commands in SQL for (i) to (iv) and write output for
(v).
14. Mr. Tondon is using table EMP with the following columns.
ECODE,DEPT,ENAME,SALARY
He wants to display all information of employees (from EMP table) in ascending order of ENAME and
within it in ascending order of DEPT. He wrote the following command, which did not show the
desired output.
SELECT * FROM EMP ORDER BY NAME DESC,DEPT;
Rewrite the above query to get the desired output.
15. Consider the following table named "GYM" with details about fitness items being sold in the store.
Write command of SQL for (i) to (ii).
(i) To display the names of all the items whose name starts
with "A".
(ii) To display ICODEs and INAMEs of all items, whose
Brandname is Reliable or Coscore.
9
16. Consider the following table named 'SBOP" with details of account holders. Write commands of MySql
for (i) to (ii) and output for (iii).
17. When using the LIKE clause, which wildcard symbol represents any sequence of none, one or more
characters ?
18. Consider the table FLIGHT given below. Write commands in SQL for (i) to (iv) and output for (v).
10
22. Consider the table „empsalary‟.
To select tuples with some salary ,Siddharth has written the following erroneous
SQL
statement:
SELECT ID, Salary FROM empsalary WHERE Salary = something;
24. Table “Emp” is shown below. Write commands in SQL for (i) to (iii) and output for (iv) and (v)
and (vi)
i. To display list of all employees below 25 years
old.
ii. To list names and respective salaries in
descending order of salary.
iii. To list names and addresses of those persons
who have „Delhi‟ in their address.
iv. SELECT Name, Salary FROM Emp where salary
between 50000 and 70000;
v. SELECT Name, phone from emp where phone like
„99%‟;
25. Mrs. Sen entered the following SQL statement to display all Salespersons of the cities
“Chennai” and „Mumbai‟ from the table „Sales‟.
Scode Name City SELECT * FROM Sales WHERE
101 Aakriti Mumbai City=„Chennai‟ AND City=„Mumbai‟;
Note : Columns SID and DOB contain Sales Person Id and Data of Birth respectively.
(i) Write the data types of SID and DOB columns.
(ii) Display names of Salespersons and their Salaries who have salaries in the range 30000.00 to
40000.00
(iii) To list Names, Phone numbers and DOB (Date of Birth) of Salespersons who were born before 1st
November, 1992.
(iv) To display Names and Salaries of Salespersons in descending order of salary.
(v) To display areas in which Salespersons are working. Duplicate Areas should not be displayed.
(vi) To display SID, Names along with Salaries increased by 500. (Increase of 500 is only to be
displayed and not to be updated in the table)
(vii) To display Names of Salespersons who have the word „Kumar‟ anywhere in their names.
32. Write the following statement using „OR‟ logical operator :
SELECT first_name, last_name, subject FROM studentdetailsWHERE subject IN („Maths‟, „Science‟);
33. Consider the Table “Gym” shown below. Write commands in SQL for (i) to (vi) :
12
(i) To display Mname, Age, FeeGiven of those members whose fee is above 12,000.
(ii) To display Mcode, Mname, Age of all female members of the Gym with age in descending order.
(iii) To list names of members and their date of admission of those members who joined after 31st
December, 2015.
iv) To display the Mname, FeeGiven of all those members of the Gym whose age is less than 40 and
are monthly type members of the Gym.
(v) To display names of members who have „mit‟ anywhere in their names. For example : Amit, Samit.
(vi) To display types of memberships available. Duplicate values should not be displayed.
34. Consider the following table:
Write commands in SQL for (i) to (iv) and output for
(v):
i. To display the details of all those students
who have IP as their optional subject.
ii. To display name, stream and optional of all
those students whose name starts with „A‟.
iii. To give an increase of 3 in the average of all
those students of humanities section who have
Maths as their optional subject.
iv. To display a name list of all those students
who have average more than 75.
v. Select name from students where optional IN („CS‟,‟IP‟);
My SQL Worksheet-7
(Single Row Functions)
1. Write the output of the following SQL queries:
a) SELECT ROUND(6.5675, 2);
b) SELECT TRUNCATE(5.3456, 1);
c) SELECT DAYOFMONTH('2009-08-25');
d) SELECT MID('Class 12', 2,3);
2. Write the output of the following SQL queries :
(i) SELECT INSTR(„UNICODE‟,‟CO‟);
(ii) SELECT RIGHT(„Informatics‟,3);
3. State difference between date functions NOW( ) and SYSDATE( ) of MySql.
4. Name a function of MySql which is used to remove trailing and leading spaces from a string.
13
5. Consider the following table named 'SBOP" with details of account holders. Write output
14
13. Consider the table given below :
Write output.
My SQL Worksheet-8
(Aggregate Functions)
1. Consider the table TEACHER given below. Write commands in SQL for (1) and output for (2) to (5)
15
3. "PrincipaiName" is a column in a table "Schools". The SOL queries
SELECT count(*) FROM Schools;
and
SELECT count( Principal) FROM schools;
Give the result 28 and 27 respectively. What may be the possible reason for this? How many records are present in
the table-27 or 28?
4. Consider the table Projects given below. Write commands in SOL fori) and output for i) to iii)
6. What is the purpose of ORDER BY clause in MySql ? How is it different from GROUP BY clause?
8. Consider the table „PERSONS‟ given below. Write commands in SQL for (i) to (iv) and write output for (i) to (iii).
9. There is a column HOBBY in a Table CONTACTS. The following two statements are giving different outputs. What
may be the possible reason ?
SELECT COUNT(*) FROM CONTACTS;
SELECT COUNT(HOBBY)FROM CONTACTS;
16
10. Consider the following table named "GYM" with details about fitness
items being sold in the store. Write output
11. Consider the following table named 'SBOP" with details of account
holders. Write output.
14. What will be the output of the following queries on the basis of Employee
table:
17
18. Consider the table „Hotel‟ given below :
Table : Hotel Mr. Vinay wanted to display average salary of
EMPID Category Salary each Category. He entered the following SQL
statement. Identify error(s) and Rewrite the
E101 MANAGER 60000
correct SQL statement.
E102 EXECUTIVE 65000 SELECT Category, Salary FROM Hotel
E103 CLERK 40000 GROUP BY Category;
E104 MANAGER 62000
E105 EXECUTIVE 50000
E106 CLERK 35000
19. Explain why the following queries give different outputs on execution:
i. SELECT COUNT(ENAME) FROM EMP;
Output: 5
ii. SELECT Count(*) FROM EMP;
Output: 8
20. Kunal has entered the following SQL command on Table „STUDENT‟ that has TotalMarks as one of the columns.
SELECT COUNT (*) FROM STUDENT;
The output displayed is 20.
Then, Kunal enters the following command :
SELECT COUNT (*) FROM STUDENT WHERE TotalMarks <100;
The output displayed is 15.
Then, Kunal enters the following command :
SELECT COUNT (*) FROM STUDENT WHERE TotalMarks >= 100;
He predicts the output of the above query as 5. Do you agree with Kunal ? Give reason for your answer.
21. Consider the table given below :
Write command for (i) and output for (ii)
(i) To display Area along with number of Salespersons working in
that area.
(ii) SELECT Area, COUNT (*) FROM Salesperson GROUP BY Area
HAVING COUNT (*) > 1;
My SQL Worksheet-9
(Joins)
1. In a database there are two tables 'Customer' and 'Bill' as shown below:
(i) How many rows and how many columns will be there in the Cartesian product of these two tables?
(ii) Which column in the 'Bill' table is the foreign key?
2. Consider the tables HANDSETS and CUSTOMER given below:
18
With reference to these tables, Write commands in SQL for (i) and (ii) and output for (iii) below:
(i) Display the CustNo, CustAddress and corresponding SetName for each customer.
(ii) Display the Customer Details for each customer who uses a Nokia handset.
(iii) select SetNo, SetName from Handsets, customer where SetNo = SetCode and CustAddress = 'Delhi';
3. In a database there are two tables "Company" and "Model" as shown below:
W1th reference to these tables, wnte commands m SQL for (1) and (II) and output for (iii) below:
(i) Display the PatNo, PatName and corresponding DocName for each patient
(ii) Display the list of all patients whose OPD_Days are MWF.
(iii) select OPD_Days, Count(*) from Doctors, Patients where Patients.Department = Doctors.Department Group
by OPD_Days;
5. In a database there are two tables "Product" and "Client" as shown below :
19
Table ITEM:
(i) Which column can be set as the PRIMARY KEY in the MAGAZINE table?
(ii) Which column in the „MAGAZINE‟ table is the foreign key?
(iii) How many rows and columns will be there in the Cartesian product of the above 2 tables.
(iv) Write command in SQL to display the mag_code, Mag_Title and corresponding types for all the Magazines.
(v) Write the output :
(vi) Select Mag_Code, Mag_Title, Number_of_Pages, Type From MAGAZINE,MAGTYPE Where
Magazine.Mag_Category=Magtype.Mag_Category and Type=‟Spiritual‟;
9. In a Database Kamataka_Sangam there are two tables with the instances given below :
(i) To display Tcode, Name and Aname of all the records where the number of tickets sold is more than 5.
(ii) To display total number of tickets booked by agent “Mr. Ayush”
(iii) To display Acode, Aname and corresponding Tcode where Aname ends with “k”.
20
(iv) With reference to “TicketDetails” table, which column is the primary key ? Which column is the foreign
key? Give reason(s)
11. In a database there are two tables „CD‟ and „TYPE‟ as shown below :
21
names.
(iv) Identify foreign key in the table EMPLOYEE.
17. Consider the tables given below.
Salesperson Orders
Table : Store
22
With reference to the above given tables, write commands in SQL for (i) and (ii) and output for (iii) below:
i. To display the ItemCode,ItemName and ReceivedDate of all the items .
ii. To display SAPID,ItemName,ItemStorageLocation of all the items whose Received date is after 2nd
May 2016.
iii. SELECT SAPID,ItemName,STOREID FROM SAP,Store WHERE SAP.ItemCode=Store.ItemCode AND
StoreLocation = “Hauz Khas”
iv. What will be the degree and cardinality of the cartesian product formed while combining both the
above given tables „SAP‟ and „Store‟ ?
v. Sangeeta is not able to add a new record in the table „Store‟ through the following query:
Insert into store values (1206,1006,‟Karol Bagh‟, „2016/07/25‟);
Identify the error if there is any
My SQL Worksheet-10
(Transaction)
1. Which command is used in MySql to make the changes in database permanent?
2. Give one difference between ROLLBACK and COMMIT commands used in MySql.
Write the output that will be displayed by statements (i) and (ii).
SELECT * FROM GAMES;
SET AUTOCOMMIT = 0;
INSERT INTO GAMES VALUES(105,'CHESS‟,2,9000);
ROLLBACK;
SAVEPOINT S1;
SELECT * FROM GAMES; ------------ (i)
INSERT INTO GAMES VALUES(108,'LAWN TENNIS‟,4,25000);
SAVEPOINT S2;
INSERT INTO GAMES VALUES(109,'CRICKET‟,11,20000);
ROLLBACK TO S2;
SELECT * FROM ITEM; ------------ (ii)
4. Consider the Stu table
SAVEPOINT A;
INSERT INTO Stu VALUES(6,'Chris'); SAVEPOINT B;
INSERT INTO Stu VALUES(7,'Babita'); SAVEPOINT C;
ROLLBACK TO B;
What will be the output of the following SQL query now:
SELECT * FROM Stu;
5. Given below is the „Stu‟ table :
RNO NAME
1 Amit
2 Bhishm
23
The following statements are entered :
SET AUTOCOMMIT = 0;
INSERT INTO Stu VALUES(5, „Rahul‟); COMMIT;
UPDATE Stu set name=„Rahuliya‟ where Rno= 5; SAVEPOINT A;
INSERT INTO Stu VALUES(6, „Cristina‟); SAVEPOINT B;
INSERT INTO Stu VALUES(7, „Fauzia‟); SAVEPOINT C;
ROLLBACK TO B;
Now what will be the output of the following statement ?
SELECT * FROM Stu;
6. Geetanjali had created a table “Customer” in the database “Test”. Immediately after the successful creation
of the database, she wrote the Rollback command to undo the creation of the table. Did she execute rollback
successfully? Explain.
7. Given below is the „Department‟ table :
SET AUTOCOMMIT = 0;
UPDATE Department SET DEPNAME = „OFFICE‟ WHERE DEPNAME =
„ADMIN‟;
INSERT INTO Department VALUES (104, „HRD‟);
UPDATE Department SET DEPNAME = „FRONT OFFICE‟ WHERE
DEPNAME = „RECEPTION‟;
COMMIT;
DELETE FROM Department WHERE DEPNAME = „FRONT OFFICE‟;
ROLLBACK;
SELECT * FROM Department;
What will be the output of the above given SELECT statement ?
24