0% found this document useful (0 votes)
10 views8 pages

Week4 Graded WithAnswers

The document contains graded questions and solutions related to SQL queries and database concepts. It discusses various SQL queries involving instructors, departments, and employee projects, along with their expected outputs and explanations. Additionally, it addresses the necessity of primary keys in database tables and provides multiple-choice questions on querying data effectively.

Uploaded by

shiva1507004
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)
10 views8 pages

Week4 Graded WithAnswers

The document contains graded questions and solutions related to SQL queries and database concepts. It discusses various SQL queries involving instructors, departments, and employee projects, along with their expected outputs and explanations. Additionally, it addresses the necessity of primary keys in database tables and provides multiple-choice questions on querying data effectively.

Uploaded by

shiva1507004
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/ 8

BSCCS2003: Graded Questions with Solutions

Week 4
1. Consider the following two tables: [MCQ : 1 point]

Instructor ID Instructor Name Department Name


101 Amit Dubey Physics
102 Sarthak Gaur English
Table 1: Instructors
103 Neha Sharma English
104 Sumit Kumar Chemistry
105 Himanshi Mehra Mathematics
Department Name Building Name
English B1
Mathematics B2
Table 2: Departments
Physics B3
Chemistry B2
Finance B5

Which of the following queries will list the names of those instructors whose department
is “English” or whose building is either ‘B1’ or ‘B2’ ?
SELECT Instructor Name from Instructors i, Departments d
WHERE d.Department Name = i.Department Name
and (i.Department Name = ‘English’ or d.Building Name = ‘B1’) and d.Building Name
= ‘B2’;
SELECT Instructor Name from Instructors i, Departments d
WHERE (i.Department Name = ‘English’ or d.Building Name in (‘B1’, ‘B2’));

SELECT Instructor Name from Instructors i, Departments d
WHERE d.Department Name = i.Department Name
and (i.Department Name = ‘English’ or d.Building Name in(‘B1’, ‘B2’));
All of the above

Solution:
Option 1: The first query will result in no records.
Option 2: The second query will result in a Cartesian product and generate wrong
output.
Option 3: The third query will result in the correct result, i.e., instructors names
who belong to “English” department or whose building is either ‘B1’ or ‘B2’.

2. Consider the following two tables: [MCQ : 1 point]


Instructor ID Instructor Name Department Name
101 Amit Dubey Physics
102 Sarthak Gaur English
Table 1: Instructors
103 Neha Sharma English
104 Sumit Kumar Chemistry
105 Himanshi Mehra Mathematics
Department Name Building Name
English B1
Mathematics B2
Table 2: Departments
Physics B3
Chemistry B2
Finance B5

Which of the following queries will list the names of those instructors whose department
is in the building ‘B1’ ?

SELECT Instructor Name from Instructors i INNER JOIN Departments d
ON i.Department Name = d.Department Name
WHERE d.Building Name = ‘B1’;
SELECT Instructor Name from Instructors i INNER JOIN Departments d
ON i.Department Name = d.Building Name
WHERE d.Building Name = ‘B1’;
SELECT Instructor Name from Instructors i INNER JOIN Departments d
ON i.Department Name = d.Department Name
WHERE i.Building Name = ‘B1’;
All of the above

Solution:
Option 1: The first query will result “Sarthak Gaur” and “Neha Sharma” as an inner
join will be performed over a common column named “Department Name” in both
of the tables.
Option 2: The second query will show no results, as there is no entry in the “Instruc-
tors” table whose “Department Name” matches with the column “Building Name”
in table “Departments”.
Option 3: The third query will generate an error, as there is no attribute called
“Building Name” in the table “Instructors”.

Page 2
3. Is it mandatory for a table to have a primary key? [MCQ: 1 point]
Yes, because each table is uniquely identified by its primary key.

No, because databases can have tables with all non-unique fields, they can
exist even without a primary key.
Yes, because each table should have a field with unique entries.
No, as the primary key is automatically set by the table if not given explicitly.

