Вы находитесь на странице: 1из 11

Database Management System: Assignment 2

Total Marks : 20

March 6, 2019

Question 1
Which is the correct SQL command to create the table borrower having two foreign keys
customer name and loan number by using the following schemas? Primary keys are underlined
in the schemas.
Marks: 2 MCQ
• loan(loan number, branch name, amount)

• customer(customer name, customer street, customer city)

• borrower(customer name, loan number)

a) CREATE TABLE borrower(


customer_name VARCHAR(20),
loan_number NUMERIC(12,2),
PRIMARY KEY(customer_name, loan_number),
FOREIGN KEY(customer_name) REFERENCES customer,
FOREIGN KEY(loan_number) REFERENCES loan);

b) CREATE TABLE borrower(


customer_name VARCHAR(20),
loan_number NUMERIC(12,2),
PRIMARY KEY(customer_name),
FOREIGN KEY(customer_name) REFERENCES customer,
FOREIGN KEY(loan_number) REFERENCES loan);

c) CREATE TABLE borrower(


customer_name VARCHAR(20) PRIMARY KEY,
loan_number NUMERIC(12,2) PRIMARY KEY,
FOREIGN KEY(customer_name) REFERENCES customer,
FOREIGN KEY(loan_number) REFERENCES loan);

d) CREATE TABLE borrower(


customer_name VARCHAR(20),
loan_number NUMERIC(12,2),
PRIMARY KEY(customer_name, loan_number),
FOREIGN KEY(customer_name) ,
FOREIGN KEY(loan_number) REFERENCES loan);

1
Answer: a)

Explanation: The syntax to create a table using FOREIGN KEY is:

CREATE TABLE table name(


column1 data_type(size),
column2 data_type(size),
.....
columnn data_type(size),
PRIMARY KEY(column names),
FOREIGN KEY(column name) REFERENCES table name);

In option b) only customer name is declared as PRIMARY KEY, but in the given schema
borrower there are two attributes namely customer name and loan number which are PRIMARY
KEY well as FOREIGN KEY.

Option c) generates an error message as table can have only one primary key. To de-
clare two columns as PRIMARY KEY we must use PRIMARY KEY(column1, column2) in the
CREATE TABLE command.

Option d) generates an error message missing keyword, since REFERENCES keyword is missed
for the FOREIGN KEY(customer name).

So from the above options only option a) is correct and rest are incorrect.

2
Question 2
Identify the correct INSERT command/s to create a record for the customer table as given
below. Primary key is underlined in the schema.
Marks: 2 MSQ
customer
customer name customer street customer city

P. SAMBHU NEW COMPLEX CHENNAI

a) INSERT INTO customer


VALUES(’P. SAMBHU’, ’NEW COMPLEX’, ’CHENNAI’);

b) INSERT INTO customer


VALUES("P. SAMBHU", "NEW COMPLEX", "CHENNAI");

c) INSERT INTO customer


VALUE(’P. SAMBHU’, ’NEW COMPLEX’, ’CHENNAI’);

d) INSERT INTO customer


(customer_name,customer_street,customer_city)
VALUES(’P. SAMBHU’, ’NEW COMPLEX’, ’CHENNAI’);

Answer: a), d)

Explanation: The syntax of INSERT command is:

INSERT INTO table_name(column1, column2,...,columnN)


VALUES(value1,value2,...,valueN);

Or

INSERT INTO table_name


VALUES(value1,value2,...,valueN);

Option b) generates an error message as character values must use (’ ’) single quote.

Option c) also generates error message – VALUE is written instead of VALUES.

Option a) and d) are correct as they satisfy the required syntax.

3
Question 3
Choose the correct SQL command to display the customer name and loan number of customers
who have a loan from bank and whose name starts with ’A’. Primary keys are underlined in
the schema.
Marks: 2 MCQ

• borrower(customer name, loan number)

a) SELECT * FROM borrower WHERE customer_name = ’A’;

b) SELECT * FROM borrower WHERE customer_name LIKE ’A%’;

