3.29

Using the university schema, write an SQL query to find the name and ID of each History student whose name begins with the letter ā€˜Dā€™ and who has not taken at least five Music courses.


SELECT id,name
FROM student AS s
WHERE dept_name = 'History' 
    AND name LIKE 'D%'
    AND (
        SELECT COUNT(DISTINCT course_id)
        FROM takes
        WHERE takes.id = s.id AND 
            course_id IN (SELECT course_id FROM course WHERE dept_name = 'Music')
    ) < 5;