0% found this document useful (0 votes)
522 views22 pages

Xudu

This document contains questions and answers related to SQL and database concepts. It includes 17 multiple choice questions covering topics like SQL statements, database tables, functions, and aggregate functions. The questions test concepts like SELECT, INSERT, UPDATE, DELETE, ALTER TABLE, JOIN, DISTINCT, GROUP BY and aggregate functions.

Uploaded by

Hercules Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
522 views22 pages

Xudu

This document contains questions and answers related to SQL and database concepts. It includes 17 multiple choice questions covering topics like SQL statements, database tables, functions, and aggregate functions. The questions test concepts like SELECT, INSERT, UPDATE, DELETE, ALTER TABLE, JOIN, DISTINCT, GROUP BY and aggregate functions.

Uploaded by

Hercules Arun
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

1 Mark Questions/Answers

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’) ;

2. Kunal created the following table with the name ‘Friends’:


FriendCode Nsme Hobbies
F101 Bijoy Swimming
F102 Abhinav Reading books
F103 Jyotsna Dancing
Now, Kunal wants to delete the ‘Hobbies’ column. Write the MySQL statement.
Ans ALTER TABLE Friends DROP Hobbies; OR
ALTER TABLE Friends DROP COLUMN Hobbies;

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');

4. ( i) Name 2 Aggregate (Group) functions of SQL. 2


SUM(), MAX(), MIN(), AVG() ,COUNT(), COUNT(*) (Any Two)
(ii) Consider the table:
Table: Company
SID SALES
S101 20000
S103 NULL
S104 10000
S105 15000
What output will be displayed by the following SQL statement?
SELECT AVG(SALES) FROM Company;
Ans 15000
5. What is the meaning of ‘Open source’ in the term ‘Open Source Database Management System’
Ans Open source means that the software can be studied, copied, redistributed freely and even
modified according to one's need without seeking any kind of permission. In order to modify such
software the developers also provide the source code to the users.

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.

7. How is ALTER TABLE statement different from UPDATE statement?


Ans
ALTER TABLE UPDATE
To modify structure of a table To modify data in a table
DDL command DML command

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';

9. Name 2 Group (Aggregate) functions of SQL.


Ans SUM(), MAX(), MIN(), AVG(), COUNT(), COUNT(*)

10. Consider the table:


Table: Company
CompanyCode Donations
C101 13000
C102 NULL
C10 7000
C105 4000
What output will be displayed by the following SQL statement:
SELECT AVG(Donations) FROM Company
Ans 8000

10. Consider the table ‘empsalary’.

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

Write the SQL command to obtain the following output :

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

1. Given below is the ‘Stu’ table:


RNO NAME
1 Amit
2 Bhishm
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;
Ans.
RNO NAME
1 Amit
2 Bhishm
5 Rahuliya
6 Cristina

2. Consider the table ‘Hotel’ given below.


Table : Hotel
EMPID Category Salary
E101 MANAGER 60000
E102 EXECUTIVE 65000
E103 CLERK 40000
E104 MANAGER 62000
105 EXECUTIVE 50000
E106 CLERK 35000

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;

