Major links



Quicklinks


📌 Quick Links
[ DBMS ] [ SQL ] [ DDB ] [ ML ] [ DL ] [ NLP ] [ DSA ] [ PDB ] [ DWDM ] [ Quizzes ]


Friday, February 19, 2016

SQL Exercise with Solutions

SQL Exercise with Solution / Exercises involving JOIN, Subqueries, Group By, Having, etc

Figure 4.6 - A relational database schema for a library database - Taken from Fundamentals of database systems by Elmasri and Navathe
[A] Write the SQL DDL statements to define this database. Include appropriate domains, constraints and referential triggered actions.
[B] Write the SQL queries for the questions given below;



  1. How many copies of the book titled The Lost Tribe are owned by the library branch whose name is "Sharpstown"?
  2. How many copies of the book titled The Lost Tribe are owned by each library branch?
  3. Retrieve the names of all borrowers who do not have any books checked out .
  4. 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.
  5. For each library branch, retrieve the branch name and the total number of books loaned out from that branch.
  6. Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out.
  7. For each book authored (or co-authored) by "Stephen King", retrieve the title and the number of copies owned by the library branch whose name is "Central"

Answers

[A]




CREATE TABLE Book (

Book_id Int PRIMARY KEY,

Title Varchar(200),

Publisher_name Varchar(200),

FOREIGN KEY (Publisher_name) REFERENCES Publisher(Name)ON DELETE SET NULL ON UPDATE CASCADE

);



CREATE TABLE Book_Authors (

Book_id Int NOT NULL,

Author_name Varchar(200) NOT NULL,

PRIMARY KEY (Book_id, Author_name),

FOREIGN KEY (Book_id) REFERENCES Book(Book_id)

ON DELETE CASCADE ON UPDATE CASCADE

);



CREATE TABLE Publisher (

Name Varchar(200) PRIMARY KEY,

Address Varchar(400),

Phone Decimal(20)

);



CREATE TABLE Book_Copies (

Book_id Int NOT NULL,

Branch_id Char(4) NOT NULL,

No_of_copies Int DEFAULT 1,

PRIMARY KEY (Book_id, Branch_id),

FOREIGN KEY (Book_id) REFERENCES Book(Book_id)

ON DELETE CASCADE ON UPDATE CASCADE,

FOREIGN KEY (Branch_id) REFERENCES Library_Branch(Branch_id)

ON DELETE CASCADE ON UPDATE CASCADE

);



CREATE TABLE Book_Loans (

Book_id Int NOT NULL,

Branch_id Char(4) NOT NULL,

Card_no Int NOT NULL,

Date_out Date,

Due_date Date,

PRIMARY KEY (Book_id, Branch_id, Card_no),

FOREIGN KEY (Book_id) REFERENCES Book(Book_id)

ON DELETE RESTRICT ON UPDATE CASCADE,

FOREIGN KEY (Branch_id) REFERENCES Library_Branch(Branch_id)

ON DELETE RESTRICT ON UPDATE CASCADE,

FOREIGN KEY (Card_no) REFERENCES Borrower(Card_no)

ON DELETE RESTRICT ON UPDATE CASCADE

);



CREATE TABLE Library_Branch (

Branch_id Char(4) PRIMARY KEY,

Branch_name Varchar(200) NOT NULL,

Address Varchar(400)

);



CREATE TABLE Borrower (

Card_no Int PRIMARY KEY,

Name Varchar(200) NOT NULL,

Address Varchar(400),

Phone Decimal(20)

);





[B]

(1)



Solution 1:
SELECT bc.No_Of_Copies
FROM BOOK b, BOOK_COPIES bc, LIBRARY_BRANCH bl
WHERE         b.BookId = bc.BookId AND
                        bc.BranchId = bl.BranchId AND
Title='The Lost Tribe' AND BranchName='Sharpstown';

