DBMS Week2 Exercixe
DBMS Week2 Exercixe
SUBJECTS
Schema:
Subjects (subcode, description, year, sem, credits)
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
subcode char(5),
description char(25),
year number(1),
sem number(1),
credits number(1)
);
Queries:
1. Change the data type of description column to varchar.
SQL> alter table subjects
2 modify description varchar(25);
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:
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:
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:
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: