1. Write an SQL query that returns the studentid and name of students who have c
ID: 3593305 • Letter: 1
Question
1. Write an SQL query that returns the studentid and name of students who have checked out books that are worth more than $100 in total.
2. Write an SQL query that returns the studentid and name of students who checked out the textbook of a course that they did not enroll in.
Explanation / Answer
The below SQL statements satisfies the given requirements as follows:
---------------------------------------------------------------------------------------------------
1. Write an SQL query that returns the studentid and name of students who have checked out books that are worth more than $100 in total.
--Here we are joining the three tables and calculating the sum.
select studentid,name from
(select studentid,name,sum(price) as sum from
(SELECT student.studentid,student.name,book_checkout.bookid,book.price
FROM book_checkout
INNER JOIN student ON student.studentid=book_checkout.studentid
INNER JOIN book ON book.bookid=book_checkout.bookid) group by name)where sum>=100
Output:-
---------------
studentid name
4 Kris
2. Write an SQL query that returns the studentid and name of students who checked out the textbook of a course that they did not enroll in.
--Here we are joining the three tables and calculating the ids which are not matching with the course ids.
SELECT distinct
studentid,name from
(SELECT student.studentid,student.name,book_checkout.bookid,enroll.courseid,enroll.courseid as cour
FROM book_checkout
INNER JOIN student ON student.studentid=book_checkout.studentid
INNER JOIN enroll ON student.studentid=enroll.studentid
INNER JOIN enroll ON textbookid=book_checkout.bookid) where bookid<>cour
Output:-
-------------
studentid name
4 Kris
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.