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

Q1: Specify The Following Queries On The Database of Figure (1) Bellow in SQL

The document contains two questions about SQL queries on databases. Question 1 specifies 7 queries on a database of a library catalog system. Question 2 specifies 6 queries on a database of a university student information system. The queries retrieve information such as the number of copies of books owned by branches, names of borrowers without loans, book titles and borrower information for overdue loans, and more.

Uploaded by

madhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Q1: Specify The Following Queries On The Database of Figure (1) Bellow in SQL

The document contains two questions about SQL queries on databases. Question 1 specifies 7 queries on a database of a library catalog system. Question 2 specifies 6 queries on a database of a university student information system. The queries retrieve information such as the number of copies of books owned by branches, names of borrowers without loans, book titles and borrower information for overdue loans, and more.

Uploaded by

madhav
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DataBase Discussion ENG.

GHADIR AL JARO

Q1: Specify the following queries on the database of Figure (1) bellow in SQL:

Figure 1

a. How many copies of the book titled The Lost Tribe are owned by the library branch whose name is
'Sharpstown'?

SELECT NoOfCopies

FROM ( (BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN LIBRARY_BRANCH )

WHERE Title='The Lost Tribe' AND BranchName='Sharpstown'


DataBase Discussion ENG. GHADIR AL JARO

b. How many copies of the book titled The Lost Tribe are owned by each library branch?

SELECT BranchName, NoOfCopies

FROM ( (BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN LIBRARY_BRANCH )

WHERE Title='The Lost Tribe'

c. Retrieve the names of all borrowers who do not have any books checked out.

SELECT Name

FROM BORROWER B

WHERE NOT EXIST ( SELECT *

FROM BOOK_LOANS L

WHERE B.CardNo = L.CardNo ).

d. For each book that is loaned out from the 'Sharpstown' branch and whose DueDate is today,

retrieve the book title, the borrower's name, and the borrower's address.

SELECT B.Title, R.Name, R.Address

FROM BOOK B, BORROWER R, BOOK_LOANS BL, LIBRARY_BRANCH LB

WHERE LB.BranchName='Sharpstown' AND LB.BranchId=BL.BranchId AND

BL.DueDate='today' AND BL.CardNo=R.CardNo AND BL.BookId=B.BookId

e. For each library branch, retrieve the branch name and the total number of books loaned out

from that branch.

SELECT L.BranchName, COUNT(*)

FROM BOOK_LOANS B, LIBRARY_BRANCH L

WHERE B.BranchId = L.BranchId

GROUP BY L.BranchName
DataBase Discussion ENG. GHADIR AL JARO

f. Retrieve the names, addresses, and number of books checked out for all borrowers who have

more than five books checked out.

SELECT B.Name, B.Address, COUNT(*)

FROM BORROWER B, BOOK_LOANS L

WHERE B.CardNo = L.CardNo

GROUP BY B.CardNo

HAVING COUNT(*) > 5

g. For each book authored (or coauthored) by 'Stephen King,' retrieve the title and the number of
copies owned by the library branch whose name is 'Central.'

SELECT TItle, NoOfCopies

FROM ( ( (BOOK_AUTHORS NATURAL JOIN BOOK)

NATURAL JOIN BOOK_COPIES)

NATURAL JOIN LIBRARY_BRANCH)

WHERE Author_Name = 'Stephen King' and BranchName = 'Central


DataBase Discussion ENG. GHADIR AL JARO

Q2: Specify the following queries on the database of Figure (2) bellow in SQL:

Figure 2
DataBase Discussion ENG. GHADIR AL JARO

a. Retrieve the names of all senior students majoring in 'COSC' (computer science).

SELECT Name

FROM STUDENT

WHERE Major='COSC'

b. Retrieve the names of all courses taught by professor King in 85 and 86.

SELECT CourseName

FROM COURSE, SECTION

WHERE COURSE.CourseNumber=SECTION.CourseNumber AND Instructor='King'

AND (Year='85' OR Year='86')

Another possible SQL query uses nesting as follows:

SELECT CourseName

FROM COURSE

WHERE CourseNumber IN ( SELECT CourseNumber

FROM SECTION

WHERE Instructor='King' AND (Year='85' OR Year='86') )

c. For each section taught by professor King, retrieve the course number, semester, year, and number
of students who took the section.

SELECT CourseNumber, Semester, Year, COUNT(*)

FROM SECTION, GRADE_REPORT

WHERE Instructor='King' AND SECTION.SectionIdentifier=GRADE_REPORT.SectionIdentifier

GROUP BY CourseNumber, Semester, Year


DataBase Discussion ENG. GHADIR AL JARO

d. Retrieve the name and transcript of each senior student (Class=5) majoring in COSC. Transcript
includes course name, course number, credit hours, semester, year, and grade for each course
completed by the student.

SELECT Name, CourseName, C.CourseNumber, CreditHours, Semester, Year, Grade

FROM STUDENT ST, COURSE C, SECTION S, GRADE_REPORT G

WHERE Class=5 AND Major='COSC' AND ST.StudentNumber=G.StudentNumber AND

G.SectionIdentifier=S.SectionIdentifier AND S.CourseNumber=C.CourseNumber

e. Retrieve the names and major departments of all straight A students (students who have a grade
of A in all their courses).

SELECT Name, Major

FROM STUDENT

WHERE NOT EXISTS ( SELECT *

FROM GRADE_REPORT

WHERE StudentNumber= STUDENT.StudentNumber AND NOT(Grade='A'))

f. Retrieve the names and major departments of all students who do not have any grade of A in any
of their courses.

SELECT Name, Major

FROM STUDENT

WHERE NOT EXISTS ( SELECT *

FROM GRADE_REPORT

WHERE StudentNumber= STUDENT.StudentNumber AND Grade='A' )

You might also like