Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
285 views

SQL Assignment - 1

This document contains an example worksheet on SQL commands related to hospital and teacher database tables. It includes SQL commands to create and modify tables, insert data, perform queries with filters and sorting. It also discusses concepts like primary keys, relations, and data independence in relational databases.

Uploaded by

muskan dahiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
285 views

SQL Assignment - 1

This document contains an example worksheet on SQL commands related to hospital and teacher database tables. It includes SQL commands to create and modify tables, insert data, perform queries with filters and sorting. It also discusses concepts like primary keys, relations, and data independence in relational databases.

Uploaded by

muskan dahiya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Science with Python

COMPUTER SCIENCE (083): WORKSHEET – 09


(05.05.2022)

Learner’s Name :_________________________________

1. Write the SQL commands for (a) to (f) and write the outputs for (g) on the basis
of the table HOSPITAL

(a) Write the command to create the Hospital table


CREATE TABLE HOSPITAL (NO INT(2), NAME VARCHAR(30), AGE INT(2),
DEPARTEMNT VARCHAR(30), DATEOFADM DATE, CHARGES INT(3), SEX
CHAR(1));
________________________________________________________________________

________________________________________________________________________
(b) Add one more column in the above table Ph_no
ALTER TABLE HOSPITAL ADD (PN_NO INT(10));
________________________________________________________________________

________________________________________________________________________

(c) Change the width of charges to 5 decimal values


ALTER TABLE HOSPITAL MODIFY (CHARGES INT(5));

1
Computer Science with Python
________________________________________________________________________

(d) To show all information about the patients of cardiology department.


SELECT * FROM HOSIPITAL WHERE DEPARTMENT = “CARDIOLOGY”;
________________________________________________________________________

________________________________________________________________________

(e) To list the name of female patients who are in orthopaedic department.
SELECT NAME FROM HOSPITAL WHERE DEPARTMENT= “ORTHOPAEDIC”
AND SEX = “F”;
________________________________________________________________________

________________________________________________________________________

(f) To list names of all patients with their date of admission in ascending order.

SELECT NAME, DATEOFADM FROM HOSPITAL ORDER BY DATEOFADM


DESC;

________________________________________________________________________

(g) The display Patient’s Name, charges, age for male patients only.

SELECT NAME, CHARGES, AGE FROM HOSPITAL WHERE SEXX = “M”;


____________________________________________________________________

________________________________________________________________________

(h) To display Patient’ Name and department with age>20 or charges > 300.

SELECT NAME, DEPARTMENT FROM HOSPITAL WHERE AGE >20 OR


CHARGES > 300;
________________________________________________________________________

________________________________________________________________________

(i) To insert a new row in the HOSPITAL table with the following data:
11, ‘Mustafa’, 37, ‘ENT, {25/02/98}, 250, ‘M’

2
Computer Science with Python
________________________________________________________________________

________________________________________________________________________

(j) Add 100 into the charges whose age are more than 50 years

UPDATE HOSPITAL SET CHARGES = CHARGES +100 WHERE AGE >50;

_______________________________________________________________________

________________________________________________________________________

2. Write the SQL commands for (a) to (f) and write the outputs for (g) on the basis
of the table TEACHER

(a) To show all information about the teacher of history department.


(b) To list the name of female teachers who are in Maths department.
(c) To list names of all teachers with their date of admission in ascending order.
(d) Display teacher’s Name, Salary, age for male teacher only.
(e) To count the number of teacher with age>23.
(f) To insert a new row in the TEACHER table with the following data:
9, ‘Raja’, 26, ‘Computer’, { 13/05/95 },2300, ‘M’
(g) Give the output of the following SQL statement:
(i) Select COUNT(DISTINCT department) from TEACHER;

3
Computer Science with Python
(ii) Select MAX(Age) from TEACHER where SEX ‘F’;
(iii) Select AVG(Salary) from TEACHER where SEX
(iv) Select SUM(Salaty) from TEACHER where DATOFJOIN <(12/07/96);

3. Write SQL commands for (i) to (vii) on the basis of the table SPORTS

