Skip to content
Advertisement

SQL Query: finding cheapest car by company

Following YouTube tutorial “Learn PostgreSQL Tutorial – Full Course for Beginners”, I replicate teacher’s code but yields different result and cannot figure out why.

Table is this simple:

and so on, 1000 entries.

Querying cheapest model from manufacturer, tutorial says:

And it works properly, returning as many entries as distinct car makers. But when I run it in my PostgreSQL terminal returns all 1000 entries disordered.

However, when I query without model’s name, I get the right answer, but (obviously) without the model name as shown below:

Any suggestions as to why this might happen?

Advertisement

Answer

This db-fiddle works as expected. Notice the output. It shows a proper GROUP BY.

Query source:

Result:

Schema (PostgreSQL v10.0)


Query #1

make model min
Fillmore F_M1 10000
Fillmore F_M2 20000
McLaren M_M2 40000
Toyota T_M1 12000
Toyota T_M2 24000
Toyota T_M3 48000

View on DB Fiddle

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