Skip to content
Advertisement

How would I make my SQL statement show only the first line (lowest price + total purchases)

I’ve got this SQL exercise I’m trying to complete but I can’t figure out how to specifically show one row that should contain the media type with the lowest # of purchases and purchase value.

I’m completely unsure of what I should add after my ‘group by’?

So this is what I want my output to be: https://i.stack.imgur.com/5biiM.png

And this is what it is: https://i.stack.imgur.com/oGyLV.png

Note that I’m using SQLite

Advertisement

Answer

Join the tables properly with each ON clause after its respective join and aggregate.
Sort the results by Total Number of Purchase ascending and return the top row:

Depending on the requirement, you may also use Minimum Total Purchase Value in the ORDER BY clause:

Never use single quotes for table/column aliases.
For SQLite you can use double quotes, backticks or square brackets.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement