0% found this document useful (0 votes)
13 views84 pages

CS TERM 1 PRACTICAL FILE (2) - Merged

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)
13 views84 pages

CS TERM 1 PRACTICAL FILE (2) - Merged

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/ 84

CS PRACTICAL FILE

(TERM 1)

ANIKET AGARWAL
CLASS-XII-C
ROLL NO-5
(b) Show all information where capacity > noofstudents in order of rtno:

(c) Show area_covered where Distance > 20 and Charges < 80000:
(d) Show transporter-wise total number of students traveling:

(e) Show Rtno, Area_covered, and average cost per student (Charges /
Noofstudents):
(f) Add a new record with data (11, "Moti Bagh", 35, 32, 10, "Kisan Tours", 35000):
ANS:-
INSERT INTO SchoolBus (Rtno, Area_covered, Capacity, Noofstudents, Distance,
Transporter, Charges) VALUES (11, 'Moti Bagh', 35, 32, 10, 'Kisan Tours', 35000);

(g) Queries and outputs:


(i) Sum of Distance for transporter = 'Yadav Co.':

(ii) Minimum Noofstudents:


(iii) Average Charges for transporter = 'Anand travels':

(iv) Distinct Transporter:


(a) List the names of those students who have obtained DIV 1 sorted by
NAME

(b) Display NAME, STIPEND, SUBJECT, and annual stipend (12 × STIPEND)
c) Count the number of students who are either PHYSICS or COMPUTER Sc.
Graduates

(d) Insert a new row in the GRADUATE table


(e) SQL Queries and Outputs
(i) Select MIN(AVERAGE) from GRADUATE where SUBJECT = 'PHYSICS':

(ii) Select SUM(STIPEND) from GRADUATE WHERE DIVISION = 'II':

(iii) Select AVG(STIPEND) from GRADUATE where AVERAGE >= 65:


(iv) Select COUNT(DISTINCT SUBJECT) from GRADUATE:

(f) Table: GUIDE

Create the GUIDE Table

Insert Data into GUIDE


(i) To show NAME and ADVISOR for all graduates

(ii) Show Natural Join

(iii) Outer Join and Inner Join


•Inner Join:
• Left Outer Join:

(g) Query Outputs


(i) SELECT NAME, ADVISOR FROM GRADUATE, GUIDE WHERE SUBJECT =
MAINAREA AND STIPEND > 250:

(ii) SELECT NAME, ADVISOR FROM GRADUATE, GUIDE:


This query produces a Cartesian Product (every row of GRADUATE combines with
every row of GUIDE).
SQL Command to Create the Table HOSPITAL

SQL Command to Insert the Provided Data into HOSPITAL Table


(a) Show all the information of patients in the Cardiology department

(b) List the names of female patients in Orthopedic or Surgery department

(c) List all patient names with their date of admission in ascending order

(d) Display patient’s name, charges, and age for male patients only
(e) Count the number of patients with age > 20

(f) Display various departments

(g) Display the number of patients in each department

(h) Display the number of male and female patients

(i) Display details of patients admitted in the first quarter of 1998


(j) Display the departments with less than 2 patients

(k) Display details of all patients whose names start with ‘Z’

(l) Change the age of the patient Kush to 20

(m) Increase the charges of all patients by 5%

(n) Remove the record of the patient whose name is Tarun

(o) Add a new column WardNumber to the table


(p) Change the column Charges to allow NULL values

(q) Set Charges to NULL for patients in the Surgery department

(r) Decrease charges by 10% for patients in the Cardiology department

(s) Remove the primary key constraint

(t) Insert a new row into the table

Outputs of SQL Queries


(i) COUNT(DISTINCT Charges)

(ii) MIN(Age) for Male Patients

(iii) AVG(Charges) for Patients Admitted Before '1998-02-12'

(iv) SUM(Charges) for Female Patients


(v) Names of Patients in ENT or Orthopedic Departments

(vi) Lowercase Departments Ending with 'y'

(vii) Group By Department with SUM and MAX of Charges


