0% found this document useful (0 votes)
127 views6 pages

SQL Server Question Paper - 5

The document contains SQL scripts to create tables (Class, Student, Marks), insert data into the tables, and write 10 queries using different join types and subqueries. The tables store student, class, and marks data with the goal of querying and analyzing the data in different ways such as finding the highest total marks by class/section or counting students by class/section.

Uploaded by

api-3766129
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
127 views6 pages

SQL Server Question Paper - 5

The document contains SQL scripts to create tables (Class, Student, Marks), insert data into the tables, and write 10 queries using different join types and subqueries. The tables store student, class, and marks data with the goal of querying and analyzing the data in different ways such as finding the highest total marks by class/section or counting students by class/section.

Uploaded by

api-3766129
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

if exists (select * from dbo.

sysobjects where id = object_id(N'[Class]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Class]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[Student]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Student]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[Marks]') and


OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [Marks]
GO

CREATE TABLE Class


(
ClassID INT PRIMARY KEY,
ClassName VARCHAR(10),
[Section] VARCHAR(1)
)
GO

CREATE TABLE Student


(
StudentID INT PRIMARY KEY,
StudentName VARCHAR(10),
ClassID INT FOREIGN KEY REFERENCES Class(ClassID)
)
GO

CREATE TABLE Marks


(
StudentID INT FOREIGN KEY REFERENCES Student(StudentID),
Marks1 DECIMAL(5,2),
Marks2 DECIMAL(5,2),
Marks3 DECIMAL(5,2)
)
GO

--Class
INSERT INTO Class (ClassID, ClassName, [Section])
VALUES(1, 'First', 'A')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(2, 'First', 'B')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(3, 'Second', 'A')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(4, 'Second', 'B')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(5, 'Third', 'A')

INSERT INTO Class (ClassID, ClassName, [Section])


VALUES(6, 'Third', 'B')

--Student Records
INSERT INTO Student(StudentID, StudentName, ClassID)
VALUES(1, 'Stud1', 1)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(2, 'Stud2', 1)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(3, 'Stud3', 2)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(4, 'Stud4', 1)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(5, 'Stud5', 2)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(6, 'Stud6', 3)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(7, 'Stud7', 4)

INSERT INTO Student(StudentID, StudentName, ClassID)


VALUES(8, 'Stud8', 5)

INSERT INTO Student(StudentID, StudentName)


VALUES(9, 'Stud9')
-- Marks Records

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(1, 73, 72, 74)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(2, 63, 62, 64)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(3, 43, 60, 60)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(4, 53, 50, 55)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(5, 83, 42, 44)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(6, 60, 60, 60)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(7, 63, 92, 94)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(8, 83, 42, 94)

INSERT INTO Marks(StudentID, Marks1, Marks2, Marks3)


VALUES(9, 43, 42, 42)

Write the following Queries using (Inner, Outer, Self, SubQueries, Inline Views)

1. Display StudentID, StudentName, ClassName, [Section] using Student, Class Table


2. Display All Students with the following columns StudentID, StudentName, ClassName,
[Section] using Student, Class Table
3. Display All Classes with the following columns StudentID, StudentName, ClassName,
[Section] using Student, Class Table
4. Display All Classes and Students with the following columns StudentID, StudentName,
ClassName, [Section] using Student, Class Table
5. Display No. of Students in Each Class and Section using Student, Class Table
6. Display Total Marks in Each Class and Section using Student, Class, Marks Table
Where Total = Marks1+Marks2+Marks3
7. Display following columns
StudentName, ClassName, Section, Total
Who got more than 180
Where Total = Marks1+Marks2+Marks3
8. Display Student Records who are studying in Class "First" and Section "A" (Hint: Using Sub
Query or Exists)
9. Display Following columns
StudentName, ClassName, [Section], Marks1, Max(Marks1)
in Each Class and Section
Hint: Using Inline Views
Order By ClassName, Section
10. Display Following columns
StudentName, ClassName, [Section], Total, Max(Total), HighestStudentName
in Each Class and Section
(HighestStudentName: Name of the StudentName who got Max Total in each Class & Section)
Total = Marks1 + Marks2 + Mark3
Hint: Using Inline Views
Order By ClassName, Section

--1
SELECT StudentID, StudentName, ClassName, C.[Section] FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)

--2
SELECT StudentID, StudentName, ClassName, C.[Section] FROM Student S
LEFT OUTER JOIN Class C ON (C.ClassID = S.ClassID)

--3
SELECT StudentID, StudentName, ClassName, C.[Section] FROM Student S
RIGHT OUTER JOIN Class C ON (C.ClassID = S.ClassID)

--OR

SELECT StudentID, StudentName, C.ClassName, C.[Section] FROM Class C


LEFT OUTER JOIN Student S ON (S.ClassID = C.ClassID)
--4
SELECT StudentID, StudentName, C.ClassName, C.[Section] FROM Class C
FULL OUTER JOIN Student S ON (S.ClassID = C.ClassID)

--5
SELECT ClassName, [Section], COUNT(*) FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
GROUP BY ClassName, [Section]

--6
SELECT ClassName, [Section], SUM(Marks1+Marks2+Marks3) FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
GROUP BY ClassName, [Section]

--7
SELECT ClassName, [Section], Marks1+Marks2+Marks3 FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
WHERE Marks1+Marks2+Marks3 > 180

--8
SELECT * FROM Student S
WHERE S.ClassID IN (SELECT ClassID FROM Class C WHERE C.ClassName =
'First' AND C.[Section] = 'A')

--OR

SELECT * FROM Student S


WHERE EXISTS (SELECT ClassID FROM Class C WHERE C.ClassName = 'First'
AND C.[Section] = 'A' AND S.ClassID = C.ClassID)

--9
SELECT
StudentName, ClassName, [Section],
Marks1, MaxMarks1
FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
INNER JOIN
(
SELECT C.ClassID, MAX(Marks1) MaxMarks1
FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
GROUP BY C.ClassID
) M1 ON (M1.ClassID = C.ClassID)
ORDER BY ClassName, [Section]

--10
SELECT
S.StudentName, ClassName, [Section],
Marks1+Marks2+Marks3 Total, MaxTotal, T1.StudentName
FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
INNER JOIN
(
SELECT C.ClassID, MAX(Marks1+Marks2+Marks3) MaxTotal
FROM Student S
INNER JOIN Class C ON (C.ClassID = S.ClassID)
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
GROUP BY C.ClassID
) M1 ON (M1.ClassID = C.ClassID)
INNER JOIN
(
SELECT StudentName, ClassID, Marks1+Marks2+Marks3 Total
FROM Student S
INNER JOIN Marks M ON (M.StudentID = S.StudentID)
) T1 ON (T1.ClassID = M1.ClassID AND M1.MaxTotal = T1.Total)
ORDER BY ClassName, [Section]

You might also like