Solution: It is not necessary for a table to have a primary key, as tables can be
designed with no primary key set. A primary key does not define any table uniquely
in the database, but can define a field or a group of fields collectively within the table.
Primary keys are set by the user or the creator of the table, and is not automatically
set by the table in case it is not set by the creator.

4. Consider the following 2 tables:

Employee ID Employee Name Age PhoneNo


1 Saurav Lokesh 23 9871768118
Table 1: Employee 2 Garima Das 25 9987712611
3 Swaroop Kumar 21 9863674830
4 Sumita Verma 25 9872618731
Project ID Employee ID Client ID Project Name
101 1 4 Project1
Table 2 : Project 102 2 8 Project2
103 3 3 Project3
104 4 7 Project4

Which of the following queries will pull out all the Project names and Project id with
their corresponding Employees names only?
[MCQ : 3 Points]

SELECT Employee.Employee ID,


Employee.Employee Name, Project.Project ID,
Project.Project Name
FROM Employee
INNER JOIN Project
ON Employee.Employee ID=Project.Employee ID;


SELECT Project.Project Name,
Project.Project ID, Employee.Employee Name

Page 3
FROM Employee
INNER JOIN Project
ON Employee.Employee ID=Project.Employee ID;

SELECT Employee.Employee ID,


Employee.Employee Name, Project.Project ID,
Project.Project Name,
FROM Employee,
INNER JOIN Project ,
ON Employee.Employee ID=Project.Employee ID;

None of the above

Solution: The correct INNER JOIN syntax is :


SELECT column name(s)
FROM table1
INNER JOIN table2
ON table1.column name = table2.column name;

In option (i) and (iii) this syntax is violated.

Page 4
5. Consider the following tables. [MCQ : 1 point]

1) Table 1: records

2) Table 2: players

What will be the output of the following code?

SELECT players.country
FROM players
INNER JOIN records ON records.player_id = players.player_id
GROUP BY country
ORDER BY SUM(records.runs) DESC
LIMIT 1;

India
Australia
South Africa
Code will produce an error

Page 5
Solution: The query will produce the name of the country with the highest aggre-
gate runs. In this case, it is India.

6. Consider the following tables. [MCQ : 2 points]

1) Table 1: records

2) Table 2: players

What will be the output of following code?

SELECT players.name
FROM players
INNER JOIN records ON records.player_id = players.player_id
WHERE players.role = "opening_batsman" and records.format = "odi"
ORDER BY records.highest_score DESC
LIMIT 1;

Rohit Sharma

Page 6
AB de Villiers
Virat Kohli
David warner

Solution: The query will produce the name of the player with highest “highest score”
in odi and who is also an opening batsman.

7. Consider the following tables. [MCQ : 3 points]


1) Table 1: records

2) Table 2: players

Which of the following queries will output the names of batsmen with the top three
averages in test matches and has played more than 75 test matches?
SELECT players.name
FROM players
INNER JOIN records ON records.player_id = players.player_id
ORDER BY records.average

Page 7
WHERE records.matches > 75 and records.format = "test"
LIMIT 3;
SELECT players.name
FROM players
INNER JOIN records ON records.player_id = players.player_id
WHERE records.matches > 75 and records.format = "test"
ORDER BY records.average
LIMIT 3;
SELECT players.name
FROM players
INNER JOIN records ON records.player_id = players.player_id
ORDER BY records.average DESC
WHERE records.matches > 75 and records.format = "test"
LIMIT 3;

SELECT players.name
FROM players
INNER JOIN records ON records.player_id = players.player_id
WHERE records.matches > 75 and records.format = "test"
ORDER BY records.average DESC
LIMIT 3;

Solution: option 1 and 3 will produce error because WHERE clause should come
before the ORDER BY clause.
option 2 will produce the top 3 batsmen with lower average in test matches and has
played more than 75 test matches.
option 4 will produce the top 3 batsmen with higher average in test matches and has
played more than 75 test matches.

Page 8

You might also like