c) SELECT * FROM borrower WHERE customer_name LIKE ’%A’;

d) SELECT * FROM borrower WHERE customer_name LIKES ’A%’;

Answer: b)

Explanation:
To search for a specified pattern in a column we use LIKE operator in the WHERE clause. The
% sign represent zero or multiple characters.

Option a) displays the name and loan number of customers whose names are exactly ’A’.

Option c) displays the name and loan number of customers whose names ends with ’A’.

Option d) gives error message, invalid relational operator for the word LIKES.

Since option b) satisfies the requirements, it is correct.

4
Question 4
Which of the following option/s is/are correct to find the names of the customers who are
having account in the bank without having a loan in the bank? Primary keys are underlined
in the schemas.

Marks: 2 MSQ

• depositor(customer name, account number) – has account

• borrower(customer name, loan number) – has taken loan

a) SELECT DISTINCT customer_name


FROM depositor INTERSECT
SELECT DISTINCT customer_name
FROM borrower;

b) SELECT customer_name
FROM depositor
WHERE customer_name
NOT IN
(SELECT customer_name
FROM borrower);

c) SELECT customer_name
FROM borrower
WHERE customer_name
NOT IN
(SELECT customer_name
FROM depositor);

d) SELECT DISTINCT customer_name


FROM depositor
MINUS
SELECT DISTINCT customer_name
FROM borrower;

Answer: b), d)

Explanation: We can use NOT IN or MINUS or EXCEPT operators to solve this type of query.

So option b) and d) are correct and rests are incorrect.

Option a) finds the names of the customers who have both an account and a loan in the
bank.

Option c) finds the names of the customers who have a loan but do not have an account
in the bank.

5
Question 5
Consider the schemas person and salary to identify the correct query/queries that display/s
the name and the salary of the persons who work in ‘KOLKATA’. Primary keys are underlined
in the schemas. Marks: 2 MSQ

• person(person id, person name, location)

• salary(person id, sal)

a) SELECT person_name, sal


FROM person,salary
WHERE person.person_id = person_id
AND location = ’KOLKATA’;

b) SELECT person_name, sal


FROM person,salary
WHERE location = ’KOLKATA’;

c) SELECT person_name, sal


FROM person,salary
WHERE person.person_id = salary.person_id
AND location = ’KOLKATA’;

d) SELECT person_name, sal


FROM person JOIN salary
ON person.person_id = salary.person_id
AND location = ’KOLKATA’;

Answer: c), d)

Explanation: From the above option c) and d) are correct as it uses join operation to
compare the attributes person.person id and salary.person id to avoid ambiguity in the
columns.

The syntax for joining two tables is:

SELECT columns
FROM table1 JOIN table2
ON table1.column = table2.column;

Option a) creates ambiguity in columns as salary.person id is missing.

Option b) perform Cartesian products between person and salary which will not compare
for the equality of column values.

6
Question 6
Which is/are the correct SQL command/s to display the loan numbers of the customers in
ascending order of their loan amounts. Primary key is underlined in the schemas.

Marks: 2 MSQ

• loan(loan number, branch name, amount)

a) SELECT loan_number FROM loan ORDER BY amount;

b) SELECT loan_number FROM loan SORT BY amount;

c) SELECT loan_number FROM loan ORDER BY amount ASC;

d) SELECT loan_number FROM loan GROUP BY amount;

Answer: a), c)

Explanation: The ORDER BY clause is used to sort list items in ascending order by de-
fault. We also use DSC or ASC to sort the values in descending or ascending order respectively.

Option b) and d) are incorrect since option b) generates the error message, SQL command
not properly ended since SORT BY is not valid for this.

Option d) also generates error message, not a GROUP BY expression.

So option a) and c) are correct.

7
Question 7
Given the schema (primary key is underlined) Marks: 2 MCQ

salary(person id, sal)

describe the result obtained by the following query.

SELECT MAX(sal) AS SALARY


FROM salary
WHERE sal < (SELECT MAX(sal) FROM salary);

