Create Library DB from scratch go through these queries & appear for interview !

Create Library DB from scratch go through these queries & appear for interview !

Create Database Library Management by following queries.

Create Database Library Management;
use [Library Management] 
drop table books 
drop table staff 
drop table department 
drop table student 
drop table book_status

Create these tables in the sequence given below

--books
create table books( 
isbn varchar(50) primary key,
Book_name varchar(100),
rack_no varchar(50),
author varchar(50),
book_edition int)

--staff 
create table staff(
sid varchar(50) primary key,
name varchar(100),
designation varchar(50),
phone varchar(10))

--department
create table department( 
dept_id varchar(50) primary key,
dept_name varchar(100),
HOD varchar(100))

--student 
create table student ( 
stud_id varchar(50) primary key,
name varchar(100),
address varchar(50),
dept_id varchar(50) foreign key references department(dept_id))

--book_status 
create table book_status ( 
issue_id varchar(50) primary key, 
stud_id varchar(50) foreign key references student(stud_id),
issue_date date, 
book_id varchar(50) foreign key references books (isbn),
ssuing_staff_id varchar(50) foreign key references staff(sid))

Now Insert the data in the above-created tables using the insert queries given below.

-- Inserting data into the 'books' table 
INSERT INTO books (isbn, Book_name, rack_no, author, book_edition)
VALUES ('9780345339683', 'The Hobbit', 'A1', 'J.R.R. Tolkien', 1),
('9780061124952', 'To Kill a Mockingbird', 'B2', 'Harper Lee', 1),
('9780140449334', 'Pride and Prejudice', 'C3', 'Jane Austen', 1),
('9780060935467', 'The Alchemist', 'D4', 'Paulo Coelho', 1);

-- Inserting data into the 'staff' table
INSERT INTO staff (sid, name, designation, phone)
VALUES ('S001', 'John Smith', 'Librarian', '1234567890'),
('S002', 'Emily Johnson', 'Assistant Librarian', '9876543210'),
('S003', 'Michael Davis', 'Library Assistant', '5551234567');

-- Inserting data into the 'department' table
INSERT INTO department (dept_id, dept_name, HOD) VALUES
('D001', 'Computer Science', 'Dr. Robert Johnson'),
('D002', 'English Literature', 'Dr. Sarah Thompson'),
('D003', 'History', 'Dr. David Wilson');

-- Inserting data into the 'student' table
INSERT INTO student (stud_id, name, address, dept_id) VALUES 
('ST001', 'Alice Smith', '123 Main St', 'D001'), 
('ST002', 'Bob Johnson', '456 Elm St', 'D001'), 
('ST003', 'Catherine Davis', '789 Oak St', 'D002'), 
('ST004', 'David Wilson', '987 Pine St', 'D003');

-- Inserting data into the 'book_status' table
 INSERT INTO book_status (issue_id, stud_id, issue_date, book_id, issuing_staff_id)
 VALUES ('ISS001', 'ST001', '2023-06-15', '9780345339683', 'S001'), 
('ISS002', 'ST002', '2023-06-16', '9780061124952', 'S001'), 
('ISS003', 'ST003', '2023-06-17', '9780140449334', 'S002'), 
('ISS004', 'ST004', '2023-06-18', '9780060935467', 'S003');

Now Since the data has been inserted into all the tables of the database we can proceed further to query the multiple SQL problems that will help you prepare for the interview.

  1. Retrieve the names of all the students along with their corresponding department names.
select a.name, b.dept_name 
from student a 
join department b 
on a.dept_id = b.dept_id;
  1. Find the book names and authors for all the books that are currently issued to students.

     select book_name, author 
     from books a join 
     book_status b on a.isbn = b.book_id
    
  2. Get the names of the staff members who issued books to students along with the corresponding student names.

     select b.name as staff_name , c.name as student_name 
     from book_status a 
     inner join staff b on a.issuing_staff_id = b.sid 
     inner join student c on a.stud_id = c.stud_id
    
  3. Retrieve the book names, authors, and issue dates for all the books issued by a specific staff member.

     select a.Book_name, a.author , b.issue_date, c.name as staff_name 
     from books a 
     inner join book_status b on a.isbn = b.book_id 
     inner join staff c on b.issuing_staff_id = c.sid
    
  4. Find the student names and their corresponding department names for the students who have issued books.

     select a.name, a.dept_id, c.dept_name 
     from student a 
     inner join book_status b on a.stud_id = b.stud_id 
     inner join department c on a.dept_id = c.dept_id
    
  5. Get the names of the staff members along with their designations who have issued books to students from the 'Computer Science' department.

     select a.name, a.designation 
     from staff a 
     inner join book_status b on a.sid = b.issuing_staff_id
     inner join student c on b.stud_id = c.stud_id 
     inner join department d on d.dept_id = c.dept_id 
     where d.dept_name = 'Computer Science'
    
  6. Retrieve the names of all the students who have not been issued any books.

     SELECT s.name 
     FROM student s LEFT JOIN book_status bs ON s.stud_id = bs.stud_id 
     WHERE bs.stud_id IS NULL;
    
  7. Find the book names and authors for the books that are currently not issued to any student.

     SELECT b.Book_name, b.author FROM books b
     LEFT JOIN book_status bs ON b.isbn = bs.book_id 
     WHERE bs.book_id IS NULL;
    
  8. Get the names of the staff members who have not issued any books.

     select a.name from staff a 
     left join book_status b on a.sid = b.issuing_staff_id;
    
  9. Retrieve the student names, book names, and issue dates for the books issued by staff members whose designation is 'Librarian'.

     select a.name, d.Book_name, c.issue_date 
     from student a 
     inner join book_status c on a.stud_id=c.stud_id 
     inner join staff b on c.issuing_staff_id = b.sid 
     inner join books d on d.isbn = c.book_id 
     where b.designation = 'Librarian'
    

These 10 queries will give you a good start for refreshing you sql topics. Please comment some questions that can be added here.

Thank You!

Did you find this article valuable?

Support The Analyst Geek by becoming a sponsor. Any amount is appreciated!