Worksheet - DBMS CASE STUDY
Worksheet - DBMS CASE STUDY
Write the SQL Commands for the following questions based on the table OPD.
TABLE: OPD
REGNO NAME AGE DEPARTMENT DATEOFREG CHARGES GENDER
R0123 Arpita 62 ENT 2000-01-20 500 M
R0124 Jai 55 Orthopedic 2002-05-22 400 M
R0125 Kamal 34 Gen. Physician 2012-03-14 500 M
R0126 Arun 25 Cardiology NULL 800 M
R0127 Neha 30 ENT 2014-12-16 600 F
R0128 Neetu 28 Cardiology 2015-07-13 500 F
R0129 Ankit 27 Orthopedic NULL 300 F
1) Insert a record into the table OPD using the following data.
REGNO – R0122, NAME – Jilesha, AGE – 32, DEPARTMENT – Cardiology, CHARGES – 700,
GENDER - F (DATEOFREG is unknown)
INSERT INTO OPD (REGNO, NAME, AGE, DEPARTMENT, CHARGES, GENDER)
VALUES (‘R0122’, ‘Jilesha’, 32, ‘Cardiology’, 700, ‘F’)
2) Add a column EXPERIENCE with datatype integer(10) and NOT NULL constraint.
ALTER TABLE OPD ADD COLUMN EXPERIENCE integer(10) NOT NULL;
3) Display the records of the ENT department from table OPD in alphabetical order as per the name of
the doctors.
SELECT * FROM OPD WHERE DEPARTMENT= ‘ENT’ ORDER BY NAME;
4) Display the Name, Department and charges whose charges are in the range of 500 to 1000.
SELECT NAME, DEPARTMENT, CHARGES FROM OPD
WHERE CHARGES BETWEEN 500 and 1000;
5) Display all the records excluding Gen. Physician Department.
SELECT * FROM OPD WHERE DEPARTMENT < > ‘Gen. Physician’;
Write the SQL Commands for the following questions based on the table OPD.
1) Display REGNO, NAME, DEPARTMENT from the table OPD whose DATEOFREG is not known.
SELECT REGNO, NAME, DEPARTMENT FROM OPD WHERE DATEOFREG IS NULL;
2) Display the records from the table OPD whose name starts with ‘A’ and contains ‘n’ in it.
SELECT * FROM OPD WHERE NAME LIKE ‘A%n%’;
3) Display the unique values of the DEPARTMENT column of the table OPD.
SELECT DISTINCT DEPARTMENT FROM OPD;
Write the output for the following SQL Commands based on the table OPD.
4) SELECT GENDER, COUNT(*) FROM OPD GROUP BY GENDER;
GENDER COUNT(*)
M 4
F 3
5) SELECT DEPARTMENT, MAX(CHARGES) AS ‘Highest Charge’ FROM OPD
HAVING COUNT(*)>1;
DEPARTMENT Highest Charge
ENT 600
ORTHOPAEDIC 400
CARDIOLOGY 800
TABLE: WORKER
WNO WNAME DOJ PAY GENDER DCODE
1001 George K 2013-09-02 75000 MALE D01
1003 Mohitesh 2013-02-03 62000 MALE D05
1002 Anil Jha 2014-01-17 NULL MALE D04
1004 Manila Sahai 2012-12-09 72000 FEMALE D01
1005 R Sahana 2013-11-18 74000 MALE D02
1006 Jaya Priya 2014-06-09 66000 FEMALE D05
TABLE: DEPT
DCODE DEPARTMENT CITY
Write the output for the following SQL Queries based on the relations WORKER and DEPARTMENT.
1) SELECT COUNT(DISTINCT DCODE) FROM WORKER;
COUNT(DISTINCT DCODE)
4
2) SELECT DCODE, SUM(PAY) AS “TOTAL PAY” FROM WORKER GROUP BY DCODE
HAVING COUNT(*) > 1;
DCODE TOTAL PAY
D01 147000
D05 128000
62000
MALE 70333.33
FEMALE 69000.00
Write the SQL commands for the following based on the table LAB.
1) Increase the capacity of all the labs by 10 students which are on the I floor.
UPDATE LAB SET CAPACITY = CAPACITY + 10
WHERE FLOOR = ‘I’;
2) Remove the LANGUAGE LAB record from the table LAB.
DELETE FROM LAB WHERE LABNAME=’LANGUAGE’;
3) Add Primary key constraint to the column LABNO.
ALTER TABLE LAB ADD PRIMARY KEY(LABNO);
TABLE: LAB
LABNO LABNAME INCHARGE CAPACITY FLOOR
P0002 4 1
P0003 2 2
P0001 3 2
P0004 5 1
Write the outputs of the SQL Queries 4 to 7 based on the relations COMPUTER and SALES.
200 4300
LOGITECH 700
CANON 8300
6) SELECT PROD_NAME, QTY_SOLD FROM COMPUTER C.SALES S
WHEREC.PROD_ID = S.PROD_ID AND TYPE= ‘INPUT’;
PROD_NAME QTY_SOLD
MOUSE 3
KEYBOARD 2
JOYSTICK 5
7) SELECT * FROM COMPUTER NATURAL JOIN SALES;
PROD_ID PROD_NAME PRICE COMPANY TYPE QTY_SOLD QUARTER
Write the SQL commands for the following questions based on the tables PRODUCT and CLIENT.
1) To display details of products whose manufacturer is either ABC or XYZ.
SELECT * FROM PRODUCT WHERE MANUFACTURER IN (‘ABC’, ‘XYZ’);
2) To display Product name, manufacturer and price for all the products that are giving discount.
SELECT PNAME, MANUFACTURER, PRICE FROM PRODUCT
WHERE DISCOUNT IS NOT NULL;
3) To display Product name and price for all the products whose name does not end with ‘Wash’.
SELECT PNAME, PRICE FROM PRODUCT WHERE PNAME NOT LIKE ‘%Wash’;
4) To display the P_ID, Product name and Client name for all the clients whose city is Delhi.
SELECT P. P_ID, PNAME, CNAME FROM PRODUCT P, CLIENT C
WHERE P. P_ID = C. P_ID and C. CITY = ‘Delhi’;
Write the output for the following SQL Queries based on the tables given PRODUCT and CLIENT.
5) SELECT P_ID, PNAME, CNAME, CITY FROM PRODUCT P LEFT JOIN CLIENT C
ON P. P_ID = C. P_ID ;
P_ID PNAME CNAME CITY
Bath Soap 1
Face Wash 2
Shampoo 1
Talcum Powder 2
Talcum Powder 40
Face Wash 45
Delhi
Mumbai
Bengaluru
3
10) SELECT P_ID, PNAME, CNAME, CITY FROM PRODUCT P RIGHT JOIN CLIENT C
ON P. P_ID = C. P_ID ;
P_ID PNAME CNAME CITY
(a) Both A and R are true and R is the correct explanation for A
(b) Both A and R are true and R is not the correct explanation for A
(c) A is True but R is False
(d) A is false but R is True
1) Assertion (A): COUNT(*) returns the total number of records from a relation and
SELECT COUNT(NAME) FROM EMPLOYEE;
Above command returns number of records present in NAME column ignoring null
values.
Reasoning (R): Aggregate functions can be used with columns with only numerical
values. Ans: c)
2) Assertion(A): The resultset refers to a logical set of records that are fetched from the
database by executing an SQL query.
Reason(R): Resultset stored in a cursor object can be extracted by using fetch(...)
functions. ANS: b)
3) Assertion: The internet is a collection of interconnected computer networks, linked
by transmission medium such as fibre optic cables, transmission wires, wireless
connections, etc.
Reason: World Wide Web is a collection of websites or web pages stored in web
servers and connected to local computers through the internet. ANS: b)
4) Assertion ( A): In SQL, the aggregate function AVG() calculates the average value on
a set of values and produces a single result.
Reason ( R): The aggregate functions are used to perform some fundamental
arithmetic tasks such as Min(), Max(), Sum() etc ANS: b)
5) Assertion (A): COUNT function ignores DISTINCT.
Reason ( R ): DISTINCT ignores duplicate values.
6) Assertion (A): Order By clause is used to sort the records in MySQL tables.
Reason (R) : For sorting, ASC and DESC keywords are mandatory.
7) Assertion (A): UNIQUE keyword ensures no duplicate values in a table.
Reason (R): DISTINCT is similar to UNIQUE.
8) Assertion (A): WHERE clause cannot be used with aggregate functions.
Reason (R): Having clause is used to place condition on group of rows.
9) Assertion (A): SQL SELECT provides clauses for summarizing results.
Reason (R): The GROUP BY clause allows to create summarised results.
10) Assertion (A) : A protocol means the set of rules that are applicable for a network.
Reason (R): STP is a networking protocol.
11) Assertion (A): Cartesian Product is also called as cross join.
Reason (R): SQL join which relates 2 tables when no condition is specified is cross
join.
12) Assertion(A): Inner Join in SQL retrieves the records that have matching values in
both the tables.
Reason (R): Left join and Right join are the variants of Inner join.
13) Assertion (A): Circuit Switching networks are specifically designed for Voice
communication.
Reason (R): Delay time between the data units is uniform in circuit switching
networks.
14) Assertion (A): IP address IPv4 is a 32 bit address IPv6 is a 128 bit address.
Reason (R): Each octet in IPv4 is separated by colon and it is separated by dot in
IPv6.
15) Assertion (A): Lower bandwidth leads to higher data transfer rate.
Reason (R): Bandwidth means the transmission capacity of a network over a specific
period of time. Data transfer rate is the actual amount of data transferred per unit time.