SQL Assignment - 1
SQL Assignment - 1
1. Write the SQL commands for (a) to (f) and write the outputs for (g) on the basis
of the table HOSPITAL
________________________________________________________________________
(b) Add one more column in the above table Ph_no
ALTER TABLE HOSPITAL ADD (PN_NO INT(10));
________________________________________________________________________
________________________________________________________________________
1
Computer Science with Python
________________________________________________________________________
________________________________________________________________________
(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.
________________________________________________________________________
(g) The display Patient’s Name, charges, age for male patients only.
________________________________________________________________________
(h) To display Patient’ Name and department with 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
_______________________________________________________________________
________________________________________________________________________
2. Write the SQL commands for (a) to (f) and write the outputs for (g) on the basis
of the table 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
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.
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;
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