Solution 2:
SELECT No_Of_Copies
FROM ((BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN
LIBRARY_BRANCH )
WHERE Title='The Lost Tribe' AND BranchName='Sharpstown';





(2)

        SELECT BranchName, No_Of_Copies
FROM ((BOOK NATURAL JOIN BOOK_COPIES ) NATURAL JOIN
LIBRARY_BRANCH )
WHERE Title='The Lost Tribe';
 



(3)



Solution 1:
SELECT        Name
FROM                        BORROWER B
WHERE         CardNo NOT IN (SELECT CardNo
                                                      FROM BOOK_LOANS );
Solution 2:
SELECT        Name
FROM                        BORROWER B
WHERE         NOT EXISTS (SELECT *
                                          FROM BOOK_LOANS L
                                                WHERE B.CardNo = L.CardNo );

(4)


            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


(5)


            SELECT        L.BranchName, COUNT(*)
FROM                        LIBRARY_BRANCH L, BOOK_LOANS BL
WHERE         BL.BranchId = L.BranchId
GROUP BY   L.BranchName;


(6)


            SELECT        B.Name, B.Address, COUNT(*)
FROM                        BORROWER B, BOOK_LOANS L
WHERE         B.CardNo = L.CardNo
GROUP BY   B.CardNo, B.Name, B.Address
HAVING        COUNT(*) > 5;


(7)



SELECT        Title, No_Of_Copies 
FROM   (((BOOK_AUTHORS NATURAL JOIN BOOK) NATURAL JOIN 
BOOK_COPIES) NATURAL JOIN LIBRARY_BRANCH)WHERE 
Author_Name='Stephen King' AND BranchName='Central';




Thursday, February 18, 2016

CS 9221 / CS 921 Database Technology question paper – Nov/Dec 2013



CS 9221 / CS 921 Database Technology question paper – Nov/Dec 2013 / Anna University Previous Year Exam Questions for ME CSE / Anna University Previous Year M.E. M.Tech. Computer Science and Information Technology Question Papers / CS 9221 / CS 921 Database Technology question paper – Nov/Dec 2013 for ME Computer and Communication Engineering






Question Paper Code : 81328
M.E. DEGREE EXAMINATION, NOVEMBER/DECEMBER 2013
Second Semester
Computer Science and Engineering
CS 9221 / CS 921 — DATABASE TECHNOLOGY
(Common to M.E. Computer and Communication)

(Regulation 2009)

Time: Three hours                                                       Maximum: 100 Marks
Answer ALL Questions

PART A — (10 × 2 = 20 Marks)

1. What is database?
2. Define concurrency control.
3. Mention about the object oriented databases.
4. What is a query language?
5. Define server model.
6. What is meant by web databases?
7. Mention about ER model.
8. What are spatial databases?
9. Define knowledge bases.
10. What is meant by Text database?


PART B — (5 × 16 = 80 Marks)

11. (a) (i) Describe in detail about the distributed databases and conventional databases. (6)
(ii) Illustrate the architecture of distributed databases. (10)

Or

(b) Briefly explain about the query processing and transaction processing in distributed databases. (16)

12. (a) Discuss in detail about the modeling and design of the object oriented databases. (16)
Or

(b) (i) Explain about multi version locks. (8)
(ii) Explain OQL with suitable examples. (8)

13. (a) (i) Illustrate the architecture of Data warehousing. (10)
(ii) List the applications of data mining. (6)

Or

(b) (i) Discuss in detail about the web databases. (8)
(ii) Illustrate the mobile transaction models. (8)

14. (a) (i) Explain in detail about the normalization in databases. (10)
(ii) List the factors to be considered to tune the database. (6)

Or

(b) (i) Describe in detail about the query optimization. (8)
(ii) Write short notes on spatial databases. (8)

15. (a) Briefly discuss about Active and Deductive databases. (16)

Or

(b)  (i) Describe the architecture of multimedia databases. (10)
(ii) Give the different architectural models of parallel databases. (6)

_____________________________

Please visit, subscribe and share 10 Minutes Lectures in Computer Science

Featured Content

Multiple choice questions in Natural Language Processing Home

MCQ in Natural Language Processing, Quiz questions with answers in NLP, Top interview questions in NLP with answers Multiple Choice Que...

All time most popular contents