Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Album MediaType MediaTypeld Name A A Track Artistld Albumid Trackid Tite Albumid

ID: 3877981 • Letter: A

Question

Album MediaType MediaTypeld Name A A Track Artistld Albumid Trackid Tite Albumid MediaTypeld Genreld Artistid Genreld Playlist Playlist!d PlaylistTrack Playlistic Track!d Milliseconds Bytes UnitPrice Trackld UnitPrice Customer LastName FirstName Titde ReportsTo BirthDate HireDate Address Customertd FirstName Address City State Invoiceld Customerld InvoiceDate BilingAddress State Country PostalCode Phone Fax Email PostalCode Phone Fax Email SupportRepld BilingState Total 1. Using a subquery, find the names of all the tracks for the album "Californication". 2. Find the total number of invoices for each customer along with the customer's full name, city and email 3. Retrieve the track name, album, artist, and tracklD for all the albums 4. Retrieve a list with the managers last name, and the last name of the employees who report to him or her. 5. Find the name and ID of the artists who do not have albums 6. Use a UNION to create a list of all the employee's & customer's first names and last names ordered by the last name in descending order. 7. See if there are any customers who have a different city listed in their billing city versus their customer city. 8. Select the customers first name, last name, and make sure the invoice total for the unit price and quantity add up to the overall invoice total. Sort in descending order by the total

Explanation / Answer

1) In where clause subquery is used to filter the album id with title 'Californication'.

SELECT Name FROM Track WHERE AlbumId = (SELECT AlbumId FROM Album WHERE Title = ‘Californication’);

2) Aggregate function COUNT() is used with group by clause.

SELECT FirstName, LastName, City, Email, COUNT(InvoiceId)

FROM Customer AS c

INNER JOIN Invoice AS i

ON c.CustomerId = i.CustomerId

GROUP BY FirstName, LastName, City, Email;

3) Three tables Artist, Album and track are joined on common attributes to get the result.

SELECT t.Name, a.Title, ar.Name, t.TrackId

FROM Track AS t

INNER JOIN Album As a

ON t.AlbumId = a.AlbumId

INNER JOIN Artist AS ar

ON a.ArtistId = ar.ArtistId;

4) Self join is used on Employee table to get the employee name with there reportees.

SELECT m.LastName AS Manager, e.LastName AS Employee

FROM Employee e, Employee m

e.ReportsTo = m.EmployeeId;

5) Subquery is used to get the names of Artist who do not have any album.

SELECT ArtistId, Name FROM Artist WHERE ArtistId NOT IN (SELECT ArtistId FROM Album);

6) Union is used to combine the names from the customer and employee table. Order by is applied on the complete result.

SELECT FirstName, LastName

FROM Employee

UNION

SELECT FirstName, LastName

FROM Customer

ORDER BY LastName;

7) Subquery is ued to get the CustomerId whose Billing city is not equal to Customer City.

SELECT CustomerId, FirstName

FROM Customer

WHERE CustomerId IN (SELECT CustomerId FROM Invoice WHERE BillingCity <> Customer.City);

8) Aggregate function SUM() is used to get the Total amount in Invoice. Group BY clause is used while using aggregate function.

SELECT FirstName, LastName, SUM(i.Total)

FROM Customer AS c

INNER JOIN Invoice AS i

ON i.CustomerId = i.InvoiceId

GROUP BY FirstName, LastName

ORDER BY SUM(i.Total) DESC;

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote