Subqueries - Practice Questions

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

ISA 245

Practice Questions – Subqueries

Not Graded

Before starting this exercise, load the ‘Artist-Album Database.sql’ file available in Week 11 in
FSB Oracle webserver and execute. This will drop all previously created tables from Artist and
Album database and recreate them.

Write and execute your queries for each of the following request. Try to use subqueries
wherever required.

1. Display the artists’ name and age who have not created any album.
SELECT ARTIST_NAME, ARTIST_AGE
FROM ARTIST
WHERE ARTIST.ARTIST_ID NOT IN
(SELECT ALBUM.ARTIST_ID
FROM ALBUM)

ARTIST_NAME ARTIST_AGE

Bob Marley 66

2. Display the artists’ name whose album release date is in 2023.


SELECT ARTIST_NAME
FROM ARTIST
WHERE ARTIST_ID IN
(SELECT ARTIST_ID
FROM ALBUM
WHERE TO_CHAR(ALBUM_REL_DATE,'YYYY’) = 2023);
ARTIST_NAME

Black Sabbath
Rush

3. Display the artists’ name and album title whose album price is less than the overall average
album price.
SELECT ARTIST_NAME, ALBUM_TITLE
FROM ARTIST INNER JOIN ALBUM
ON ARTIST.ARTIST_ID = ALBUM.ARTIST_ID
WHERE ALBUM_PRICE < (SELECT AVG(ALBUM_PRICE)
FROM ALBUM);

ARTIST_NAME ALBUM_TITLE

Black Sabbath Master of Reality


Rush 2112
Pink Floyd Dark Side of the Moon

4. Display the artists’ id whose album price is more than the overall average album price. Also,
display the overall average price in the same query.

5. Display the artists’ name and age who have more than one album.

SELECT ARTIST_NAME, ARTIST_AGE


FROM ARTIST
WHERE ARTIST.ARTIST_ID IN (
SELECT ARTIST_ID
FROM ALBUM
HAVING COUNT(ARTIST_ID) > 1
GROUP BY ARTIST_ID);

ARTIST_NAME ARTIST_AGE

Black Sabbath (null)


Rush (null)

6. Display the album title and the difference between the album price and the average album
price.
SELECT ALBUM_TITLE, ALBUM_PRICE - (SELECT AVG(ALBUM_PRICE) FROM ALBUM) AS
PRICE_DIFFERENCE
FROM ALBUM;

ALBUM_TITLE PRICE_DIFFERENCE

Never Say Die 1.4


Master of Reality -.6
Untitled .4
Untitled (null)
2112 -.6
Dark Side of the Moon -.6

7. Use DELETE to remove all artists from the ARTIST table who are not in the ALBUM table.
DELETE FROM ARTIST
WHERE ARTIST.ARTIST_ID NOT IN (
SELECT ARTIST_ID
FROM ALBUM);
8. Use DELETE to remove all artist from the ALBUM table whose age is not available.
DELETE FROM ALBUM
WHERE ARTIST_ID IN
(SELECT ARTIST_ID FROM ARTIST WHERE ARTIST_AGE IS NULL);

You might also like