**Họ và Tên:** Lê Phước Phát **Lớp:** 22CLC10 **MSSV:** 22127322 **Ngày:** 19/03/2024 --- <center><p> <b>BÀI ÔN TẬP SQL & ĐSQH </b> </p> <p> <b>CSDL: Quản lý sản xuất phim</b> </p></center> **Trả lời các câu truy vấn sau bằng ngôn ngữ Đại số quan hệ và SQL Server** 1. Cho biết tên những bộ phim được sản xuất trong khoảng thời gian từ năm $2009$ đến $2010$ * Ngôn ngữ SQL: ```sql= select ph.TenPhim from PHIM ph where ph.NamSanXuat between 2009 and 2010 ``` * Đại số quan hệ: $$\text{KQ} \gets \Pi_{\text{TenPhim}}(\sigma_{\text{NamSanXuat $\geq$ 2009 $\wedge$ NamSanXuat $\leq$ 2010}}(\text{PHIM}))$$ 2. Cho biết tên những bộ phim mà có thời lượng $> 120$ phút * Ngôn ngữ SQL: ```sql= select ph.TenPhim from PHIM ph where ph.Dodai > 120 ``` * Đại số quan hệ: $$\text{KQ} \gets \Pi_{\text{TenPhim}}(\sigma_{\text{Dodai > 120}}(\text{PHIM}))$$ 3. Cho biết tên bộ phim và tên đạo diễn của những bộ phim được sản xuất trong năm $2008$. * Ngôn ngữ SQL: ```sql= select ph.TenPhim as [TenPhim], dd.HoTen as [TenDaoDien] from PHIM ph join DAODIEN dd on dd.MaDaoDien = ph.MaDaoDien where ph.NamSanXuat = 2008 ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{PHIM} \bowtie \text{DAODIEN}$ * $\text{KQ(TenPhim, TenDaoDien)} \gets \Pi_{\text{TenPhim, HoTen}}(\sigma_{\text{NamSanXuat = 2008}}({\bf r_1}))$ 4. Cho biết tên phim, tên đạo diễn và tên thể loại của phim “Bình minh trên biển” * Ngôn ngữ SQL: ```sql= select ph.TenPhim, dd.HoTen, tl.TenTheLoai from PHIM ph join DAODIEN dd on dd.MaDaoDien = ph.MaDaoDien join THELOAI tl on tl.MaTheLoai = ph.MaTheLoai where ph.TenPhim = N'Bình minh trên biển' ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{PHIM} \bowtie \text{DAODIEN}$ * ${\bf r_2} \gets {\bf r_1} \bowtie \text{THELOAI}$ * $\text{KQ} \gets \Pi_{\text{TenPhim, HoTen, TenTheLoai}}(\sigma_{\text{TenPhim = 'Bình minh trên biển'}}({\bf r_2}))$ 5. Cho biết mã những thể loại phim mà chưa được sản xuất lần nào. * Ngôn ngữ SQL: ```sql= select MaTheLoai from THELOAI except select MaTheLoai from PHIM ``` * Đại số quan hệ: * ${\bf r_1} \gets \Pi_{\text{MaTheLoai}}(\text{PHIM})$ * $\text{KQ} \gets \Pi_{\text{MaTheLoai}}(\text{THELOAI}) - {\bf r_1}$ 6. Cho biết mã và tên những thể loại phim chưa được sản xuất lần nào * Ngôn ngữ SQL: ```sql= select tl.MaTheLoai, tl.TenTheLoai from THELOAI tl where tl.MaTheLoai not in (select ph.MaTheLoai from PHIM ph) ``` * Đại số quan hệ: * ${\bf r_1} \gets \Pi_{\text{MaTheLoai, TenTheLoai}}(\text{PHIM})$ * $\text{KQ} \gets \Pi_{\text{MaTheLoai, TenTheLoai}}(\text{THELOAI}) - {\bf r_1}$ 7. Cho biết tên những diễn viên có địa chỉ tại thành phố “Hồ Chí Minh” * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDienVien] from DIENVIEN dv where dv.DiaChi = N'Hồ Chí Minh' ``` * Đại số quan hệ: $$\text{KQ(TenDienVien)} \gets\Pi_{\text{HoTen}}(\sigma_{\text{DiaChi = N'Hồ Chí Minh'}}(\text{DIENVIEN}))$$ 8. Cho biết tên diễn viên và tên thể loại sở trường của những diễn viên nữ * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDienVien], tl.TenTheLoai as [TenTheLoai] from DIENVIEN dv join THELOAI tl on tl.MaTheLoai = dv.TheLoaiSoTruong where dv.Phai = N'Nữ' ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DIENVIEN} \bowtie_{\text{TheLoaiSoTruong = MaTheLoai}}\text{THELOAI}$ * $\text{KQ(TenDienVien, TenTheLoai)} \gets \Pi_{\text{HoTen, TenTheLoai}}(\sigma_{\text{Phai=N'Nữ'}}({\bf r_1}))$ 9. Cho biết tên và địa chỉ của những diễn viên và đạo diễn có năm sinh trong khoảng $1970$ đến $1980$. * Ngôn ngữ SQL: ```sql= select HoTen, DiaChi from DIENVIEN where NamSinh between 1970 and 1980 union select HoTen, DiaChi from DAODIEN where NamSinh between 1970 and 1980 ``` * Đại số quan hệ: $$\Pi_{\text{HoTen, DiaChi}}(\sigma_{\text{NamSinh} \geq 1970 \wedge \text{NamSinh} \leq 1980}(\text{DIENVIEN}))$$$$\cup \Pi_{\text{HoTen, DiaChi}}(\sigma_{\text{NamSinh} \geq 1970 \wedge \text{NamSinh} \leq 1980}(\text{DAODIEN}))$$ 10. Cho biết mã diễn viên và thu nhập từ việc đóng phim của mỗi diễn viên * Ngôn ngữ SQL: ```sql= select dp.MaDienVien, SUM(dp.DonGia * dp.SoGio) as [ThuNhap] from DONGPHIM dp group by dp.MaDienVien ``` * Đại số quan hệ: $$\text{KQ(MaDienVien, ThuNhap)}\gets _{\text{MaDienVien}}\mathfrak{J}_{\text{SUM(DonGia * SoGio)}}(\text{DONGPHIM})$$ 11. Cho biết tên diễn viên và thu nhập từ việc đóng phim của mỗi diễn viên * Ngôn ngữ SQL: ```sql= select dv.HoTen, SUM(dp.DonGia * dp.SoGio) as [ThuNhap] from DIENVIEN dv join DONGPHIM dp on dp.MaDienVien = dv.MaDienVien group by dv.HoTen ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DIENVIEN} \bowtie \text{DONGPHIM}$ * $\text{KQ(TenDienVien, ThuNhap)}\gets _{\text{HoTen}}\mathfrak{J}_{\text{SUM(DonGia * SoGio)}}({\bf r_1})$ 12. Với mỗi diễn viên cho biết mã diễn viên và số bộ phim mà diễn viên đó đóng. * Ngôn ngữ SQL: ```sql= select dp.MaDienVien, count(dp.MaPhim) as [SLP] from DONGPHIM dp group by dp.MaDienVien ``` * Đại số quan hệ: * $\text{KQ(MaDienVien, SLP)} \gets _{\text{MaDienVien}}\mathfrak{J}_{\text{COUNT(MaPhim)}}(\text{DONGPHIM})$ 13. Với mỗi diễn viên, cho biết tên diễn viên và số bộ phim mà diễn viên đó đóng * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDV],count(dp.MaPhim) as [SLP] from DIENVIEN dv left join DONGPHIM dp on dp.MaDienVien = dv.MaDienVien group by dv.HoTen ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DIENVIEN} ⟕ \text{DONGPHIM}$ * $\text{KQ(TenDV, SLP)} \gets _{\text{HoTen}}\mathfrak{J}_{\text{COUNT(MaPhim)}}({\bf r_1})$ 14. Cho biết những mã diễn viên có số lần đóng là từ $2$ bộ phim trở lên * Ngôn ngữ SQL: ```sql= select dp.MaDienVien from DONGPHIM dp group by dp.MaDienVien having count(dp.MaPhim) >= 2 ``` * Đại số quan hệ: * ${\bf r_1}(\text{MaDienVien, SLP}) \gets _{\text{MaDienVien}}\mathfrak{J}_{\text{COUNT(MaPhim)}}(\text{DONGPHIM})$ * $\text{KQ} \gets \Pi_{\text{MaDienVien}}(\sigma_{\text{SLP} \geq 2}({\bf{r_1}}))$ 15. Cho biết tên những diễn viên đã đóng từ $2$ bộ phim trở lên * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDienVien] from DIENVIEN dv left join DONGPHIM dp on dp.MaDienVien = dv.MaDienVien group by dv.HoTen having count(dp.MaPhim) >=2 ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DIENVIEN} ⟕ \text{DONGPHIM}$ * ${\bf r_2}(\text{TenDienVien, SLP}) \gets _{\text{HoTen}}\mathfrak{J}_{\text{COUNT(MaPhim)}}({\bf r_1})$ * $\text{KQ} \gets \Pi_{\text{TenDienVien}}(\sigma_{\text{SLP} \geq 2}({\bf r_2}))$ 16. Cho biết tên diễn viên và tên thể loại phim sở trường của diễn viên của những diễn viên đã đóng từ 2 bộ phim trở lên * Ngôn ngữ SQL: ```sql= select dv1.HoTen, tl.TenTheLoai from DIENVIEN dv1 join THELOAI tl on tl.MaTheLoai = dv1.TheLoaiSoTruong where dv1.MaDienVien IN ( select dv.MaDienVien from DIENVIEN dv join DONGPHIM dp on dp.MaDienVien = dv.MaDienVien group by dv.MaDienVien having count(dp.MaPhim) >= 2 ) ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DIENVIEN} \bowtie \text{DONGPHIM}$ * ${\bf r_2}(\text{MaDienVien, SLP}) \gets _{\text{MaDienVien}}\mathfrak{J}_{\text{COUNT(MaPhim)}}({\bf r_1})$ * ${\bf r_3} \gets {\bf r_2} \bowtie_{{\bf r_2}\text{.MaDienVien = DIENVIEN.MaDienVien}} \text{DIENVIEN}$ * ${\bf r_4} \gets {\bf r_3} \bowtie_{{\bf r_3}\text{.TheLoaiSoTruong = THELOAI.MaTheLoai}} \text{THELOAI}$ * $\text{KQ(TenDienVien, TenTheLoai)} \gets \Pi_{\text{HoTen, TenTheLoai}}(\sigma_{\text{SLP} \geq 2}({\bf r_4}))$ 17. Với mỗi vai trò trong phim, cho biết đã có bao nhiêu diễn viên tham gia * Ngôn ngữ SQL: ```sql= select MaPhim, VaiTro, count(*) as [SoDV] from DONGPHIM group by MaPhim, VaiTro ``` * Đại số quan hệ:$$\text{KQ(MaPhim, VaiTro, SoDV)} \gets _{\text{MaPhim, VaiTro}}\mathfrak{J}_{\text{COUNT(*)}}(\text{DONGPHIM})$$ 18. Với mỗi phim cho biết mã phim và số lượng diễn viên tham gia * Ngôn ngữ SQL: ```sql= select MaPhim, count(*) as [SoDV] from DONGPHIM group by MaPhim ``` * Đại số quan hệ: $$\text{KQ(MaPhim, SoDV)} \gets _{\text{MaPhim}}\mathfrak{J}_{\text{COUNT(*)}}(\text{DONGPHIM})$$ 19. Với mỗi phim cho biết tên phim, tên đạo diễn và số lượng diễn viên tham gia * Ngôn ngữ SQL: ```sql= select ph.TenPhim, dd.HoTen as [TenDaoDien], count(*) as [SoDV] from DONGPHIM dp join PHIM ph on ph.MaPhim = dp.MaPhim join DAODIEN dd on dd.MaDaoDien = ph.MaDaoDien group by ph.TenPhim, dd.HoTen ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DONGPHIM} \bowtie \text{PHIM}$ * ${\bf r_2} \gets {\bf r_1} \bowtie \text{DAODIEN}$ * $\text{KQ(TenPhim, TenDaoDien, SoDV)} \gets _{\text{TenPhim, HoTen}}\mathfrak{J}_{\text{COUNT(*)}}({\bf r_2})$ 20. Cho biết mã phim có nhiều diễn viên tham gia nhất * Ngôn ngữ SQL: ```sql= select dp.MaPhim from DONGPHIM dp group by dp.MaPhim having count (*) >= ALL( select count(*) from DONGPHIM dp1 group by dp1.MaPhim ) ``` * Đại số quan hệ: * ${\bf r_1}(\text{MaPhim, SoDV}) \gets _{\text{MaPhim}}\mathfrak{J}_{\text{COUNT(*)}}(\text{DONGPHIM})$ * ${\bf r_2}(\text{Max_SoDV}) \gets \mathfrak{J}_{\text{MAX(SoDV)}}({\bf r_1})$ * $\text{KQ} \gets \Pi_{\text{MaPhim}}({\bf r_1} \bowtie_{{\bf r_1}\text{.SoDV = }{\bf r_2}\text{.Max_SoDV}} {\bf r_2})$ 21. Cho biết tên phim và tên đạo diễn mà có nhiều diễn viên tham gia nhất * Ngôn ngữ SQL: ```sql= select distinct ph.TenPhim, dd.HoTen as [TenDaoDien] from DONGPHIM dp join PHIM ph on ph.MaPhim = dp.MaPhim join DAODIEN dd on dd.MaDaoDien = ph.MaDaoDien group by ph.MaPhim, ph.TenPhim, dd.MaDaoDien, dd.HoTen having count (*) >= ALL( select count(*) from DONGPHIM dp1 group by dp1.MaPhim ) ``` * Đại số quan hệ: * ${\bf r_1} \gets (\text{DONGPHIM} \bowtie \text{PHIM}) \bowtie \text{DAODIEN}$ * ${\bf r_2}(\text{TenPhim, HoTen, SoDV}) \gets _{\text{MaPhim, TenPhim, MaDaoDien, HoTen}}\mathfrak{J}_{\text{COUNT(*)}}({\bf r_1})$ * ${\bf r_3}(\text{Max_SoDV}) \gets \mathfrak{J}_{\text{MAX(SoDV)}}({\bf r_2})$ * $\text{KQ(TenPhim, TenDaoDien)} \gets \Pi_{\text{TenPhim, HoTen}}({\bf r_3} \bowtie_{{\bf r_3}\text{.Max_SoDV} = {\bf r_3}\text{.SoDV}} {\bf r_2})$ 22. Cho biết tên những diễn viên không tham gia vào bộ phim “Bình minh trên biển” * Ngôn ngữ SQL: ```sql= select distinct dv.HoTen as [TenDienVien] from DIENVIEN dv where dv.MaDienVien NOT IN ( select dv1.MaDienVien from PHIM ph1 join DONGPHIM dp1 on dp1.MaPhim = ph1.MaPhim join DIENVIEN dv1 on dv1.MaDienVien = dp1.MaDienVien where ph1.TenPhim = N'Bình minh trên biển' ) ``` * Đại số quan hệ: * ${\bf r_1} \gets \Pi_{\text{MaDienVien}}(\text{DIENVIEN})$ * ${\bf r_2} \gets \text{DONGPHIM} \bowtie \text{PHIM}$ * ${\bf r_3} \gets {\bf r_2} \bowtie \text{DIENVIEN}$ * ${\bf r_4} \gets \Pi_{\text{MaDienVien, HoTen}}(\sigma_{\text{TenPhim = 'Bình minh trên biển'}}({\bf r_3}))$ * $\text{KQ(TenDienVien)} \gets \Pi_{\text{HoTen}}({\bf r_1} - {\bf r_4})$ 23. Cho biết những diễn viên đã từng đóng những phim có thể loại là “Phim truyền hình” * Ngôn ngữ SQL: ```sql= select distinct dv.HoTen as [TenDienVien] from DONGPHIM dp join DIENVIEN dv on dp.MaDienVien = dv.MaDienVien join PHIM ph on ph.MaPhim = dp.MaPhim join THELOAI tl on tl.MaTheLoai= ph.MaTheLoai where tl.TenTheLoai = N'Phim truyền hình' ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DONGPHIM} \bowtie \text{DIENVIEN}$ * ${\bf r_2} \gets {\bf r_1} \bowtie \text{PHIM}$ * ${\bf r_3} \gets {\bf r_2} \bowtie \text{THELOAI}$ * $\text{KQ(TenDienVien)} \gets \Pi_{\text{HoTen}}(\sigma_{\text{TenTheLoai = 'Phim truyền hình'}}({\bf r_3}))$ 24. Cho biết tên những diễn viên chưa từng đóng phim có thể loại là “Phim truyền hình” * Ngôn ngữ SQL: ```sql= select distinct dv.MaDienVien, dv.HoTen as [TenDV] from DIENVIEN dv where dv.MaDienVien NOT IN ( select distinct dv1.MaDienVien from DONGPHIM dp1 join DIENVIEN dv1 on dp1.MaDienVien = dv1.MaDienVien join PHIM ph1 on ph1.MaPhim = dp1.MaPhim join THELOAI tl1 on tl1.MaTheLoai= ph1.MaTheLoai where tl1.TenTheLoai = N'Phim truyền hình' ) ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DONGPHIM} \bowtie \text{DIENVIEN}$ * ${\bf r_2} \gets {\bf r_1} \bowtie \text{PHIM}$ * ${\bf r_3} \gets {\bf r_2} \bowtie \text{THELOAI}$ * ${\bf r_4} \gets \Pi_{\text{MaDienVien, HoTen}}(\sigma_{\text{TenTheLoai = 'Phim truyền hình'}}({\bf r_3}))$ * $\text{KQ(MaDienVien, TenDV)} \gets \Pi_{\text{MaDienVien, HoTen}}(\text{DIENVIEN}) - {\bf r_4}$ 25. Cho biết tên những phim mà có đạo diễn phim là một người ở “Hồ Chí Minh” và có diễn viên tham gia đóng phim cũng ở “Hồ Chí Minh” * Ngôn ngữ SQL: ```sql= select ph.TenPhim from DONGPHIM dp join PHIM ph on ph.MaPhim = dp.MaPhim join DIENVIEN dv on dv.MaDienVien = dp.MaDienVien join DAODIEN dd on dd.MaDaoDien = ph.MaDaoDien where dv.DiaChi = N'Hồ Chí Minh' and dd.DiaChi = N'Hồ Chí Minh' ``` * Đại số quan hệ: * ${\bf r_1} \gets \Pi_{\text{MaDienVien}}(\sigma_{\text{DiaChi = 'Hồ Chí Minh'}}(\text{DAODIEN}))$ * ${\bf r_2} \gets \Pi_{\text{MaDaoDien}}(\sigma_{\text{DiaChi = 'Hồ Chí Minh'}}(\text{DIENVIEN}))$ * ${\bf r_3} \gets \text{PHIM} \bowtie \text{DONGPHIM}$ * $\text{KQ} \gets \Pi_{\text{TenPhim}}(({\bf r_3} \bowtie {\bf r_1}) \bowtie {\bf r_2})$ 26. Cho biết những diễn viên nhỏ tuổi hơn diễn viên “Lý Mạnh Hùng” * Ngôn ngữ SQL: ```sql= select distinct dv.* from DIENVIEN dv where dv.NamSinh > ( select dv1.NamSinh from DIENVIEN dv1 where dv1.HoTen = N'Lý Mạnh Hùng' ) ``` * Đại số quan hệ: * ${\bf r_1}(\text{NS}) \gets \Pi_{\text{NamSinh}}(\sigma_{\text{HoTen = 'Lý Mạnh Hùng'}}(\text{DIENVIEN}))$ * $\text{KQ} \gets \sigma_{\text{DIENVIEN.NamSinh > }{\bf r_1}\text{.NS}}(\text{DIENVIEN} \times {\bf r_1})$ 27. Cho biết tên diễn viên nhỏ tuổi nhất * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDienVien] from DIENVIEN dv where dv.NamSinh >= ALL( select dv1.NamSinh from DIENVIEN dv1 ) ``` * Đại số quan hệ: * ${\bf r_1}(\text{Max_NamSinh}) \gets \mathfrak{J}_{\text{MAX(NamSinh)}}(\text{DIENVIEN})$ * $\text{KQ(TenDienVien)} \gets \Pi_{\text{HoTen}}(\text{DIENVIEN} \bowtie_{\text{DIENVIEN.NamSinh = }{\bf r_1}\text{.Max_NamSinh}} {\bf r_1})$ 28. Cho biết số lượng phim mà có độ dài từ $120$ phút trở lên * Ngôn ngữ SQL: ```sql= select count(*) as [SLP] from PHIM ph where ph.Dodai >= 120 ``` * Đại số quan hệ: * ${\bf r_1} \gets \sigma_{\text{Dodai} \geq 120}(\text{PHIM})$ * $\text{KQ(SLP)} \gets \mathfrak{J}_{\text{COUNT(*)}}({\bf r_1})$ 29. Cho biết số lượng phim có thể loại phim “Phim hành động” * Ngôn ngữ SQL: ```sql= select count(*) as [SLP] from PHIM ph join THELOAI tl on tl.MaTheLoai = ph.MaTheLoai where tl.TenTheLoai = N'Phim hành động' ``` * Đại số quan hệ: * ${\bf r_1} \gets \sigma_{\text{TenTheLoai = 'Phim hành động'}}(\text{PHIM} \bowtie \text{THELOAI})$ * $\text{KQ(SLP)} \gets \mathfrak{J}_{\text{COUNT(*)}}({\bf r_1})$ 30. Cho biết tên những đạo diễn và diễn viên ở “Hồ Chí Minh” * Ngôn ngữ SQL: ```sql= select dv.HoTen from DIENVIEN dv where dv.DiaChi = N'Hồ Chí Minh' UNION select dd.HoTen from DAODIEN dd where dd.DiaChi = N'Hồ Chí Minh' ``` * Đại số quan hệ: * ${\bf r_1} \gets \Pi_{\text{HoTen}}(\sigma_{\text{DiaChi = 'Hồ Chí Minh'}}(\text{DIENVIEN}))$ * ${\bf r_2} \gets \Pi_{\text{HoTen}}(\sigma_{\text{DiaChi = 'Hồ Chí Minh'}}(\text{DAODIEN}))$ * $\text{KQ} \gets {\bf r_1} \cup {\bf r_2}$ 31. Cho biết tên những diễn viên mà chưa từng đóng phim. * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDV] from DIENVIEN dv where dv.MaDienVien NOT IN ( select dp.MaDienVien from DONGPHIM dp ) ``` * Đại số quan hệ: * ${\bf r_1} \gets \Pi_{\text{HoTen}}(\text{DIENVIEN})$ * ${\bf r_2} \gets \Pi_{\text{HoTen}}(\text{DONGPHIM} \bowtie \text{DIENVIEN})$ * $\text{KQ(TenDV)} \gets \Pi_{\text{HoTen}}({\bf r_1} - {\bf r_2})$ 32. Cho biết mã phim có chi phí thuê diễn viên cao nhất. * Ngôn ngữ SQL: ```sql= select dp.MaPhim from DONGPHIM dp group by dp.MaPhim having sum(dp.SoGio * dp.DonGia) >= ALL ( select sum(dp1.SoGio * dp1.DonGia) from DONGPHIM dp1 group by dp1.MaPhim ) ``` * Đại số quan hệ: * ${\bf r_1}\text{(MaPhim, ChiPhiThue)} \gets _{\text{MaPhim}}\mathfrak{J}_{\text{SUM(SoGio * DonGia)}}(\text{DONGPHIM})$ * ${\bf r_2}\text{(Max_ChiPhiThue)} \gets \mathfrak{J}_{\text{MAX(ChiPhiThue)}}({\bf r_1})$ * $\text{KQ} \gets \Pi_{\text{MaPhim}}({\bf r_1} \bowtie_{{\bf r_1}\text{.ChiPhiThue = }{\bf r_2}\text{.Max_ChiPhiThue}} {\bf r_2})$ 33. Cho biết tên phim và tên đạo diễn có chi phí thuê diễn viên ==cao nhất==. * Ngôn ngữ SQL: ```sql= select ph.TenPhim, dd.HoTen as [TenDaoDien] from DONGPHIM dp join PHIM ph on dp.MaPhim = ph.MaPhim join DAODIEN dd on dd.MaDaoDien = ph.MaDaoDien group by ph.MaPhim, ph.TenPhim, dd.MaDaoDien, dd.HoTen having sum(dp.SoGio * dp.DonGia) >= ALL ( select sum(dp1.SoGio * dp1.DonGia) from DONGPHIM dp1 join PHIM ph1 on dp1.MaPhim = ph1.MaPhim join DAODIEN dd1 on dd1.MaDaoDien = ph1.MaDaoDien group by ph1.MaPhim, ph1.TenPhim, dd1.MaDaoDien, dd1.HoTen ) ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DONGPHIM} \bowtie \text{PHIM}$ * ${\bf r_2} \gets {\bf r_1} \bowtie \text{DAODIEN}$ * ${\bf r_3}(\text{TenPhim, HoTen, CPT}) \gets _{\text{TenPhim, HoTen}}\mathfrak{J}_{\text{SUM(DonGia * SoGio)}}({\bf r_2})$ * ${\bf r_4}(\text{Max_CPT}) \gets \mathfrak{J}_{\text{MAX(CPT)}}({\bf r_3})$ * $\text{KQ(TenPhim, TenDaoDien)} \gets \Pi_{\text{TenPhim, HoTen}}({\bf r_3} \bowtie_{{\bf r_3}\text{.CPT = }{\bf r_4}\text{.Max_CPT}} {\bf r_4})$ 34. Cho biết tên những diễn viên có tham gia đóng phim “Bình minh trên biển” * Ngôn ngữ SQL: ```sql= select dv.HoTen as [TenDV] from DONGPHIM dp join DIENVIEN dv on dv.MaDienVien = dp.MaDienVien join PHIM ph on ph.MaPhim = dp.MaPhim where ph.TenPhim = N'Bình minh trên biển' ``` * Đại số quan hệ: * ${\bf r_1} \gets (\text{DONGPHIM} \bowtie \text{DIENVIEN}) \bowtie \text{PHIM}$ * $\text{KQ(TenDV)} \gets \Pi_{\text{HoTen}}(\sigma_{\text{TenPhim = 'Bình minh trên biển'}}({\bf r_1}))$ 35. Cho biết tên phim mà có chi phí đóng phim cao nhất. * Ngôn ngữ SQL: ```sql= select ph.TenPhim from DONGPHIM dp join PHIM ph on dp.MaPhim = ph.MaPhim group by ph.MaPhim, ph.TenPhim having sum(dp.SoGio * dp.DonGia) >= ALL ( select sum(dp1.SoGio * dp1.DonGia) from DONGPHIM dp1 join PHIM ph1 on dp1.MaPhim = ph1.MaPhim group by ph1.MaPhim, ph1.TenPhim ) ``` * Đại số quan hệ: * ${\bf r_1} \gets \text{DONGPHIM} \bowtie \text{PHIM}$ * ${\bf r_2}(\text{TenPhim, ChiPhiDP}) \gets _{\text{MaPhim, TenPhim}}\mathfrak{J}_{\text{SUM(DonGia * SoGio)}}({\bf r_1})$ * ${\bf r_3}(\text{Max_CPDP}) \gets \mathfrak{J}_{\text{MAX(ChiPhiDP)}}({\bf r_2})$ * $\text{KQ} \gets \Pi_{\text{TenPhim}}({\bf r_2} \bowtie_{{\bf r_2}\text{.ChiPhiDP = } {\bf r_3}\text{.Max_CPDP}} {\bf r_3})$ 36. Cho biết tên phim mà có chi phí thuê diễn viên cao hơn chi phí thuê diễn viên của bộ phim “Ở cuối chân trời” * Ngôn ngữ SQL: ```sql= select ph.TenPhim from DONGPHIM dp join PHIM ph on ph.MaPhim = dp.MaPhim group by ph.MaPhim, ph.TenPhim having sum(dp.SoGio * dp.DonGia) > ( select sum(dp1.SoGio * dp1.DonGia) from DONGPHIM dp1 join PHIM ph1 on ph1.MaPhim = dp1.MaPhim where ph1.TenPhim = N'Ở cuối chân trời' group by ph1.MaPhim, ph1.TenPhim ) ``` * Đại số quan hệ * ${\bf r_1} \gets \sigma_{\text{TenPhim = 'Ở cuối chân trời'}}(\text{DONGPHIM} \bowtie \text{PHIM})$ * ${\bf r_2}(\text{CP1}) \gets _{\text{MaPhim, TenPhim}}\mathfrak{J}_{\text{SUM(DonGia * SoGio)}}({\bf r_1})$ * ${\bf r_3}(\text{MaPhim, TenPhim, CP2}) \gets _{\text{MaPhim, TenPhim}}\mathfrak{J}_{\text{SUM(DonGia * SoGio)}}(\text{DONGPHIM} \bowtie \text{PHIM})$ * $\text{KQ} \gets \Pi_{\text{TenPhim}}(\sigma_{{\bf r_3}\text{.CP2} > {\bf r_2}\text{.CP1}}({\bf r_3} \times {\bf r_2}))$ 37. Với mỗi tỉnh thành cho biết tỉnh thành đó có bao nhiêu diễn viên. * Ngôn ngữ SQL: ```sql= select dv.DiaChi, count (*) as [SoDV] from DIENVIEN dv group by dv.DiaChi ``` * Đại số quan hệ: $$\text{KQ(DiaChi, SoDV)} \gets _{\text{DiaChi}}\mathfrak{J}_{\text{COUNT(*)}}(\text{DIENVIEN})$$ 38. Với mỗi tỉnh thành có hơn 2 diễn viên, cho biết số lượng diễn viên của tỉnh thành đó. * Ngôn ngữ SQL: ```sql= select dv.DiaChi as [TinhThanh], count (*) as [SoDV] from DIENVIEN dv group by dv.DiaChi having count(*) > 2 ``` * Đại số quan hệ: * ${\bf r_1}(\text{DiaChi, SoDV}) \gets _{\text{DiaChi}}\mathfrak{J}_{\text{COUNT(*)}}(\text{DIENVIEN})$ * $\text{KQ(TinhThanh, SoDV)} \gets \Pi_{\text{DiaChi, SoDV}}(\sigma_{\text{SoDV > 2}}({\bf r_1}))$ 39. Tìm tên những phim mà có thời lượng lớn hơn thời lượng của phim “Cuộc phiêu lưu trong rừng” * Ngôn ngữ SQL: ```sql= select ph.TenPhim from PHIM ph where ph.Dodai > ( select ph2.DoDai from PHIM ph2 where ph2.TenPhim = N'Cuộc phiêu lưu trong rừng' ) ``` * Đại số quan hệ: * ${\bf r_1}(\text{Dodai1}) \gets \Pi_{\text{Dodai}}(\sigma_{\text{TenPhim = 'Cuộc phiêu lưu trong rừng'}}(\text{PHIM}))$ * $\text{KQ} \gets \Pi_{\text{TenPhim}}(\sigma_{\text{PHIM.DoDai > }{\bf r_1}\text{.Dodai1}}(\text{PHIM}))$ 40. Tìm tên những phim và tên đạo diễn của phim mà có thời lượng lớn hơn thời lượng của phim “Cuộc phiêu lưu trong rừng” * Ngôn ngữ SQL: ```sql= select ph.TenPhim, dd.HoTen as [TenDaoDien] from PHIM ph join DAODIEN dd on ph.MaDaoDien = dd.MaDaoDien where ph.Dodai > ( select ph2.DoDai from PHIM ph2 where ph2.TenPhim = N'Cuộc phiêu lưu trong rừng' ) ``` * Đại số quan hệ: * ${\bf r_1}(\text{Dodai1}) \gets \Pi_{\text{Dodai}}(\sigma_{\text{TenPhim = 'Cuộc phiêu lưu trong rừng'}}(\text{PHIM}))$ * ${\bf r_2} \gets \text{PHIM} \bowtie \text{DAODIEN}$ * $\text{KQ(TenPhim, TenDaoDien)} \gets \Pi_{\text{TenPhim, HoTen}}(\sigma_{{\bf r_2}\text{.DoDai > }{\bf r_1}\text{.Dodai1}}({\bf r_2}))$ 41. Cho biết tên những bộ phim được sản xuất sớm nhất. * Ngôn ngữ SQL: ```sql= select ph.TenPhim from PHIM ph where ph.NamSanXuat <= ALL ( select ph2.NamSanXuat from PHIM ph2 ) ``` * Đại số quan hệ: * ${\bf r_1}(\text{Min_NamSX}) \gets \mathfrak{J}_{\text{MIN(NamSanXuat)}}(\text{PHIM})$ * $\text{KQ(TenPhim)} \gets \Pi_{\text{TenPhim}}({\bf r_1} \bowtie_{\text{Min_NamSX = NamSanXuat}} \text{PHIM})$ 42. Cho biết tên phim mà có diễn viên tham gia phim ở tại “Hồ Chí Minh” * Ngôn ngữ SQL: ```sql= select ph.TenPhim from DONGPHIM dp join DIENVIEN dv on dv.MaDienVien = dp.MaDienVien join PHIM ph on ph.MaPhim = dp.MaPhim where dv.DiaChi = N'Hồ Chí Minh' ``` * Đại số quan hệ: * ${\bf r_1} \gets (\text{DONGPHIM} \bowtie \text{DIENVIEN}) \bowtie \text{PHIM}$ * $\text{KQ} \gets \Pi_{\text{TenPhim}}(\sigma_{\text{DiaChi = 'Hồ Chí Minh'}}({\bf r_1}))$