R
R
Roman K2021-09-30 19:15:28
SQL
Roman K, 2021-09-30 19:15:28

What is the error in SQL query (MAX and AVG)?

Hello, there is a database with two tables, from it you need to pull out a table indicating the manufacturer with the maximum average cost of phones 6155e0865fe7c054293838.png
; `companyId`) AS reporttable INNER JOIN company ON reporttable.companyId=company.companyId " gives the wrong result, namely, it incorrectly indicates the phone manufacturer, while without INNER JOIN it works correctly, indicating the manufacturer's Id and the maximum average price of phones. But you need to display the name of the manufacturer in the table, not its Id. Database file https://disk.yandex.ru/d/lQnRbz4WqZKDHQ

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Slava Rozhnev, 2021-09-30
@Roman_v_k

You need to use the following query:

SELECT 
  companyName, 
  PriceAvg 
FROM 
  (
    SELECT 
      `companyId`, 
      AVG(`price`) AS PriceAvg 
    FROM 
      phone 
    GROUP BY 
      `companyId`
  ) AS reporttable 
  INNER JOIN company ON reporttable.companyId = company.companyId
  ORDER BY PriceAvg DESC LIMIT 1;

Check SQL online

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question