a) Finds the salary of the person with highest salary

b) Finds the salary of the person with second highest salary

c) Finds the salary of all persons having less than the highest salary

d) Finds the salary of all persons having the highest salary

Answer: b)

Explanation:
This is a SUB-QUERY in SQL which finds the maximum salary from the table and then com-
pares the salary which is less than the maximum salary to find the second highest salary.

So only option b) is correct and other options are incorrect.

8
Question 8
Suppose a bank wants to make a view consisting of the names of customers having loan in
’MUMBAI’ branch with the loan amount being more than 50000 but less than 70000. Identify
the correct query from the following. Primary keys are underlined in the schemas.

Marks: 2 MSQ

• loan(loan number, branch name, amount)


• borrower(customer name, loan number)

a) CREATE VIEW v1 AS
SELECT customer_name
FROM loan, borrower
WHERE branch_name = ’MUMBAI’
AND loan.loan_number = borrower.loan_number
AND amount > 50000 AND amount < 70000;
b) CREATE VIEW v1 AS
SELECT customer_name
FROM loan
WHERE branch_name = ’MUMBAI’
AND amount > 50000 AND amount < 70000;
c) CREATE VIEW v1 AS
SELECT customer_name
FROM loan, borrower
WHERE branch_name = ’MUMBAI’
AND loan.loan_number = borrower.loan_number
AND amount BETWEEN 50000 AND 70000;
d) CREATE VIEW v1 AS
SELECT customer_name
FROM loan, borrower
WHERE branch_name = ’MUMBAI’
AND amount > 50000, amount < 70000;

Answer: a), c)

Explanation: The Syntax for creating a VIEW in SQL is:


CREATE VIEW view name AS
SELECT column1, column2...
FROM table name
WHERE condition;
Moreover, the joining will be used to avoid duplicate values in the view.
So from the above only option a) and c) are satisfying the requirements and rest are incor-
rect.

Option b) generates error,"CUSTOMER NAME": invalid identifier since borrower table is miss-
ing in the FROM clause.

Option d) also generates error – SQL command not properly ended since the conditions
are combined through (,) instead of AND operator.

9
Question 9
Identify the following the correct way/s to delete all rows at a time from salary table.

Marks: 2 MSQ

a) DELETE * FROM salary;

b) TRUNCATE TABLE salary;

c) DELETE FROM salary;

d) TRUNCATE TABLE FROM salary;

Answer: b), c)

Explanation: To delete all rows from a table we can use either DELETE or TRUNCATE command
and the syntax for these commands are:

DELETE FROM table name


[WHERE conditions];

Here condition is optional.

and

TRUNCATE TABLE table name;

So option b) and c) are correct and rest of the options are incorrect.

Option a) gives an error due to * operator in DELETE command.

Option d) also gives an error as it violates the syntax of TRUNCATE command.

10
Question 10
Find the names of the branches whose average loan amount is more than 70000. Primary keys
are underlined in the schemas.
Marks: 2 MCQ

• loan(loan number, branch name, amount)

a) SELECT branch_name
FROM loan
GROUP BY branch_name AVG(amount) > 70000;

b) SELECT branch_name
FROM loan
GROUP BY branch_name
HAVING AVG(amount) > 70000;

c) SELECT branch_name
FROM loan
ORDER BY branch_name
HAVING AVG(amount) > 70000;

d) SELECT branch_name
FROM loan
GROUP BY branch_name
HAVING amount > 70000;

Answer: b)

Explanation: The syntax is as follows:

SELECT column name(s)


FROM table name
WHERE condition
GROUP BY column name(s)
HAVING condition
ORDER BY column name(s);

Where ORDER BY column name(s) is optional.

So only option b) is correct and rest of the options are all incorrect.

Option a) generates error – SQL command not properly ended due to missing of HAVING
clause.

Option c) is also gives the error – SQL command not properly ended as it uses ORDER BY
clause before HAVING

Option d) does not compare the average loan amount since AVG keyword is missing.

11

Вам также может понравиться