Class XII – Informatics Practices
MySQL Assignment
1. Consider the given Table Hospital and write SQL queries for the
following:
Table : Hospital
Pn Name Age Department DateofAdm Charges Sex
o
1 Arpit 54 Surgery 2013-12-12 20000 M
2 Zarina 32 ENT 2014-05-25 3500 F
3 Arun NULL Orthopaedic 2014-06-09 4000 M
4 Ketal 16 Surgery 2015-01-30 16000 F
5 Kush 12 ENT 2014-12-13 800 M
6 Shilpa 30 Gynaecology 2014-10-31 1200 F
7 Karan 31 Cardiology 2014-06-06 2500 M
8 Arun NULL Orthopaedic 2014-07-18 4500 M
9 Zarina 32 ENT 2014-06-26 3500 F
i. Write a command to describe the structure of the above table.
ii. To list the names of female patients in ENT department.
iii. To count the number of patients with Age less than 30.
iv. To list all details of patients in descending order of their age.
v. To modify the column Department as char(25).
vi. To display average age of patients in each department.
vii. To display number of female patients in each department.
viii. To add a column named address of varchar type with 25 as size.
ix. To delete the column address from the table.
x. To list genderwise sum of charges of all patients whose name starts with
letter K.
xi. To display total charges of Surgery department.
xii. To display maximum charges paid in each department only for those who
visit is more than 1 in the same department.
xiii. To change the department of karan to Radiology department.
xiv. To list the patient details admitted in year 2014.
xv. To display first 3 letters of patient name concatenated with first 3
department name and a hyphen (-) in middle. For ex.: Result of first row
will be Arp-Sur.
xvi. To list all departments available without repetition.
Write the output of the following:
xvii. Select AVG(charges) from HOSPITAL where sex= ‘F’;
xviii. Select MIN(age) from HOSPITAL where DateofAdm<”2014-07-01”;
xix. Select concat(left(Name,3), mid(department,2,4) from hospital where
charges>10000;
xx. Select instr(name,”s”) from hospital;
xxi. Select avg(age) from hospital where sex=”M”;
2. Consider table Results to answer the following questions:
Table : Results
Column Name Data Size Constraints
Type
RollNumber Integer 5 Primary key
St_name Varchar 15 Unique
Marks Integer 3 Not Null
Percentage Float 5,2
Rank Integer 2
i. Write a SQL command to create above table results including all
constraints.
ii. Write a SQL command to display all students scoring above 80 percent.
iii. Write a query to increase marks by 5 whose st_name is Lokesh Sharma.
iv. Write a query to count rank 1 students whose name contains K as third
character.
v. Write a query to insert a new row in table results.
3. Write the output of the following queries:
i. SELECT LEFT(MID (“HINDUSTAN”,2,7) , 4) ;
ii. SELECT MOD (POWER (3,2), 3);
iii. SELECT POWER(INSTR(“MANGO”, “AN”), 4);
iv. SELECT UCASE (CONCAT(SUBSTR(“OPERATING”,4), “SYSTEM”);
v. SELECT ROUND(145.352,-1)+ROUND( 1523.259,1);
4. Write a SQL commands for (i) to (iV) on the basis of table FURNITURE.
Table : FURNITURE
NO ITEMNAME TYPE DATEOFSTOCK PRICE DISCOUNT
1 White lotus Double Bed 23/02/02 30000 25
2 Pink feather Baby cot 20/01/02 7000 20
3 Dolphin Baby cot 19/02/02 9500 20
4 Decent Office Table 01/01/02 25000 30
5 Comfort Zone Double Bed 12/01/02 25000 25
6 Donald Baby cot 24/02/02 6500 15
7 Royal Finish Office Table 20/02/02 18000 30
8 Royal tiger Sofa 22/02/02 31000 30
9 Econo sitting Sofa 13/12/01 9500 25
10 Eating Paradise Dining table 19/02/02 11500 25
i. To list the ITEMNAME which are priced at more than 15000 from the
FURNITURE table.
ii. To list ITEMANME and TYPE of those items, in which DATEOFSTOCK is
before 22/01/02 from FURNITURE table in descending order of ITEMNAME.
iii. To display ITEMANME and DATEOFSTOCK of those items, in which the
discount percentage is more than 25 from FURNITURE table.
iv. To count the number of items, whose TYPE is “sofa” from FURNITURE table.
Topic: SQL Joins
Q1. Write SQL commands and output based on following relations.
TABLE: CONSIGNEE
CNEEID CNORID CNEENAME CNEEADDRESS CNEECITY
RAHUL
MU05 ND01 5, PARK AVENUE MUMBAI
KISHORE
ND08 ND02 P DHINGRA 16/J MOORE ENCLAVE NEW DELHI
KO19 MU15 A P ROY 2A CENTRAL AVENUE KOLKATA
MU32 ND02 S MITTAL P245, AB COLONY MUMBAI
ND48 MU50 B P JAIN 13 BLOCK-D A VIHAR NEW DELHI
TABLE: CONSIGNOR
CNORID CNORNAME CNORADDRESS CITY
ND01 R SINGHAL 24, ABC ENCLAVE NEW DELHI
NDO2 AMIT KUMAR 123, PALM NEW DELHI
MU15 R KOHLI 5/A SOUTH STREET MUMBAI
MU50 S KAUR 27 K WESTEND MUMBAI
1. To display the names of all consignees as well as consignors.
2. To display the names of all consignees as well as consignors where
consignee city is Delhi.
3. To display the number of consignors from each city
4. Select a.cnorname, b.cneename from consignor a, consignee b where
a.cnorid = b.cnorid and cneecity = ‘MUMBAI’;
Q2. Write SQL commands and output based on following relations.
TABLE: SENDER
SENDERID SENDERNAME SENDERADDRESS SENDERCITY
ND01 R JAIN 2, ABC APPTS NEW DELHI
MU02 H SINHA 12, NEWTOWN MUMBAI
MU15 S JHA 27/A, PARK STREET MUMBAI
ND50 T PRASAD 122-K, SDA NEW DELHI
TABLE: RECIPIENT
RECID SENDERID RECNAME RECADDRESS RECCITY
KO05 ND01 R BAJPAYEE 5, CENTRAL AVENUE KOLKATA
ND08 MU01 S MAHAJAN 116, A VIHAR NEW DELHI
MU19 ND01 H SINGH 2A, ANDHERI EAST MUMBAI
MU32 MU15 PK SWAMI B5, CS TERMINUS MUMBAI
ND48 ND50 S TRIPATHI 13, B1 D, MAYUR VIHAR NEW DELHI
1. To display recid, sendername, senderid, senderaddress, recname,
recaddress for every recipient.
2. To display the number of recipients from each reccity where the number of
recipients is more than 1.
3. To display the number of recipients from each sendercity.
4. Select A.sendername, B.recname from sender A, recipient B where
A.senderid =B.senderid and reccity=’NEW DELHI’;
5. Write a query to count the number of senders in each city. Display the city
and the corresponding sender count.
6. Write a query to find the number of recipients in each city. Display the city
and the total number of recipients.
7. Create a query to group senders by city and list the sender names in each
city.
8. Write a query to display the number of recipients associated with each
sender based on SENDERID. Show SENDERID and the count of recipients.
9. Formulate a query to find out how many senders are in New Delhi and how
many in Mumbai, grouping them by city and counting.
Sol: SELECT SENDERCITY, COUNT(*) AS SenderCount FROM SENDER
WHERE SENDERCITY IN ('NEW DELHI', 'MUMBAI') GROUP BY SENDERCITY;
10. Write an equi join query to display all sender and recipient details where
SENDERID matches in both tables. Include sender name, sender address,
recipient name, and recipient address in the output.
11. Construct a query to display SENDERID, SENDERNAME, and RECNAME for
matching records in both tables.
12. Write a query to join both tables and display the full details of senders and
recipients who are from the same city.
13. Create a query to display senders and their corresponding recipients, but
only for records where the recipient city is New Delhi.
14. Write a query to list all the recipients and the corresponding sender
addresses using inner join, showing RECNAME and SENDERADDRESS.
Q3. Write SQL commands and output based on following relations.
Table: Stationary
S_ID StationaryName Company Price
DP01 Dot Pen ABC 10
PL02 Pencil XYZ 6
ER05 Eraser XYZ 7
PL01 Pencil CAM 5
GP02 Gel Pen ABC 15
Table: Consumer
C_ID ConsumerName Address S_ID
01 Good Learner Delhi PL01
06 Write Well Mumbai GP02
12 Topper Delhi DP01
15 Write & Draw Delhi PL02
16 Motivation Banglore PL01
1. To display the ConsumerName, Address from Table Consumer, and
Company and Price from table Stationary, with their corresponding
matching S_ID.
2. To display the number of consumers in various cities.
3. SELECT Company, MAX(Price), MIN(Price), COUNT(*) from Stationary
GROUP BY Company;
4. SELECT Consumer.ConsumerName, Stationary.StationaryName,
Stationary.Price FROM Strionary, Consumer WHERE
Consumer.S_ID=Stationary.S_ID;
Q4. Consider these two tables to solve the following questions:
1. Write a query to perform a Cross Join between Employees and Departments
tables and display all combinations of employee and department records.
2. Modify the Cross Join query to display only EmpID, FirstName, and
DepartmentName
3. Write a query to create a natural join between two tables.
4. What is the difference between Cross Join and Natural Join? Explain with
examples from the tables provided.