Ans. SELECT Category, AVG(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

ii. Anita is now trying to insert the following row:


OrderId OrderDate OrderAmount StoreId
0102 NULL 59000 S105
Will she be able to successfully insert it? Give reason.
Ans (ii) No
Reason – Not Null Constraint applied on attribute AnimalName

4. Write the output of the following SQL queries:

(i) SELECT MID(‘BoardExamination’, 2, 4);


Ans oard

(ii) SELECT ROUND(67.246, 2);


Ans 67_25

(iii) SELECT INSTR(‘INFORMATIONFORM’, ‘FOR’);


Ans 3

(iv) SELECT DAYOFYEAR(‘20150110’);


Ans 10

5. Given below is the ‘Emp’ table:


ENO NAME
1 Anita Khanna
2 Bishmeet Singh
SET AUTOCOMMIT = 0;
INSERT INTO Emp VALUES(5,'Farzia');
COMMIT;
UPDATE Emp SET NAME ='Farzziya' WHERE Eno= 5;
SAVEPOINT A;
INSERT INTO Emp VALUES(6,'Richard');
SAVEPOINT B;
INSERT INTO Emp VALUES(7,'Rajyalakshmi');
SAVEPOINT C;
ROLLBACK TO B;
What will be the output of the following SQL query now:
SELECT * FROM Emp;
Ans.
ENO NAME
1 Anita Khanna
2 Bishmeet Singh
5 Farzziya
6 Richard

6. Consider the table below.


Table: Company

EMPID DEPARTMENT SALARY


E101 PERSONNEL 60000
E102 ACCOUNTS 65000
E103 MARKETING 40000
E104 PERSONNEL 62000
E105 PERSONNEL 50000
E106 MARKETING 350000
Identify error(s) in the following SQL statement. Rewrite the correct
SQL statement.
SELECT Department, Salary
FROM Company
GROUP BY Department;

Ans. Some aggregate function like AVG(),SUM(), MAX(), MIN() etc.


should be mentioned with SELECT .
SELECT DEPARTMENT, AVG(SALARY)
FROM COMPANY
GROUP BY DEPARTMENT;

7. Srishti has created the following table with the name ‘Veterinary’.

Column Name Constraint


AnimalId Primary Key
VacinantionsDate
AnimalName Not Null
OwnerName

One of the rows inserted is as follows:


AnimalId VacinnationDate AnimalName OwnerName
A101 2015-02-12 Sheru Amit Sharma

(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

(ii) SELECT ROUND(76.384,2);


Ans ROUND(76.384,2)
76.38

(iii) SELECT INSTR('INFORMATION FORM','RM');


Ans INSTR('INFORMATION FORM','RM')
5

(iv) SELECT DAYOFYEAR('20150130');


Ans DAYOFYEAR('20150130')
30

9. Write SQL query to create a table ‘Song’ with the following structure:

Field Type Constraint


Songid Integer Primary key
Title Varchar(50
Duration Integer
ReleaseDate Date
Ans CREATE TABLE Song
(
SongId Integer PRIMARY KEY,
Title Varchar(50),
Duration Integer,
ReleaseDate Date

10. Consider the tables given below.


Table: Party
PartyId Description Cost Per Person
P101 Birthday 400
P102 Wedding 700
P103 Farewell 350
P104 Engagement 450
(i) Name the Primary keys in both the tables
Ans Primary key (Table : Party ) ‐ PartyId
Primary key (Table : Client) ‐ ClientId

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

11. Consider the table ‘Teacher’ given below.


TeacherId Department Periods
T101 Science 32
T102 Null 30
T103 Mathematics 34
What will be the output of the following queries on the basis of the above table:
(i)Select count(Department) from Teacher;
(ii)Select count(*) from Teacher;
Ans: i. 2 ii. 3

12. Consider the Stu table


Roll No. Name
1 Ashi
2 Bimmi
4 Akash

The following SQL queries are executed on the above table


INSERT INTO Stu VALUES(5,'Gagan');
COMMIT;
UPDATE Stu SET name='Abhi' WHERE Rollno = 4;
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;
Ans: Output:
1 Ashi
2 Bimmi
4 Abhi
5 Gagan
6 Chris
13. An attribute A of datatype varchar(20) has the value “Amit” . The attribute B of
datatype char(20) has value ”Karanita” . How many characters are occupied in
attribute A ? How many characters are occupied in attribute B?
Ans: 4, 20

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.

15. Write the output of the following SQL queries:


i) SELECT TRUNCATE(8.975,2);
ii) SELECT MID(‘HONESTY WINS’,3,4);
iii) SELECT RIGHT(CONCAT(‘PRACTICES’,’INFORMATICS’),5);
iv) SELECT DAYOFMONTH(‘2015-01-16’);
Ans :i. 8.97
ii. NEST
iii. ATICS
iv. 16

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)