Table : SPORTS
Sno Class Name Game1 Grade Game2 Grade1
10 7 Sameer Cricket B Swimming A
11 8 Sujit Tennis A Skating C
12 7 Kamal Swimming B Footbal B
(a) Display the names of the students who have grade ‘C’ in either Game I or Game2
or both.
(b) Display the number of students getting grade ‘A’ in Cricket.
(c) Display the names of the students who have same game for both Game 1 and
Game2.
(d) Display the games taken up by the students, whose name starts with ‘A’.
(e) Add a new column named ‘Marks’.
(f) Assign a value 200 for Marks for all those who are getting grade ‘B’ or grade ‘A
in both Game 1 and Game2,
(g) Arrange the whole table in the alphabetical order of Name.

4
Computer Science with Python

4.(a) What do you understand by the terms Primary Key and Degree of a relation in
relational database?
(b) Consider the following tables EMPLOYEES and EMPSALARY. Write SQL
commands for the statements (i) to (iv) and give outputs for SQL queries (v)

(i) To display Firstname, Lastname, Address and City of all employees living in
Paris from the table EMPLOYEES.
(ii) To display the content of EMPLOYEES table in descei order FIRSTNAME.
(iii) To display the Firstname, Lastname, and Total Salary of all Managers from
tables EMPLOYEES and EMPSALARY, where Total Salary is calculateda
Salary + Benefits.
5
Computer Science with Python
(iv) TO display the Maximum salary among Managers and Clerks from the table
EMPSALARY.
(v) SELECT FIRSTNAME, SALARY
FROM EMPLOYEES, EMPSALARY
WHERE DESIGNATION ‘Salesman’ AND
EMPLOYEES.EMPID EMPSALARY.EMPID;

5. (a) What is a relation? What is the difference between a tuple and an attribute?
(b) Consider the following tables GAMES and PLAYER. Write SQL commands for the
statements (i) to (iv) and give outputs for SQL queries (v) to (viii)
GAMES
GCode GameName Number PrizeMoney Date
101 CaromBoard 2 5000 23-jan-2004
102 Badminton 2 12000 12-dec-2003
103 TableTennis 4 8000 14-feb-2004
104 Chess 2 9000 01-jan-2004
105 LawnTennis 4 25000 19-mar-2004
PLAYER
PCode Name GCode
1 Arjun 101
2 Ravi 105
3 Jignesh 101
4 Nihir 103
5 Sohil 104
i). To display the name of players who plays CaromBoard.
ii). To display details of those game which are having PrizeMoney more than 8000.
iii). To display the details of those games whose name starts from character ‘B’.
iv). To display sum of PrizeMoney for each of the Number of participation groupings.
(as shown in column Number 2 or 4)
v). Select COUNT(DISTINCT number) from games;
vi). Select MAX(date) , MIN(date) from games;
vii). Select AVG(PrizeMoney) from games

6
Computer Science with Python
Group by Number
Having count(GCode) > 2;
viii). Select GameName from games
Where date BETWEEN ’10-Jan-2004’ AND ’20-Feb-2004’;

6 (a) What is data independence? How is logical data independence different from
physical data independence?
(b) Consider the following tables EMPLOYEE and DESIG. Write SQL commands
for the statements (i) to (iv) and give outputs for SQL queries (v) to (viii)

EMPLOYEE
W_ID FIRSTNAME LASTNAME CITY
102 SAM TONES PARIS
105 SARAH ACKERMAN NEW YORK
144 MANILA SENGUPTA NEW DELHI
210 GEORGE SMITH HOWARD
255 MARY JONES HUSTON
300 ROBERT SAMUEL WASHINGTON
335 HENRY WILLIAMS BOSTON
400 RONNY LEE NEW YORK
451 PAT THOMPSON PARIS
DESIG
W_ID SALARY BENEFITS DESIGNATION
102 75000 15000 MANAGER
105 85000 25000 DIRECTOR
144 70000 15000 MANAGER
210 75000 12500 MANAGER
255 50000 12000 CLERK
300 45000 10000 CLERK
335 40000 10000 CLERK
400 32000 7500 SALESMAN
451 28000 7500 SALESMAN