SQL Queries and Solutions
(a) Display the names of all the activities with their Acodes in descending order
(b) Display the sum of PrizeMoney for the activities played in each stadium separately

(c) Display the coach’s name and Acodes in ascending order of Acode
(d) Display the content of the Activity table whose ScheduleDate is earlier than
01/01/2004 in ascending order of ParticipantsNum

(e) Display the names of coaches who are not associated with any activity at all

If Acode allows NULL in the COACH table:

If not, using a LEFT JOIN:


(f) Display the names of activities and their corresponding coach names

Output of Queries

(i) SELECT COUNT(DISTINCT ParticipantsNum) FROM Activity;

(ii) SELECT Max(ScheduleDate), MIN(ScheduleDate) FROM Activity;


(iii) SELECT COUNT(*) FROM Coach;

(iv) SELECT Name, ActivityName FROM activity A, COACH C WHERE A.Acode =


C.Acode AND A.ParticipantsNum = 10;

(v) SELECT DISTINCT Acode FROM Coach;


(a) SELECT POW (2,3);

● The POW (x, y) function calculates x raised to the power y.


● Output: 8

(b) SELECT ROUND(342.9234, -1);

● The ROUND(number, -1) function rounds the number to the nearest 10s
place (negative precision).
● Output: 340

(c) SELECT LENGTH("Informatics Practices");

● The LENGTH() function returns the number of characters in the string,


including spaces.
● Output: 21

(d) SELECT YEAR("1979/11/26"), MONTH("1979/11/26"),


DAY("1979/11/26"), MONTHNAME("1979/11/26");
● These functions extract specific parts of the date:
○ YEAR() gives the year.
○ MONTH() gives the month as a number.
○ DAY() gives the day.
○ MONTHNAME() gives the full name of the month.

(e) SELECT LEFT("INDIA", 3), RIGHT("Computer Science", 4),


MID("Informatics", 3, 4), SUBSTR("Practices", 3);

● LEFT(string, n): Extracts the first n characters.


● RIGHT(string, n): Extracts the last n characters.
● MID(string, start, n): Extracts n characters starting at position
start.
● SUBSTR(string, start): Extracts the substring starting from position
start till the end.
Table Creation Code
SQL Query Solutions

a) Display all the information from the Movie table.

b) List business done by the movies showing only MovieID, MovieName and
Total_Earning.

Total_Earning = ProductionCost + BusinessCost.

c) List the different categories of movies.


d) Find the net profit of each movie showing its MovieID, MovieName and
NetProfit.

NetProfit = BusinessCost - ProductionCost.

e) List MovieID, MovieName and Cost for all movies with ProductionCost greater than
10,000 and less than 1,00,000.

f) List details of all movies which fall in the category of comedy or action.

g) List details of all movies which have not been released yet
Assumed Table Creation Schema
Table: UNIFORM

Table: COST
Rectifying the Anomalies

a) Adding data for handkerchiefs to both tables

Before inserting into the COST table, the handkerchief's details must first be inserted
into the UNIFORM table to avoid a foreign key violation.

b) Preventing insertion into COST without a valid UniformCode

The FOREIGN KEY constraint ensures that any data entered into the COST table will
require a corresponding record in the UNIFORM table.

If not already present, add the following constraint to the COST table:

This enforces referential integrity, preventing invalid UCode values in the COST table.

c) Ensuring a valid UCode is assigned only if it has a valid UName

To ensure a UCode can only be assigned if the UName is provided, add a NOT NULL
constraint to the UName column in the UNIFORM table:
This ensures that UName must be specified whenever a new UCode is added.

d) Adding a constraint so that the price is always greater than zero

To enforce the condition that the price of an item is greater than zero in the COST table,
add a CHECK constraint:

Final Schema

Table: UNIFORM

Table: COST
a) Create the table Product with appropriate data types and constraints

b) Identify the primary key in Product

The Primary Key is PCode, as it uniquely identifies each product.

c) List the Product Code, Product Name, and price in descending order of
their Product Name. If PName is the same, display in ascending order of
price.