Ans: CREATE TABLE Player (


playerID integer PRIMARY KEY,
name varchar(50),
height integer,
weight integer,
datebirth date,
teamname varchar(50) );
17. Consider the table given below
Salesperson
SalespersonId Name Age Salary
1 Ajay 61 140000
2 Sunil 34 44000
3 Chris 34 40000
4 Amaaya 41 52000
Orders
OrderId SalespersonId Amount
10 2 54000
20 7 18000
30 1 46000
40 5 24000
i. The SalespersonId column in the "Salesperson" table is the _________ KEY.The SalespersonId
column in the "Orders" table is a ___________ KEY.
ii. Can the ‘SalespersonId’ be set as the primary key in table ‘Orders’. Give reason.

Ans: i. Primary, Foreign

ii. No as it may be repeated in Orders table.

18. What is meant by Cardinality of a table? Give an example.


Ans. The number of tuples (rows) in the relation is called its Cardinality. For example, consider the
table:
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
There are total 3 records in a table. So, the cardinality of the above table is 3.

19. Differentiate between CREATE TABLE and ALTER TABLE commands.


Ans. CREATE TABLE command is used to create table a new table on the database. With this command,
you can specify a name, type, precision and scale for each field of the table. The syntax is as follows:
CREATE TABLE <create table>
(column_name data_type(size),
……………………………………..,
……………………………………..,
……………………………………..);
For example,
CREATE TABLE student
(roll_no NUMBER(4) NOT NULL,
name VARCHAR2(20),
d_o_b DATE,
fees NUMBER(8,2),
grade CHAR(1));
ALTER TABLE command is used to change the structure of the table. We can add new column,
change the data type of column or drop any constraint using alter command. Adding a column is
straight forward and similar to creating a table. The syntax is as follows:
ALTER TABLE table_name
ADD/MODIFY/DROP(column_name data_type(size));
For example,
ALTER TABLE student
ADD (ADDRESS VARCHAR2(30));

20. Name the commands/clauses for the following:


(i) To create the table.
(ii) To change the structure of the table.
Ans. (i) CREATE TABLE
(ii) ALTER TABLE

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

22. Differentiate between COMMIT and ROLLBACK.


Ans. COMMIT. The commit statement is used to end a transaction and make all changes permanent.
Until a transaction is committed, other users cannot see the changes made to the database by the
transaction. COMMIT also releases any locks acquired by the transaction. The syntax is as
follows:
COMMIT [WORK];
Where WORK keyword is optional.
ROLLBACK. The ROLLBACK statement is used to end a transaction and undo the work done by
that transaction. It is as if the transaction had never begun. The syntax is as follows:
ROLLBACK [WORK];
Where work keyword is optional, Like COMMIT, ROLLBACK also release any lock acquired by the
transaction.

23. Write the resulting output of the following:


(i) SELECT 1000 + SQRT(100);
(ii) SELECT TRIM(‘ ABS Public School ‘);
(iii) SELECT FLOOR(100.34) + ABS(200.43);
(iv) SELECT LOWER(‘abs Public School’);
Ans. (i) 1010
(ii) ABS Public School
(iii) 300.43
(iv) abs public school

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.

27. Differentiate between SQL command INSERT and ALTER.


