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

Module4 RA

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Module4 RA

Uploaded by

justice.chitra.v
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Relational Algebra

Studen
t course Stu_crs
PK PK FK FK
cnam
regno sname cgpa ccode e credits regno ccode
shivra CSE200 CSE200
101 m 9.7 4 DBMS 4 101 4
CSE200 CSE200
102 surya 9.5 3 DS 4 102 4
CSE300 CSE200
103 pranay 9.9 2 IWP 4 103 4
SWE30 DBM CSE200
104 sadhun 9.5 05 S 3 104 4
CSE200
105 kumar 9.4 105 4
CSE200
104 3
CSE200
105 3

RA and SQL
Select (Row) σ
1. List the name of the student with cgpa
Select student details
Select * from student;
σ (course)

2. List the course details with credits 3


select * from course where credit=3;
σ credit =3 (course)

3. List the students with more than 9.5 cgpa


Select * from student where cgpa>9.5;
σ cgpa>9.5 (student )

4. List the students with either 9.5 or 9.7 as cgpa


Select * from student where cgpa=9.5 or cgpa=9.7;
σ cgpa=9.5 ˅cgpa=9.7 (student ) v

4. List the students with 9.5 and the name ends with n
Select * from student where cgpa=9.5 and name like “%n”;
σ cgpa=9.5 ˄name like %n (student )

Project (∏) – Selection of columns


1. Select the name of the course
select cname from course;
∏cname (course)

2.List the name and cgpa from student;


Select name, cgpa from student;
∏name , cgpa (student )

3.List the regno with the ccode registered by the student


Select regno, ccode from stu_crs;
∏regno ,ccode (stucrs )

Select and Project: ∏(σ)


1. List the regno of the student who has 9.9 as cgpa

Student
PK
regno sname cgpa
101 shivram 9.7
102 surya 9.5
103 pranay 9.9
104 sadhun 9.5
105 kumar 9.4

103 pranay 9.9

select regno from student where cgpa=9.9;


∏regno (σ ¿¿ cgpa=9.9 (student ))¿

2. List the ccode with name of the courses which has 4 credits
Select cname from course where credit=4;
∏ccode ,cname ¿ ¿

Aggregation(§)

Order:
Select
From
Where
Group by
Having
Studen
t course Stu_crs
PK PK FK FK
cnam
regno sname cgpa ccode e credits regno ccode
shivra CSE200 CSE200
101 m 9.7 4 DBMS 4 101 4
CSE200 CSE200
102 surya 9.5 3 DS 4 102 4
CSE300 CSE200
103 pranay 9.9 2 IWP 4 103 4
SWE300 CSE200
104 sadhun 9.5 5 DBMS 3 104 4
CSE200
105 kumar 9.4 105 4
CSE200
104 3
CSE200
105 3

1. Count the number of students


select count(regno) from student;
§ count (regno )(student )

2. Select the maximum cgpa


select max(cgpa) from student;
§ max ⁡(cgpa) (student )
3. select the regno with min cgpa
Select regno from student where cgpa=(Select min(cgpa) from student);
Select regno from student where cgpa=9.4

105 kumar 9.4

π regno (σ cgpa=(§ min ( cgpa)


( student ) ) (student ))❑

4. select the min cgpa of a student whose name starts with s


Select min(cgpa) from student where name like “s%”;
§ min (cgpa ) (σ name like s% ( student ))

Group by
5. List the number of courses for each credit
select credits, count(ccode) from course group by credits;
❑credits §count (ccode ), credits ( course )

6. List the number of courses registered by each student

Stu_crs
FK FK
regno ccode
101 CSE2004
102 CSE2004
103 CSE2004
104 CSE2004
105 CSE2004
104 CSE2003
105 CSE2003

101 1
102 1
103 1
104 2
105 2
select count(ccode), regno from stu_crs group by regno;
❑regno §count ( ccode ) ,regno ( stucrs )

7. List the number of courses registered by each student except 105


select count(ccode), regno from stu_crs where regno<>105 group by
regno;
❑regno §count ( ccode ) ,regno (σ regno≠105 ( stucrs ) )

Stu_crs
FK FK
regno ccode
101 CSE2004
102 CSE2004
103 CSE2004
104 CSE2004
105 CSE2004
104 CSE2003
105 CSE2003

101 1
102 1
103 1
104 2
105 2

104 2
105 2
Having – condition for aggregation function
8. List the number of courses registered by each student if, the count of
the courses is greater than one
select count(ccode), regno
from stu_crs
group by regno
having count(ccode)>1;
❑regno §count ( ccode ) ,regno (σ count (ccode )>1 ( stucrs ) )

9. List the number of courses registered by each student except 105 if,
the count of the courses is greater than one
select count(ccode), regno
from stu_crs
where regno<>105
group by regno
having count(ccode)>1;
❑regno §count ( ccode ) ,regno (σ count (ccode )>1 ,regno ≠105 ( stucrs ) )

You might also like