0% found this document useful (0 votes)
42 views21 pages

22MCA10062

The document contains SQL queries that create tables and insert data for students, subjects they are enrolled in, and information about the subjects. It then lists 13 questions and provides the relational algebra and SQL queries to answer each question by joining the tables and filtering on conditions.

Uploaded by

rahul kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views21 pages

22MCA10062

The document contains SQL queries that create tables and insert data for students, subjects they are enrolled in, and information about the subjects. It then lists 13 questions and provides the relational algebra and SQL queries to answer each question by joining the tables and filtering on conditions.

Uploaded by

rahul kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

22MCA10062

Rahul Kumar
Sql queries:

CREATE TABLE student (id int, name varchar (n));

INSERT INTO student (id, name)

VALUES (1234, ‘joe’);

INSERT INTO student (id, name)

VALUES (4000, ‘hector’);

INSERT INTO student (id, name)

VALUES (2000, ‘ling’);


CREATE TABLE enrolledIn (id int, code varchar (n));

INSERT INTO enrolledIn (id, code)

VALUES (1234, ‘cs1500’);

INSERT INTO enrolledIn (id, code)

VALUES (1234, ‘cs1200’);

INSERT INTO enrolledIn (id, code)

VALUES (1234, ‘cs2001’);

INSERT INTO enrolledIn (id, code)

VALUES (4000, ‘cs3010’);

INSERT INTO enrolledIn (id, code)

VALUES (4000, ‘ma3000’);

CREATE TABLE subject (code varchar (n), lecturer varchar (n));

INSERT INTO subject (code, lecturer)

VALUES (‘cs1500’, ‘curtis’);

INSERT INTO subject (code, lecturer)

VALUES (‘cs2001’, ‘dave’);

INSERT INTO subject (code, lecturer)

VALUES (‘cs3010’, ‘curtis’);

INSERT INTO subject (code, lecturer)

VALUES (‘cs2001’, ‘olivier’);

INSERT INTO subject (code, lecturer)

VALUES (‘cs3000’, ‘roger’);

Figure out which relational algebra operations were used to obtain


each of the following tables.
1.
name
joe
hector
ling

Relational algebra: πname(student)


SQL Code: SELECT name FROM student

2.
Lecturer
---------
curtis
dave
olivier
roger

Relational Algebra: πlecturer(subject)


SQL Code: SELECT lecturer FROM subject

3.
code | lecturer
---------------------
cs3010 | curtis
cs1500 | Curtis

Relational Algebra: σlecturer='curtis'(subject)


SQL Code: SELECT * FROM subject
WHERE lecturer = 'curtis';

4.
id | name
------------------
1234 | joe
4000 | hector

Relational Algebra: σid=1234 or id=4000(student)


SQL Code: SELECT * FROM student
WHERE id=1234 OR id=4000;
5.
id | name | id | code
--------------------------------------
1234 | joe | 1234 | cs1500
1234 | joe | 1234 | cs1200
1234 | joe | 1234 | cs2001
1234 | joe | 4000 | cs3010
1234 | joe | 4000 | ma3000

Relational Algebra: σname='joe'(student⨯enrolledIn)


SQL Code: SELECT * FROM student CROSS JOIN enrolledIn
WHERE name='joe';

6.
id | name | id | code
--------------------------------------
1234 | joe | 1234 | cs1500
1234 | joe | 1234 | cs1200
1234 | joe | 1234 | cs2001

Relational Algebra: σname='joe'(student⟗enrolledIn)


SQL Code: select * from student full outer join enrolledIn
on student.id=enrolledIn.id
where name='joe';

7.
id | name | code
----------------------------
1234 | joe | cs1500
1234 | joe | cs1200
1234 | joe | cs2001

Relational Algebra: σname='joe'(student⨝enrolledIn)


SQL Code: select * from student natural join enrolledIn
where name='joe';

8.
id | code
-------------------
1234 | cs1500
1234 | cs1200
1234 | cs2001
Relational Algebra: σid=1234(enrolledIn)
SQL Code: select * from enrolledIn
where id=1234

9.
id | name | code | lecturer
--------------------------------------------------
4000 | hector | cs3010 | curtis
4000 | hector | ma3000 | roger
Relational Algebra: σid=4000(student⨝enrolledIn⨝subject)
SQL Code: select * from student natural join
enrolledIn natural join
subject
where id=4000

10.
name | lecturer
-------------------------
joe | curtis
hector | curtis

Relational Algebra:
πname,lecturer(σlecturer='curtis'(student⨝enrolledIn⨝subject))
SQL Code: select name, lecturer from
student natural join
enrolledIn natural join
subject
where lecturer='curtis';

1. What are the names of students enrolled in cs3020?


Relational Algebra: πname(σcode='cs3020'(student⨯enrolledIn))
Sql Code: select name from student cross join enrolledIn
where code='cs3020';
2. Which subjects is hector taking?
Relational Algebra: πcode(σname='hector'(student⨝enrolledIn))
SQL Code: select code from student natural join enrolledIn
where name='hector';
3. Who teaches cs1500?
Relational Algebra: πlecturer(σcode='cs1500'(subject))
SQL Code: select lecturer from subject
where code='cs1500';

4. Who teaches cs1500 or cs3020?


Relational Algebra: πlecturer(σcode='cs1500' or code='cs3020'(subject))
SQL Code: select lecturer from subject
where code='cs1500' or code='cs3020';
5. Who teaches at least two different subjects?

6. What are the names of students in cs1500 or cs3010?

Relational Algebra: πname(σcode='cs1500' or code='cs3010'(student⨝enrolledIn))


SQL Code: select name from student natural join enrolledIn
where code='cs1500' or code='cs3010';
7. What are the names of students in both cs1500 and cs1200?
Relational Algebra: πname(σcode='cs1500' or code='cs1200'(student⨝enrolledIn))
SQL Code: select name from student natural join enrolledIn
where code='cs1500' or code='cs1200';

8. What are the names of students in at least two different subjects?


9. What are the codes of all the subjects taught?
Relational Algebra: πcode(subject)
SQL Code: select code from subject

10. What are the names of all the students?


Relational algebra: πname(student)
SQL Code: select name from student
11. What are the names of all the students in cs1500?
Relational Algebra: πname(σcode='cs1500'(student⨝enrolledIn))
SQL Code: select name from student natural join enrolledIn
where code='cs1500';

12. What are the names of students taking a subject taught by


Roger.
Relational algebra: πname(σlecturer='roger'(student⨝enrolledIn⨝subject))
SQL Code: select name from student natural join enrolledIn natural join
subject
where lecturer='roger';
13. What are the names of students who are taking a subject
not taught by Roger?
Relational algebra: πname(σlecturer≠'roger'(student⨝enrolledIn⨝subject))
SQL Code: select name from student natural join enrolledIn natural join
subject
where lecturer!='roger';

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy