SQL Query Practice - Library and Football League Databases
Library Information System (LIS) - 15 Questions
1. Retrieve the names of all students along with their department names.
2. Find the total number of books issued by each member.
3. List all books that have never been issued.
4. Retrieve the names of authors who have written more than 5 books.
5. Get the student details who have issued the maximum number of books.
6. Find the total number of books issued in the current year.
7. Retrieve the names of all faculty members who have never issued a book.
8. List all books along with the number of copies available.
9. Find the most frequently issued book.
10. Retrieve the names of members who have issued books beyond their allowed quota.
11. Get the details of books issued but not yet returned.
12. Retrieve the details of the department having the highest number of students.
13. Find students who have never issued a book.
14. List all members and their maximum allowed book quota.
15. Find the total number of books issued by each department.
Football League Information System (FLIS) - 15 Questions
16. Retrieve the names of all teams and their cities.
17. Find the total number of matches played by each team.
18. List the referees who have officiated more than 5 matches.
19. Get the details of the team that has won the most matches.
20. Find all matches where the host team scored more than 3 goals.
21. Retrieve the names of players who have played in more than 10 matches.
22. Find teams that have never lost a match.
23. Get the details of referees who have never officiated a match.
24. Retrieve the names of players along with their teams.
25. List the managers who have been managing their team for more than 5 years.
26. Find teams that have both home and away jersey colors the same.
27. Retrieve all match details where the guest team won.
28. Find the player who has played for the maximum number of teams.
29. Get the details of matches where the score difference is 2 or more.
30. Find the youngest player in the league.
SQL Solutions
Library Information System (LIS) - Solutions
1. SELECT s.student_fname, s.student_lname, d.department_name FROM students s JOIN
departments d ON s.department_code = d.department_code;
2. SELECT member_no, COUNT(*) AS total_books_issued FROM book_issue GROUP BY
member_no;
3. SELECT * FROM book_catalogue WHERE ISBN_no NOT IN (SELECT ISBN_no FROM
book_copies WHERE accession_no IN (SELECT accession_no FROM book_issue));
4. SELECT author_fname, author_lname, COUNT(*) AS book_count FROM book_authors GROUP
BY author_fname, author_lname HAVING COUNT(*) > 5;
5. SELECT member_no, COUNT(*) AS issued_books FROM book_issue GROUP BY member_no
ORDER BY issued_books DESC LIMIT 1;
6. SELECT COUNT(*) FROM book_issue WHERE YEAR(doi) = YEAR(CURRENT_DATE());
7. SELECT f.faculty_fname, f.faculty_lname FROM faculty f LEFT JOIN members m ON f.id = m.id
LEFT JOIN book_issue b ON m.member_no = b.member_no WHERE b.member_no IS NULL;
8. SELECT c.ISBN_no, COUNT(*) AS copies_available FROM book_copies c LEFT JOIN
book_issue b ON c.accession_no = b.accession_no WHERE b.accession_no IS NULL GROUP BY
c.ISBN_no;
9. SELECT accession_no, COUNT(*) AS issue_count FROM book_issue GROUP BY accession_no
ORDER BY issue_count DESC LIMIT 1;
10. SELECT member_no FROM book_issue GROUP BY member_no HAVING COUNT(*) >
(SELECT max_books FROM quota WHERE member_type = (SELECT member_type FROM
members WHERE members.member_no = book_issue.member_no));
Football League Information System (FLIS) - Solutions
16. SELECT name, city FROM teams;
17. SELECT team_id, COUNT(*) AS matches_played FROM matches GROUP BY team_id;
18. SELECT referee, COUNT(*) AS matches_officiated FROM match_referees GROUP BY referee
HAVING COUNT(*) > 5;
19. SELECT host_team_id, COUNT(*) AS wins FROM matches WHERE host_team_score >
guest_team_score GROUP BY host_team_id ORDER BY wins DESC LIMIT 1;
20. SELECT * FROM matches WHERE host_team_score > 3;
21. SELECT p.name FROM players p JOIN matches m ON p.team_id = m.host_team_id OR
p.team_id = m.guest_team_id GROUP BY p.name HAVING COUNT(*) > 10;
22. SELECT team_id FROM teams WHERE team_id NOT IN (SELECT guest_team_id FROM
matches WHERE guest_team_score > host_team_score);
23. SELECT r.name FROM referees r LEFT JOIN match_referees mr ON r.referee_id = mr.referee
WHERE mr.referee IS NULL;
24. SELECT p.name, t.name FROM players p JOIN teams t ON p.team_id = t.team_id;
25. SELECT name FROM managers WHERE since <= DATE_SUB(CURRENT_DATE(),
INTERVAL 5 YEAR);
26. SELECT * FROM teams WHERE jersey_home_color = jersey_away_color;
27. SELECT * FROM matches WHERE guest_team_score > host_team_score;