7
Computer Science with Python
i). Display FirstName and City of Employee having salary between 50,000 and
90,000
ii). Display details of Employees who are from “PARIS” city.
iii). Increase the benefits of employee having W_ID = 210 by 500.
iv). Count number of employees whose name starts from character ‘S’.
v). Select MAX(salary) from desig;
vi). Select FirstName from employee, desig
where designation = ‘MANAGER’ AND employee.W_ID = desig.W_ID;
vii). Select COUNT (DISTINCT designation) from desig;
viii). Select designation, SUM(salary) from desig
Group by designation
Having count (*) > 2;

8
Computer Science with Python

7. (a) What do you understand by the terms Cardinality and Degree of a


relation in relational database?
(b) Given the following LAB table, write SQL command for the questions (i) to
(iii) and give the output of (iv).
LAB
No ItemName CostPerItem Quantity Dateofpurchase Warranty Operational
1 Computer 60000 9 21/5/96 2 7
2 Printer 15000 3 21/5/97 4 2
3 Scanner 18000 1 29/8/98 3 1
4 Camera 21000 2 13/10/96 1 1
5 Switch 8000 1 31/10/99 2 1
6 UPS 5000 5 21/5/96 1 4
7 Router 25000 2 11/1/20 2 5
(i) To select the ItemName,which are within the Warranty period till
present date.
(ii) To display all the itemName whose name starts with “C”.
(iii) To list the ItemName in ascending order of the date of purchase
where quantity is more than 3.
(iv) Give the output of the following SQL commands:
(a) select min(DISTINCT Quantity) from LAB;
(b) select max(Warranty) from LAB;
(c) select sum(CostPerItem) from Lab;

9
Computer Science with Python
8. (a)Write SQL commands for (a) to ( j) and write output for (h) on the
basis of Teacher relation given below.

No Name Age Departmen Date of Salary Sex


t Join
1. jigal 34 Computer 10/01/97 12000 M
2. Sharmila 31 History 24/03/98 20000 F
3. Sandeep 32 Maths 12/12/96 30000 M
4. Sangeeta 35 History 01/07/99 40000 F
5. Rakesh 42 Maths 05/09/97 25000 M
6. Shyam 50 History 27/02/97 30000 M
7. Shiv Om 44 Computer 25/02/97 21000 M
8. Shalakha 33 Maths 31/07/97 20000 F

I. To show all information about the teacher of history department.


II. To list the names of female teachers who are in Maths department
III. To list names of all teachers with their date of joining in ascending
order.
IV. To count the number of teachers with age<35.
V. To insert a new row in the TEACHER table with the following data:
9,”Raja”,26,”Computer”,’13/05/95’,2300,”M”.
VI. To count the number of teachers having salary >=12000, with each
department.
VII. Give the output of following statement.
(i) Select COUNT(distinct department) from TEACHER.
(ii) Select name, MAX(Age) from Teacher where sex=”F”

9. (a) What do you understand by Primary Key.


(b) Consider the following tables Employee and salary. Write SQL commands
for the statements (i) to (iv) and give outputs for SQL queries (v) to (viii)
10
Computer Science with Python

Table : Employee
Eid Name Deptid Qualification Sex
1 Deepali Gupta 101 MCA F
2 Rajat Tyagi 101 BCA M
3 Hari Mohan 102 B.A M
4 Harry 102 M.A M
5 Sumit Mittal 103 B.Tech M
6 Jyoti 101 M.Tech F
Table : Salary
Eid Basic DA HRA Bonus
1 6000 2000 2300 200
2 2000 300 300 30
3 1000 300 300 40
4 1500 390 490 30
5 8000 900 900 80
6 10000 300 490 89
(i) To display the frequency of employees department wise.
(ii) To list the names of those employees only whose name starts with
‘H’
(iii) To add a new column in salary table . the column name is total_sal.
(iv) To store the corresponding values in the total_sal column.
(v) Select name from employee where eid=(select eid from salary
where basic= (select max(basic) from salary));
(vi) select max(basic) from salary where bonus >40;
(vii) Select count(*) from employee group by sex;
(viii) select Distinct deptid from Employee;

