Class 11 Informatics Practices Chapter 8 Introduction to Structured Query Language SQL NCERT Exercise Solution

NCERT Exercise Solution Ch 8 – SQL

Introduction to Structured Query Language NCERT Exercise Solution

9. An organization ABC maintains a database EMPDEPENDENT to record the following details about its employees and their dependents.

EMPLOYEE(AadhaarNo, Name, Address, Department, EmpID)
DEPENDENT(EmpID, DependentName, Relationship)

Use the EMP-DEPENDENT database to answer the following SQL queries:

a) Find the names of employees with their dependent names.

Answer :-

SELECT NAME AS 'EMPLYOEE NAME', DEPENDENTNAME 
FROM EMPLOYEE E, DEPENDENT D 
WHERE E.EMPID = D.EMPID; 

b) Find employee details working in a department, say, ‘PRODUCTION’.

Answer :-

SELECT * FROM EMPLOYEE 
WHERE DEPARTMENT = 'PRODUCTION';  

c) Find employee names having no dependent.

Answer :-

SELECT NAME AS 'EMPLYOEE NAME' FROM EMPLOYEE E 
WHERE E.EMPID IN (SELECT D.EMPID FROM DEPENDENT D 
WHERE DEPENDENTNAME IS NULL);  

d) Find names of employees working in a department, say, ‘SALES’ and having exactly two dependents.

Answer :-

SELECT NAME AS 'EMPLYOEE NAME' FROM EMPLOYEE E 
WHERE E.DEPARTMENT = SALES AND E.EMPID IN 
(SELECT D.EMPID FROM DEPENDENT D 
GROUPBY D.EMPID HAVING COUNT(*) =2);    

10. A shop called Wonderful Garments that sells school uniforms maintain a database SCHOOL_UNIFORM as shown below. It consisted of two relations — UNIFORM and PRICE. They made UniformCode as the primary key for UNIFORM relation. Further, they used UniformCode and Size as composite keys for PRICE relation. By analysing the database schema and database state, specify SQL queries to rectify the following anomalies.

UNIFORM

UCodeUNameUColor
1ShirtWhite
2PantGrey
3SkirtGrey
4TieBlue
5SocksBlue
6BeltBlue

PRICE

UCodeSizePrice
1M500
1L580
1XL620
2M810
2L890
2XL940
3M770
3L830
3XL910
4S150
4L170
5S180
5L210
6M110
6L140
6XL160

a) The PRICE relation has an attribute named Price. In order to avoid confusion, write SQL query to change the name of the relation PRICE to COST.

Answer :-

ALTER TABLE Price
RENAME TO Cost;

b) M/S Wonderful Garments also keeps handkerchiefs of red color, medium size of `100 each. Insert this record in COST table.

Answer :-

INSERT INTO Cost 
VALUES(7, 'm', 100);

c) When you used the above query to insert data, you were able to enter the values for handkerchief without entering its details in the UNIFORM relation. Make a provision so that the data can be entered in COST table only if it is already there in UNIFROM table.

Answer :-

ALTER TABLE Cost
ADD FOREIGN KEY(UCode) REFERENCES UNIFORM(UCode);

d) Further, you should be able to assign a new UCode to an item only if it has a valid UName. Write a query to add appropriate constraint to the SCHOOL_UNIFORM database.

Answer :-

ALTER TABLE Uniform
MODIFY UName VARCHAR(30) NOT NULL;

e) ALTER table to add the constraint that price of an item is always greater than zero.

Answer :-

ALTER TABLE Cost
ADD CONSTRAINT CheckPrice
CHECK(Price > 0);

NCERT Book Exercise Solution – Class 11 Informatics Practices


You cannot copy content of this page

Scroll to Top