d) Add a new column "Discount" to the table Product


e) Calculate the value of the discount as 10% of UPrice for products where UPrice >
100, otherwise set it to 0.

f) Increase the price by 12% for all the products manufactured by


"Dove"

g) Display the total number of products manufactured by each manufacturer

h) Write the output of the query:

SELECT PName, avg(UPrice) FROM Product GROUP BY PName;


i) Write the output of the query:

SELECT DISTINCT Manufacturer FROM Product;

j) Write the output of the query:

SELECT COUNT(DISTINCT PName) FROM Product;

k) Write the output of the query:


SELECT PName, MAX(UPrice), MIN(UPrice) FROM Product GROUP BY
PName;

Final Table After Updates


CS PRACTICAL FILE
TERM 2

ANIKET AGARWAL
CLASS XII-C
ROLL NO-5
SQL
Create Table:

(b) To show all information of students where capacity is more than the number of
students in order of Rtn.

(c) To show Area_covered for buses covering more than 20 km, but charges less
than 80000.

(d) To show transporter-wise total number of students traveling.

(e) To show Rtn, Area_covered, and average cost per student for all routes
where average cost per student is Charges / No_of_students.
(f) Add a new record with the following data:

(11, "Moti Bagh", 35, 32, 10, "Kisan Tours", 35000)

(g) Give the output considering the original relation as given:

(i) Select sum(distance) from SchoolBus where Transporter = 'Yadav


Travels':

(ii) Select the minimum No_of_students from SchoolBus:

(iii) Select the average Charges from SchoolBus where Transporter = 'Anand
Travels':

(iv) Select distinct Transporter from SchoolBus:


Creating the GRADUATE Table

(a) List the names of those students who have obtained DIV 1 sorted by NAME.

(b) Display a report, listing NAME, STIPEND, SUBJECT, and amount of stipend
received in a year (assuming STIPEND is paid every month).

(c) To count the number of students who are either PHYSICS or COMPUTER Sc.
graduates.

(d) To insert a new row in the GRADUATE table:

(e) Give the output of the following SQL statements based on the GRADUATE table:

(i) Select MIN(AVERAGE) from GRADUATE where SUBJECT = "PHYSICS";


(ii) Select SUM(STIPEND) from GRADUATE WHERE DIV = 'II';

(iii) Select AVG(STIPEND) from GRADUATE where AVERAGE >= 65;

(iv) Select COUNT(distinct SUBJECT) from GRADUATE;

(f) Assume there is one more table GUIDE in the database:

Table: GUIDE

(g) What will be the output of the following query:


SELECT NAME, ADVISOR FROM GRADUATE, GUIDE WHERE SUBJECT =
MAINAREA;
Creating the ACTIVITY and COACH Tables:

(a) Display the names of all the activities with their Acodes in descending order.

(b) Display the sum of PrizeMoney for the activities played in each of the
stadiums separately.

(c) Display the coach’s name and Acodes in ascending order of Acode from the
table COACH.
(d) Display the content of the Activity table whose ScheduleDate is earlier than
01/01/2004 in ascending order of ParticipantsNum.

(e) Display the names of coaches who are not associated with any activity at all.

(f) Display the names of activities and their corresponding coach names.

SQL Query Outputs:


(i) SELECT COUNT(DISTINCT ParticipantsNum) FROM ACTIVITY;

(ii) SELECT Max(ScheduleDate), MIN(ScheduleDate) FROM ACTIVITY;


(iii) SELECT COUNT(*) FROM COACH;

(iv) SELECT Name, ActivityName FROM activity A, COACH C WHERE A.Acode = C.Acode
AND A.ParticipantsNum = 10;

(v) SELECT DISTINCT Acode FROM COACH;


Assume the following file (sample.txt):
The quick brown fox
The lazy dog
A fast runner is always ahead
The sun rises in the east
Let's assume the file Address.TXT contains:

Example Interaction:
Content of NAdd.TXT after replacement:

You might also like