Xudu
Xudu
1. What is MySQL?
Ans MySQL is a Relational Database Management System.
2. 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) ;
Ans INSERT INTO Emp(Lastname) VALUES (‘Sharma’) ;
3. Mrs. Sen entered the following SQL statement to display all Salespersons of the cities "Chennai"
and ‘Mumbai’ from the table ‘Sales’.
Table :Sales
SCode Name City
101 Aakriti Mumbai
102 Aman Punjab
103 Banit Delhi
104 Fauzia Mumbai
SELECT * FROM Sales
WHERE City='Chennai'
AND City='Mumbai';
Rewrite the correct statement if wrong or write statement is correct.
Ans SELECT * FROM Sales
WHERE City='Chennai'
OR City='Mumbai';
OR
SELECT * FROM Sales
WHERE City='Chennai'|| City='Mumbai'; OR
SELECT * FROM Sales
WHERE City IN ('Chennai', 'Mumbai');
6. In a table ‘Employee’, a column ‘Occupation’ contains many duplicate values. Which keyword
would you use if you wish to list only different values?
Ans The DISTINCT keyword can be used to return only distinct (different) values.
8. Charvi wants to delete the records where the "FirstName" is "Rama" in the ‘Emp’ Table. She has
entered the following SQL statement. An error is being displayed. Rewrite the correct statement.
DELETE ‘Rama’ FirstName FROM Emp;
Ans DELETE FROM Emp WHERE FirstName = 'Rama';
ID Salary
101 43000
102 NULL
104 56000
107 NULL
To select tuples with some salary ,Siddharth has written the following erroneous SQL
statement:
SELECT ID, Salary FROM empsalary WHERE Salary = something;
Write the correct SQL statement.
Ans: SELECT ID, Salary FROM empsalary WHERE Salary is NOT NULL;
11. Consider the table ‘Employee’.
Employee
Name Location
Gurpreet Mumbai
Jatinder Chennai
Deepa Mumbai
Harsh Chennai
Simi New Delhi
Anita Bengaluru
Location
Mumbai
Chennai
New delhi
Bangaluru
Ans: SELECT DISTINCT Location FROM Employee;
12. While creating the table Student last week, Ms. Sharma forgot to include the column
Game_Played. Now write a command to insert the Game_Played column with VARCHAR
data type and 30 size into the Student table?
Ans: Alter Table Student Add (Game_Played VARCHAR(30));
13. 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.
Ans: UPDATE command
14. Ms Sneha is working in a software company. She works under MySQL database. She forgot the
name of the tables which she worked in last week. What MySQL statement she should use to
know the tables?
Ans. SHOW TABLES;
15. A numeric data field CNT contains 35675.8765. Write a command to round of CNT to
(i) Whole number.
(ii) Upto 3 decimal places.
Ans. (i) SELECT ROUND(CNT, 0);
(ii) SELECT ROUND(CNT, 3);
16. Ms. Ankita enter the following SQL statement to display all the department of “MUMBAI” and
“KOLKATTA” from the table DEPT.
TABLE: DEPT
DCODE DEPARTMENT CITY
D01 MEDIA DELHI
D02 MARKETING DELHI
D03 INFRASTRUCTURE MUMBAI
D05 FINANCE KOLKATTA
D04 HUMAN RESOURCE MUMBAI
SELECT * FROM DEPT
WHERE CITY=’MUMBAI’ AND CITY=’KOLKATTA’;
Rewrite the statement, if it is incorrect or else write the word ‘CORRECT’.
Ans. The correct statement is:
SELECT * FROM DEPT
WHERE (City=’MUMBAI’ OR City=’KOLKATTA’);
17. Mr. Satish wants to remove a column AC_Type from Account table. What MySQL command
should he use?
Ans. The MySQL command is as follow:
ALTER TABLE Account DROP COLUMN AC_Type;
18. A table APS has 4 rows and 3 columns and another table ORDER has 3 rows and 4 columns. How
many rows and columns will be there if we obtain the Cartesian product of these two tables?
Ans. 12 rows and 7 columns.
19. Mr. Sanjeev is working as a database administrator in a company and wants to change his
database DEPT table of ‘MARKETING’ department to ‘SALES’. He entered the following SQL
statement and found some error.
TABLE: DEPT
DCODE DEPARTMENT CITY
D01 MEDIA DELHI
D02 MARKETING DELHI
D03 INFRASTRUCTURE MUMBAI
D05 FINANCE KOLKATTA
D04 HUMAN MUMBAI
RESOURCE
UPDATE TABLE DEPT WITH DEPARTMENT = ‘SALES’
Rewrite the statement, if it is incorrect or else write the word ‘CORRECT’.
Ans. The correct statement is:
UPDATE DEPT SET DEPARTMENT = ‘SALES’
WHERE DEPARTMENT = ‘MARKETING’;
2 Marks Questions/Answers
Mr. Vinay wanted to display average salary of each Category. He entered the following SQL
statement. Identify error(s) and Rewrite the correct SQL statement.
SELECT Category, Salary
FROM Hotel
GROUP BY Category;
3. Anita has created the following table with the name ‘Order’.
Table: Order
Column Name Constraint
OrderId Primary Key
Order Date Not Null
Order Amount
StoreId
One of the rows inserted as a follows:
OrderId OrderDate OrderAmount StoreId
0101 2015-02-12 34000 S104
i. What is the data type of columns OrderId and OrderDate in the table Order ?
Ans Data type of OrderId : varchar/char
Data type of OrderDate : date
7. Srishti has created the following table with the name ‘Veterinary’.
(i) What are the data type of columns AnimalId and VacinnationDate in the table Veterinary ?
Ans Data type of AnimalId : Varchar/char
Data type of VaccinationDate : Date
(ii) Srishti is now trying to insert the following row
AnimalId VacinnationDate AnimalName OwnerName
A102 2015‐08‐09 NULL Abhimanyu Shah Will she be able to successfully insert it? Give reason.
Ans No
Reason – Not Null Constraint applied on attribute AnimalName
8. Write the output of the following SQL queries:
(i) SELECT MID('LearningIsFun',2,4);
Ans MID('LearningIsFun',2,4)
Earn
9. Write SQL query to create a table ‘Song’ with the following structure:
(ii) ‘P101’ data is present twice in column ‘PartyId’ in ‘Client’ table – Is there any discrepancy?
Give reason for your answer.
Ans There is no discrepancy. PartyId is not the Primary key in table Client, hence repetition is
permissible.
14. 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.
Ans: i. a) RollNo b) Name
ii.Primary Key: RollNo as it will be unique for each student of the class.
16. Write SQL query to create a table ‘Player’ with the following structure:
Field Type Constraint
playerid Integer Primary key
name Varchar(50)
height Integer
weight Integer
datebirth Date
teamname Varchar(50)
21. What will be the output of the following queries on the basis of SALESITEM table:
Item No Item Name Item Amount
IT001 Tata Steel 255600
IT002 Jindal Steel NULL
IT003 Mittal Steel 655000
(i) SELECT AVG(ItemAmount) FROM SALESITEM;
(ii) SELECT ItemAmount+100000 FROM SALESITEM WHERE ItemNo = ‘IT002’;
Ans. (i) 455300
(ii) NULL
24. Write the SQL command to create the table EMPLOYEE including its constraints.
TABLE: EMPLOYEE
Name of Column Type Constraint
ID INT(4) PRIMARY KEY
First_Name VARCHAR(15) NOT NULL
Last_Name VARCHAR(15) NOT NULL
Email_ID VARCHAR(15) NOT NULL
Salary INT(10) DEFAULT 1000
Ans. The command is as follows:
CREATE TABLE EMPLOYEE
(ID INT(4) PRIMARY KEY,
First_Name VARCHAR(15) NOT NULL
Last_Name VARCHAR(15) NOT NULL
Email_ID VARCHAR(15) NOT NULL
Salary INT(10) DEFAULT 1000);
25. In a database there are two tables ‘Display’ and ‘Model’ as shown below:
Table: Display
DispID DispName DispHO ContPerson
1 Tital Okhla C.B.Ajit
2 Maxima Shahdra V.P.Kohli
3 Ajanta Najafgarh R.Mehta
Table: Model
ModelNo DispID ModelCost
T020 1 2000
M032 4 2500
M059 2 7000
A167 3 800
T024 1 1200
(i) Identify the foreign key column in the table model.
(ii) Check every value in DispID column of both the tables. So you find any discrepancy?
(b) (i) DispID
(ii) Yes, in the 2nd row of model table the value of DispID is 4. This DispID does not exist in the
table display.
26. What is Primary Key in a table? Give an Example.
Ans. Primary Key. It is that column/field which uniquely identities each records of the table. Primary key
field of a table does not contain null values.
For example, consider the table student with the fields (Adm_no, Name, Class, Sec) in this Table
Adm_no will be unique for every, so it can serve as a primary key.
32. Write the SQL command to create the table voter including is constraints.
Table: VOTER
Column Name Data Type Size Constraint Description
V_ID INT 8 Primary key Voter identification
Vname VARCHAR 25 NOT NULL Name of the voter
Age INT 3 CHECK > 17 Age should not less than equal to 17
Address VARCHAR 30 Address of voter
Phone VARCHAR 10 Phone number of the voter
Ans. The command is as follows:
CREATE TABLE VOTER (V_ID INT(8) Primary key,
Vname VARCHAR(25) NOT NULL,
Age INT(3) CHECK Age > 17,
Address VARCHAR(30),
Phone VARCHAR(10));
33. In a database there are two tables ‘Client’ and “Bill’ as shown below:
Table: Client
ClientID ClientName ClientAddress ClientPhone
1 Akhilesh Narang C4, Janak Puri, Delhi 9811078987
2 Purnima Williams B1, Ashok Vihar, Delhi 9678678711
3 Sumedha Madaan 33, South Ext., Delhi 6767655412
Table: Bill
BillNo ClientID Bill_Amt
1 2 12000
2 1 15000
3 2 13000
4 3 13000
5 2 14000
(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?
Ans. (i) 15 rows and 7 columns (ii) ClientID
6 Marks Questions/Answers
1. Write commands in SQL for (i) to (iv) and output for (v) and (vi).
Table: Store
(i) To display name, location, city, SalesAmount of stores in descending order of SalesAmount.
Ans SELECT Name,Location,City, SaleAmount
FROM Store
ORDER BY SaleAmount desc;
(ii) To display names of stores along with SalesAmount of those stores
that have ‘fashion’ anywhere in their store names.
Ans SELECT Name,SalesAmount
FROM Store
WHERE Name like ‘%fashion%’;
(iii) To display Stores names, Location and Date Opened of stores that were opened before
1 st March 2015.
Ans SELECT Name,Location,DateOpened
FROM Store
WHERE DateOpened <’03/01/2015’;
OR
SELECT Name,Location,DateOpened
FROM Store
WHERE DateOpened <03012015;
(iv) To display total SalesAmount of each city along with city name.
Ans SELECT SUM(SalesAmount),City
FROM Store
GROUP BY City;
(i) Which column is used to relate the two tables?
Ans TeacherId column is used to connect the two tables.
(ii) Is it possible to have a primary key and a foreign key both in one table? Justify your answer with
the help of table given above.
Ans Yes. As in the Table:Course, CourseId can be used as Primary key and
TeacherId is the foreign key.
2. With reference to the above given tables, write commands in SQL for
(i) and (ii) and output for (iii) :
(i) To display CourseId,TeacherId, Name of Teacher, Phone Number of Teachers living in Delhi.
Ans SELECT CourseId, TeacherId, Name, PhoneNumber
FROM Course, Faculty
WHERE Course.TeacherId = Faculty.TeacherId
AND city = ‘Delhi’;
(ii) To display TeacherID, Names of Teachers, Subjects of all teachers with names of Teachers
starting with ‘S’.
Ans SELECT TeacherId,Name,Subject FROM Faculty, Course WHERE Faculty.TeacherId = Course.
TeacherId AND Name like ‘S%;;
(iii) SELECT CourseId, Subject, TeacherId, Name, PhoneNumber FROM Faculty, Course WHERE
Faculty.TeacherId = Course.TeacherId AND Fee >=5000;
Ans
CourseId Subject TeacherId Name PhoneNumber
C103 Physics T101 Savita Sharma 991019564
C105 Advance T104 Simi Arora 658777564
computer science
3. Table “Order” is shown below. Write commands in SQL for (i) to (iv) and output for (v) and (vi)
Table: Order
OrderId OrderDate SalesPerson OrderAmount
0101 2015-09-12 Ravi Kumar 34000
0102 2015-08-15 Rashmi Arora 50000
0103 2015-11-01 Ravi Kumar 55000
0104 2015-12-09 Manjeet Singh 60000
0105 2015-11-10 Rashmi Arora 50000
(ii) To list Orderid and respective Order amount in descending order of order amount.
Ans SELECT OrderId,OrderAmount
FROM Order
ORDER BY OrderAmount desc;
(iii) To count the number of orders booked by Salespersons with names starting with ‘R’
Ans SELECT COUNT(*)
FROM Order
WHERE SalesPerson LIKE “R%”;
(iv) To list Order ids, order dates and order amounts that were booked after 1 st September 2015.
Ans SELECT OrderId,OrderDate,OrderAmount
FROM Order
WHERE OrderDate >’20150901’;
(i) To display Client names of clients, their phone numbers,PartyId and party description who will
have
number of guests more than 50 for their parties.
Ans SELECT C.CLIENTNAME, C.PHONE, P.PARTYID,
P.DESCRIPTION
FROM PARTY P, CLIENT C
WHERE P.PARTYID = C.PARTYID
AND C.NOOFGUESTS > 50;
(ii) To display Client Ids, their addresses, number of guests of those clients who have ‘Adarsh’
anywhere in their addresses.
ClientId ClientName Address Phone NoOfGuests PartyId
Ans SELECT CLIENTID, ADDRESS, NOOFGUESTS
FROM CLIENT
WHERE ADDRESS LIKE ‘%Adarsh%’;
iii. To count the number of employees with names starting with ‘K’
iv. To list names and addresses of those persons who have ‘Delhi’ in their address.
v. SELECT Name, Salary FROM Emp where salary between 50000 and 70000;
vi. SELECT Name, phone from emp where phone like ‘99%’;
6. With reference to the above given tables (in Q5), Write commands in SQL for (i) and (ii) and
output for (iii) below:
i.) To display SalespersonID, names, orderids and order amount of all salespersons.
Ans: SELECT S.SalespersonID, Name, OrderID, Amount FROM Salesperson S, Orders O WHERE
S.SalespersonID= O.SalespersonID;
ii.) To display names ,salespersons ids and order ids of those sales persons whose names start
with ‘A’ and sales amount is between 15000 and 20000.
Ans. SELECT Name,S.SalespersonID,OrderID FROM Salesperson S, Orders O WHERE S.
SalespersonID=O.SalespersonID AND Name LIKE “A%” AND Amount BETWEEN 15000
AND 20000;
iii.) SELECT SalespersonId, name, age, amount FROM Salesperson, orders WHERE
Salesperson.salespersonId= Orders.salespersonId AND AGE BETWEEN 30 AND 45;
Ans. 2 Sunil 34 54000
5 Chris 34 24000
7. Table ‘PAINT’ is shown below. Write commands in SQL for (i) to (iv) and output for (v) to (vi).
TABLE: PAINT
Name Code Category Title Status Price Year
G. Hussain 2098 Water Demons Sold 70000 1980
J. Juneja 3099 Oil Twilight Not Sold 8000 1990
Y.D. Sharma 8001 College Masses Sold 9500 1968
A.D’SOUZA 7901 Oil Tresses Sold 13000 1977
Nevill 5400 Water Holiday Not Sold 8900 1977
A.Dasgupta 3400 Oil Kites Not Sold 9000 1982
S.Rohtagi 2100 Oil Ruins Sold 18000 1981
P.Arora 3100 Water Castle NotSold 20000 1965
Col Singhvi 2211 Water Valley Sold 7000 1962
(i) Display the complete list of all the paintings that belong to category Water.
(ii) Display only the most expensive painting in each category.
(iii) Display the name of the painter, title of painting and price in desending order of price.
(iv) Display the list of all the paintings made before the year 1970.
(v) Select AVG(Price) from Paint where price <8500;
(vi) Select COUNT(DISTINCT (CATEGORY)) from Paint;
Ans. (i)SELECT * FROM paint WHERE category = ‘Water;
(ii)SELECT MAX(price) FROM paint group by category;
(iii) SELECT name, title, price FROM paint ORDER BY price DESC;
(iv) SELECT * FROM paint WHERE YEAR < 1970;
(v) 7500.00
(vi) 3
8. with reference to the two tables given below, write commands in SQL for (i) and (ii) and output for
(iii):
Table: CUSTOMER
Booking_Code Customer_Name No_of_tkts BClerk_Code
B001 Veer 4 BC003
B002 Milan 2 BC004
B003 Jahmu 3 BC003
B004 Michal 20 BC001
B005 Meera 5 BC001
Table: BCLERK
BClerk_Code Name
BC001 Varsha
BC002
Answer the following questions based on the above tables:
(i) Write a query to display the total number of tickets booked by booking Clerk ‘PAYAL’.
(ii) Write a query to display the name of the clerk who sold maximum number of tickets.
(iii) Select lowe(b.name), lower(c.Customer_Name) from customer c, bclerk b
Where c.bclerk_code = b.bclek_code;
Ans. (i) Select count(c.No_of_tkts) from customer c, bclerk b
Where c.bclerk_code = b.clerk_code and b.name = ‘PAYAL’;
(ii) Select b.name, max(No_of_tkts) from customer c, bclerk b
Where c.bclerk_code = b.bclerk_code;
(iii) lower(b.name) lower(c.Customer_Name)
varsha michal
varsha meera
vineet veer
vineet jahmu
payal Milan
9. Write commands in SQL for (i) to (iv) and output for (v) to (vi).
TABLE: STUDENT
No Name Stipend Stream Avgmark Grade Class
1 Karan 450.00 Medical 78.5 B 12B
2 Divakar 450.00 Commerce 89.2 A 11C
3 Divya 300.00 Commerce 68.6 C 12C
4 Arun 350.00 Humanities 73.1 B 12D
5 Sabina 500.00 Nonmedical 90.6 A 11A
6 John 400.00 Medical 75.4 B 12B
7 Robert 250.00 Humanities 64.4 C 11A
8 Rubina 450.00 Nonmedical 88.5 A 12A
9 Vikas 500.00 Nonmedical 92.0 A 12A
10 Mohan 300.00 Commerce 67.5 C 12C
(i) Select all the Nonmedical stream student from STUDENT.
(ii) List the names of those students who are in class 12 sorted by Stipend.
(iii) List all student sorted by AvgMrk in descending order.
(iv) Display a record, listing Name, Stipend, Stream and amount of stipend received in a year a
assuming that the Stipend is paid every month.
(v) Select SUM(Stipend) from STUDENT where Grade = ‘B’;
(vi) Select Name, LENGTH(Name) from Students where Grade = ‘C’;
10. With reference to the two tables given below, write commands in SQL for (i) and (ii) and output for
(iii):
Table: CUSTOMER_RECORD
C_CODE NAME ADDRESS CITY PHONE
C001 George B-31-32 N V Delhi 6199408
C002 Ashok H-61 Arawali New Delhi 6129400
C003 Raman WZ-41 DBG Road Delhi 6133888
C004 James Q-31 Raj Pura New Delhi 6144218
C005 Patrick L-21 Vasant Vihar New Delhi 6111228
Table: ORDER_RECORD
O_CODE C_CODE ORD_DATE ITEMNAME ORDQTY AMOUNT
A001 C003 2016-03-22 BURGER 3 60.00
A002 C001 2016-03-24 PIZZA 2 300.00
A003 C005 2016-04-25 BURGER 5 100.00
A004 C004 2016-03-27 BURGER 5 100.00
A005 C001 2016-04-09 HOT-DOG 2 40.00
Answer the following:
(i) Write a query to display the names of the customers along with their order code (O_CODE)
who lives in NEW DELHI.
(ii) Write a query to display name of the customer who has to paid maximum amount.
(iii) select C.Name, C.ADDRESS O. ITEMNAME from CUSTOMER_RECORD C,
ORDER_DETAIL O. WHERE O. C_CODE AND O. AMOUNT > 200;
Ans. (i) select C.Name, O.O_CODE from CUSTOMER_RECORD C, ORDER_DETAIL O
WHERE O.C_CODE = C.C_CODE AND C.CITY = ‘New Delhi’;
(ii) select C.NAME, MAX(O.AMOUNT) from CUSTOMER_RECORD C, ORDER_DETAIL O
WHERE O.C_CODE = C.C_CODE;
(iii) NAME ADDRESS ITEMNAME
George B-31-32 N V PIZZA