I have 4 tables. One is called artist. Here is the table structure:
artistID lastname firstname nationality dateofbirth datedcease
The other table is called work
workId title copy medium description artist ID
Trans table
TransactionID Date Acquired Acquistionprice datesold
askingprice salesprice customerID workID
Customer table
customerID lastname Firstname street city state
zippostalcode country areacode phonenumber email
First question is which artist has the most works of artsold and how many of the artist works have been sold.
My SQL query is below
SELECT *
FROM dtoohey.artist A1
INNER JOIN
(SELECT
COUNT(W1.ArtistID) AS COUNTER, artistID
FROM dtoohey.trans T1
INNER JOIN dtoohey.work W1 ON W1.workid = T1.Workid
GROUP BY W1.artistID) TEMP1 ON TEMP1.artistID = A1.artistID
WHERE
A1.artistID = TEMP1.artistId
ORDER BY
COUNTER desc;
I am to get the whole table but I want to show only the first row which is the highest count - how do I do that??
qns 2 is sales of which artist's work have resulted in the highest average profit(i.e) the average of the profits made on each sale of worksby an artist), and what is that amount.
My SQL query is below
SELECT
A1.artistid, A1.firstname
FROM
(SELECT
(salesPrice - AcquisitionPrice) as profit,
w1.artistid as ArtistID
FROM dtoohey.trans T1
INNER JOIN dtoohey.WORK W1 ON W1.workid = T1.workid) TEMP1
INNER JOIN
dtoohey.artist A1 ON A1.artistID = TEMP1.artistID
GROUP BY
A1.artistid
HAVING
MAX(PROFIT) = AVG(PROFIT);
I'm not able to execute it