Ans. The INSERT command adds a new record to the end of an existing database. The syntax is as:
[(<column <table_name>
[(<column name1>[, <column name2>[, …]])]
VALUES (<expr1>[, <expr2>[, …]]);
For example,
INSERT INTO student
VALUES (100, ‘Ram Prakash’, ‘2004-2003-10’, ‘A’, ‘F-10, Saket Kunj, New Delhi’);
The alter command is used to change the table. We can add a new column, change the data type
of a column or drop any constraint using alter command. Adding a column is straight forward and
similar to creating a table. The syntax is as follows:
ALTER TABLE table_name
ADD/MODIFY/DROP(<column_name> <data_type> <size>);
For example,
ALTER TABLE student
ADD (ADDRESS VARCHAR(30));

28. Name the commands/clause:


(i) To describe the structure of the table.
(ii) To delete the table physically.
(iii) To view record in specified range.
(iv) To view record in specified list.
Ans. (i) DESC (ii) DROP TABLE (iii) BETWEEN (iv) IN

29. A table name MITEM has the following contents:


ICODE INAME IPRICE
101 CHAIR 1500.00
102 DINNING TABLE 24000.00
Write the output that will be displayed by SELECT statement as the SQL statement given below are
executed.
SET AUTOCOMMIT = 0;
INSERT INTO MITEM VALUES(103;’COFFEE TABLE’,340);
ROLLBACK;
START TRANSACTION;
UPDATE MITEM SET IPRICE = IPRICE +200;
SAVEPOINT S1;
UPDATE MITEM SET IPRICE = IPRICE +400;
ROLLBACK TO S1;
SELECT * FROM MITEM;
Ans. The output is:
ICODE INAME IPRICE
101 CHAIR 1700.00
102 DINNING TABLE 24200.00
30. Differentiate between GRANT and REVOKE.
Ans. The GRANT command is used to give permission to one user to access the table of another user or
do any other desired operation according to the privileges assigned.
The REVOKE statement is used to remove the privileges granted by the GRANT statement.

31. Write the resulting output of the following:


(i) SELECT ROUND(1023,432,1);
(ii) SELECT LENGTH(‘RAMESH SHARMA’);
(iii) SELECT UPPER(‘master’);
(iv) SELECT MOD(ROUND(120, 60, 1), 5);
Ans. (i) 1023.4 (ii) 13 (ii) MASTR (iv) 0.6

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

StoreId Name Location City No. of Date Sales


Employees opened Amount
S101 Planet KarolBagh Delhi 7 2015-10- 300000
fashion 16
S102 Trends Nehru Mumbai 11 2015-08- 400000
Nagar 09
S103 Vogue Vikash Delhi 10 2015-06- 200000
Vihar 27
S104 Super Defence Delhi 8 2015-02- 450000
fashion Colony 18
S105 Rage Bandra Mumbai 5 2015-09- 600000
22

(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

(i) To display names of Salespersons (without duplicates).


Ans SELECT DISTINCT SalesPerson
FROM Order;

(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’;

(v) SELECT OrderId, OrderAmount FROM Order where


OrderAmount between 50000 and 60000;
Ans.
OrderId OrderAmount
0102 50000
0103 55000
0104 60000
0105 50000

(vi) SELECT concat(OrderId,SalesPerson),


length(SalesPerson) FROM Order;
Ans.
concat(OrderId, Salesperson) Length (Salesperson)
0101Ravi Kumar 10
0102Rashmi Arora 12
0103Ravi Kumar 10
0104Manjeet Singh 13
0105Rashmi Arora 12
4. With reference to the above given tables (in Q6 b), Write commands in SQL for (i) and
(ii) and output for (iii) given below:

(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) SELECT ClientId, ClientName, NoOfGuests,


description,Costperperson,FROM Client, Party WHERE
Client.Partyid= Party.Partyid AND NOofGuests BETWEEN
50 AND 100
5. Table “Emp” is shown below. Write commands in SQL for (i) to (iv) and output for (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 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%’;

Ans: MySQL Commands:


i. SELECT * FROM Emp WHERE AGE<25;
ii. SELECT NAME,SALARY FROM Emp ORDER BY SALARY desc;
iii. SELECT COUNT(*) FROM Emp WHERE NAME LIKE “K%”;
iv. SELECT NAME,ADDRESS FROM Emp WHERE ADDRESS LIKE”%Delhi%”;
OUTPUT
v. Siddharth 62000
Karan 65000
vi. Chavi 99113423989
Raunaq 99101393576

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

You might also like