0% found this document useful (0 votes)
10 views

DBMS Week2 Exercixe

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)
10 views

DBMS Week2 Exercixe

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

WEEK – 2

SUBJECTS
Schema:
Subjects (subcode, description, year, sem, credits)

Column name Data type width remarks


Subcode char 5 Subject code
description char 25 Subject title
Year Number 1 Year in which taught
sem Number 1 semester
credits Number 1

Instance:
subcode Description Year Sem credits
5BS11 Advanced Calculus 1 1 4
5ME53 Engineering workshop 1 1 2
5CS11 Code of Ethics 1 2 3
5IT52 Datastructures lab 1 2 2
5EE22 Elements of EEE 2 1 3
5CS07 Java Programming 2 2 3
5IT08 Operating systems 3 1 3
5CS13 Data Mining 3 2 4
5BS42 Management Science 4 1 3
5CS92 Technical Seminar 4 2 2

Creating the table and instances:

create table subjects (

subcode char(5),

description char(25),
year number(1),

sem number(1),

credits number(1)

);

insert into subjects (subcode, description, year, sem, credits) values

('5BS11', 'advanced calculus', 1, 1, 4),

('5ME53', 'engineering workshop', 1, 1, 2),

('5CS11', 'code of ethics', 1, 2, 3),

('5IT52', 'datastructures lab', 1, 2, 2),

('5EE22', 'elements of eee', 2, 1, 3),

('5CS07', 'java programming', 2, 2, 3),

('5IT08', 'operating systems', 3, 1, 3),

('5CS13', 'data mining', 3, 2, 4),

('5BS42', 'management science', 4, 1, 3),

('5CS92', 'technical seminar', 4, 2, 2);

Queries:
1. Change the data type of description column to varchar.
SQL> alter table subjects
2 modify description varchar(25);

Output:

2. Display the data in the subjects table.


SQL> select * from subjects;
Output:

3. What are the subjects handled by the H & S department (Hint: The subject code
consists of the letters ‘BS’).
SQL> SELECT * FROM subjects WHERE SUBCODE LIKE '%BS%';
Output:

4. How many subjects are handled by the Mechanical department. List.


SQL> Select COUNT(description) from subjects where subcode like '%ME%';
Output:

5. List the subjects studied by the student in 3rd year 1st semester.
SQL> SELECT * FROM subjects WHERE year LIKE 3 AND sem LIKE 1;
Output:

6. Display the subjects which have two words in their description.


SQL> select description from subjects
2 where description like '% %';
Output:
7. List the 1st year subjects, not handled by the H & S department.
SQL> SELECT * FROM subjects WHERE year LIKE 1 AND subcode NOT LIKE
'%BS%';
Output:

8. How many credits each are assigned to the subjects taught in 3rd year (List the subject
names and the corresponding credits).
SQL> SELECT description, credits FROM subjects WHERE year LIKE 3;
Output:

9. List the subject names and the corresponding code of those, which end with a ‘S’.
SQL> select subcode, description
2 from subjects
3 where rtrim(ltrim(description)) like '%s';
Output:
10. List the subjects across the entire course, except 1st year, where the credit assigned
is ‘3’.
SQL> select description from subjects where year not like 1 AND credits LIKE 3;
Output:

11. Display the data of the courses with credit value ‘2’ and are taught either in 1st or 3rd
year.
SQL> select * from subjects where (year like 1 OR year like 3) AND credits LIKE 2;
Output:

12. Change the course credit of Java Programing to 4.


SQL> update subjects set credits = 4 where subcode = '5CS07';
Output:

13. Change subject name ‘Advanced Calculus’ to ‘Engineering Calculus’.


SQL> UPDATE subjects SET description = 'Engineering Calculus' WHERE subcode
= '5BS11';
Output:

14. Delete the course taught by the Mechanical Engineering department (Hint: The subject
code has ME).
SQL> delete from subjects where subcode like '%ME%';
Output:

15. Write a query to generate the following output for each row in the table:
Example: ‘5CS13 – Data Mining is offered to the student of Computer Science in 3rd
year, 2nd semester. It has 4 credits’.
SQL> select
2 subcode || ' – ' || description || ' is offered to the student of ' ||
3 case
4 when year = 1 then 'First'
5 when year = 2 then 'Second'
6 when year = 3 then 'Third'
7 when year = 4 then 'Fourth'
8 end ||
9 ' year, ' ||
10 case
11 when sem = 1 then '1st'
12 when sem = 2 then '2nd'
13 end ||
14 ' semester. It has ' || credits || ' credits.' as output
15 from subjects;

Output:

16. Delete the 2nd year data from the table.


SQL> delete from subjects
2 where year like 2;
Output:
17. Replace ‘Management Science’ as a single word in the data. i.e, as ‘Mgmt.Science’.
SQL> UPDATE subjects
2 SET description = 'Mgmt.Science'
3 WHERE subcode = '5BS42';
Output:

18. Remove the structure of the Subjects table


SQL> drop table subjects;
Output:

You might also like