CASE Study DBMS - 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Case Study 1: Art Gallery Management System

Scenario: You are managing a database for an art gallery that tracks artists, artworks, exhibitions, and sales.

Tables:
• Artists (ArtistID, Name, BirthYear,
Nationality)
• Artworks (ArtworkID, Title, ArtistID, YearCreated, Price)
• Exhibitions (ExhibitionID, Title, StartDate, EndDate)
• Sales (SaleID, ArtworkID, ExhibitionID, SaleDate, SalePrice)

Questions:

1. Create the Artists, Artworks, Exhibitions, and Sales tables.


CREATE TABLE Artists (
ArtistID INT PRIMARY KEY,
Name VARCHAR(100),
BirthYear INT,
Nationality VARCHAR(50)
);

CREATE TABLE Artworks (


ArtworkID INT PRIMARY KEY,
Title VARCHAR(100),
ArtistID INT,
YearCreated INT,
Price DECIMAL(10, 2),
FOREIGN KEY (ArtistID) REFERENCES Artists(ArtistID)
);

CREATE TABLE Exhibitions (


ExhibitionID INT PRIMARY KEY,
Title VARCHAR(100),
StartDate DATE,
EndDate DATE
);

CREATE TABLE Sales (


SaleID INT PRIMARY KEY,
ArtworkID INT,
ExhibitionID INT,
SaleDate DATE,
SalePrice DECIMAL(10, 2),
FOREIGN KEY (ArtworkID) REFERENCES Artworks(ArtworkID),
FOREIGN KEY (ExhibitionID) REFERENCES Exhibitions(ExhibitionID)
);

2. Alter sales table and add one more column of your own choice

ALTER TABLE Sales ADD CustomerName VARCHAR(100);

3. Insert sample data into each table (at least 5 records per table).
-- Insert data into Artists
INSERT INTO Artists (ArtistID, Name, BirthYear, Nationality)
VALUES
(1, 'Pablo Picasso', 1881, 'Spanish'),
(2, 'Vincent van Gogh', 1853, 'Dutch'),
(3, 'Frida Kahlo', 1907, 'Mexican'),
(4, 'Salvador Dalí', 1904, 'Spanish'),
(5, 'Claude Monet', 1840, 'French');

-- Insert data into Artworks


INSERT INTO Artworks (ArtworkID, Title, ArtistID, YearCreated, Price)
VALUES
(1, 'Guernica', 1, 1937, 2000000),
(2, 'Starry Night', 2, 1889, 1500000),
(3, 'The Two Fridas', 3, 1939, 1200000),
(4, 'The Persistence of Memory', 4, 1931, 1800000),
(5, 'Water Lilies', 5, 1906, 1300000);

-- Insert data into Exhibitions


INSERT INTO Exhibitions (ExhibitionID, Title, StartDate, EndDate)
VALUES
(1, 'Modern Art Exhibition', '2023-01-15', '2023-02-15'),
(2, 'Surrealism and Beyond', '2023-03-01', '2023-04-01'),
(3, 'Impressionist Masterpieces', '2024-05-01', '2024-06-01'),
(4, '20th Century Art', '2024-09-15', '2024-10-15'),
(5, 'Post-War Art', '2025-01-10', '2025-02-10');

-- Insert data into Sales


INSERT INTO Sales (SaleID, ArtworkID, ExhibitionID, SaleDate, SalePrice, CustomerName)
VALUES
(1, 1, 1, '2023-01-20', 1900000, 'John Doe'),
(2, 2, 1, '2023-01-22', 1400000, 'Jane Smith'),
(3, 3, 2, '2023-03-05', 1100000, 'Alice Brown'),
(4, 4, 2, '2023-03-10', 1750000, 'Bob Johnson'),
(5, 5, 3, '2024-05-05', 1250000, 'Carol White');
4. Update the Price of an artwork with ArtworkID = 3 to $1500.
UPDATE Artworks SET Price = 1500 WHERE ArtworkID = 3;

5. Delete all artworks created before the year 2000.


DELETE FROM Artworks WHERE YearCreated < 2000;

6. Find the average Price of artworks.


SELECT AVG(Price) AS AveragePrice FROM Artworks;

7. Count the number of artworks by each artist.


SELECT ArtistID, COUNT(*) AS ArtworkCount FROM Artwork GROUP BY ArtistID;

8. Find the ExhibitionID where the total sales amount is the highest.
SELECT ExhibitionID
FROM Sales
GROUP BY ExhibitionID
ORDER BY SUM(SalePrice) DESC
LIMIT 1;

9. List the names of all artists and the titles of their artworks.
SELECT Artists.Name, Artworks.Title
FROM Artists
JOIN Artworks ON Artists.ArtistID = Artworks.ArtistID;

10. Retrieve the titles and sale prices of artworks sold during exhibitions starting in 2024.
SELECT Artworks.Title, Sales.SalePrice
FROM Sales
JOIN Exhibitions ON Sales.ExhibitionID = Exhibitions.ExhibitionID
JOIN Artworks ON Sales.ArtworkID = Artworks.ArtworkID
WHERE Exhibitions.StartDate >= '2024-01-01';

11. Retrieve the titles of Exhibitions using IN & BETWEEN CLAUSE


SELECT Title
FROM Exhibitions
WHERE ExhibitionID IN (1, 3, 5)
OR StartDate BETWEEN '2023-01-01' AND '2025-01-01';

12. Display exhibitions and their total sales, including those with no sales.
SELECT Exhibitions.Title, SUM(Sales.SalePrice) AS TotalSales
FROM Exhibitions
LEFT JOIN Sales ON Exhibitions.ExhibitionID = Sales.ExhibitionID
GROUP BY Exhibitions.Title;

13. Find artists who have artworks both exhibited and sold (use INTERSECT).

SELECT ArtistID
FROM Artworks
WHERE ArtworkID IN (SELECT ArtworkID FROM Sales)
INTERSECT
SELECT ArtistID
FROM Artworks
WHERE ArtworkID IN (SELECT ArtworkID FROM Exhibitions);
14. List all unique exhibition titles and artwork titles combined (use UNION).

SELECT Title FROM Exhibitions UNION SELECT Title FROM Artworks;

15. Combine the table Artist and Artworks through ArtistID (use JOIN)

SELECT Artists.Name, Artworks.Title, Artworks.YearCreated, Artworks.Price FROM Artists


JOIN Artworks ON Artists.ArtistID = Artworks.ArtistID;

You might also like