0% found this document useful (0 votes)
12 views2 pages

Q 3

The document contains SQL queries to perform inner joins, left/right/full outer joins, subqueries, and group by on tables of students and subjects. It finds the minimum and maximum marks obtained by a student with their name, subject name and percentage. It also finds the total marks of each subject.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Q 3

The document contains SQL queries to perform inner joins, left/right/full outer joins, subqueries, and group by on tables of students and subjects. It finds the minimum and maximum marks obtained by a student with their name, subject name and percentage. It also finds the total marks of each subject.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

# Q - 3 : Perform different query like (inner join, left/right/full outer join,

subquery, group by) in above create table.

# Find min and maximum mark obtain student with subject name,student name, marks,
percentage
# Find total marks of each subject.

CREATE TABLE IF NOT EXISTS Student(


Id SERIAL PRIMARY KEY,
Name VARCHAR(255),
Percentage REAL);

CREATE TABLE IF NOT EXISTS Subject(


Id SERIAL PRIMARY KEY,
Name VARCHAR(255),
Student_id INTEGER REFERENCES Student(id),
Marks INTEGER);

INSERT INTO Student (Name, Percentage) VALUES ('Kapil', 85.5);


INSERT INTO Student (Name, Percentage) VALUES ('Karan', 78.9);
INSERT INTO Student (Name, Percentage) VALUES ('Rahul', 92.3);
INSERT INTO Student (Name, Percentage) VALUES ('Soham', 79.8);
INSERT INTO Student (Name, Percentage) VALUES ('Rohit', 88.2);
INSERT INTO Student (Id, Name, Percentage) VALUES (7, 'Ridham', 87.8);
INSERT INTO Student (Id, Name, Percentage) VALUES (9, 'Darshil', 94.2);

INSERT INTO Subject (Id, Name, Student_id, Marks) VALUES (1, 'Math', 1, 90);
INSERT INTO Subject (Id, Name, Student_id, Marks) VALUES (3, 'Science', 2, 85);
INSERT INTO Subject (Id, Name, Student_id, Marks) VALUES (4, 'History', 4, 82);
INSERT INTO Subject (Id, Name, Student_id, Marks) VALUES (5, 'Geography', 5, 91);
INSERT INTO Subject (Id, Name, Student_id, Marks) VALUES (7, 'English', 3, 88);

# Inner Join
SELECT Subject.Name As SubjectName, Student.Name As StudentName, Subject.Marks,
Student.Percentage
FROM Student
INNER JOIN Subject ON Student.ID = Subject.ID;

# Left Outer Join


SELECT s.Name AS Student_Name, su.Name AS Subject_Name, su.Marks, s.Percentage
FROM Student s
LEFT JOIN Subject su ON s.Id = su.Student_id;

# Right Outer Join


SELECT s.Name AS Student_Name, su.Name AS Subject_Name, su.Marks, s.Percentage
FROM Student s
RIGHT JOIN Subject su ON s.Id = su.Student_id;

# Full Outer Join


SELECT s.Name AS Student_Name, su.Name AS Subject_Name, su.Marks, s.Percentage
FROM Student s
FULL OUTER JOIN Subject su ON s.Id = su.Student_id;

# Sub Querry
SELECT s.Name AS Student_Name, su.Name AS Subject_Name, su.Marks, s.Percentage
FROM Student s
INNER JOIN Subject su ON s.Id = su.Student_id
WHERE su.Marks = (SELECT MIN(Marks) FROM Subject) OR su.Marks = (SELECT MAX(Marks)
FROM Subject);

# Group by
SELECT su.Name AS Subject_Name, SUM(su.Marks) AS Total_Marks
FROM Subject su
GROUP BY su.Name;

You might also like