Give the SQL statement to do the following :

1. To create a table STUDENT with Roll, Name, Age and Marks.


2. To create a table with the following structure as DEPART.
Deptcd Char(3)
Deptname Char(10)

11
Computer Science with Python
City Char(20)
3. To create a table with the following structure as BABY.
name char(20);
4. Create a table called PROJECT with the following columns :
ProjID NUMBER(4)
ProjDesig CHAR(20)
ProjStartDT DATE
ProjEndDT DATE
BudgetAmt NUMBER(7)
MaxNoStaff NUMBER(2) [CBSE Question
Bank 1998]
5. To display the student table with columns in the following order :
name, class, marks.
6. To display the student table with columns of all name with marks
<40.
7. To count the number of students who scored less than 40 marks of
the table student.
8. To find the highest marks of the student table, as per the following
format:
Mr/Ms. <name> has secured highest marks as <marks>
9. To list the names in ascending order of marks, with fields of rollno,
names and marks.
10. To list the names in descending order of marks, with fields of names
and marks from the student table :
11. To insert the following data into the student table :
111,”Yogita”,89.
12. To delete all records from student table with marks = 25.
13. To increase the marks of all students by 10, for name = “Shruti”.
14. To count the total number of records in the table student.
Write SQL commands for the following taking in view the following
table as SCHOOL:
No. Name Age Department Sex
1 Ankit 45 Comp.Sc. M
2 Sumit 32 History M
3 Amit 22 Geog M
4 Suchitra 23 Maths F
5 Ankita 22 Hindi
F
6 Shruti 21 Comp.Sc.
F
7 Raksha 22 Hindi
F
8 Priya 33 Maths F
12
Computer Science with Python
15. To show all information about the members of the Hindi
Department.
16. To list the names of female members who are in Hindi department.
17. To list names of all members with the ascending order of their ages.
18. To display member”s name, age and department name of males.
19. To count the number of members with age >21.
20. To insert a new row in the SCHOOL table with the following data :
9, “Pinto”,31,”Maths”,”M”
Give the output of the following, considering the above table :
21. SELECT COUNT (DISTINCT AGE) FROM SCHOOL;
22. SELECT MAX(AGE) FROM SCHOOL WHERE SEX = “M”;
23. SELECT AVG(AGE) FROM SCHOOL WHERE SEX =”M”;
24. SELECT SUM(AGE) FROM SCHOOL WHERE SEX=”M”;
Consider the following table OFFICE and frame queries for the
following:
S.No. Emp.Name Age Department Sex B-Pay
1 Ankit 45 Comp.Sc. M 2390
2 Sumit 32 History M 2323
3 Amit 22 Geog M 5654
4 Suchitra 23 Maths F 5644
5 Ankita 22 Hindi F 2322
6 Shruti 21 Comp.Sc. F 3323
7 Raksha 22 Hindi F 4321
8 Priya 33 Maths F 2388
25. Find the Department for Employee Ankita.
26. Display the records of all employees who belong to the Hindi
department.
27. Display the detailed table for all employees having Basic Pay greater
than 3000.
28. Display the list of employees who belong to the Maths department
in ascending order of ages.
29. Display the list of employees who belong to the Hindi department in
descending order of ages.
30. Find the total number of records present in the table Office.
31. Find the total number of employees greater than 30 years of age.
32. Find the total number of employees Belonging to the the Computer
Science department.
33. Find the total of Basic Pay of the employees :
34. Find the maximum of the Basic Pay being paid to the employees.
35. Modify the record having name as “Shruti”, by increasing the Basic
Pay by Rs. 100.
36. Modify all the records by increasing the Basic Pay by Rs. 200.
37. Insert a new record with the following information :
13
Computer Science with Python
9 Poonam 30 Maths F 2348
38. Delete the record having Employee name =”Ankit”.
39. Delete all the records belonging to the Hindi Department.

14

You might also like