Using the COUNT function in SQL

前端 未结 5 1081
猫巷女王i
猫巷女王i 2021-01-25 00:24

First and Foremost, this is part of an assignment.

I am trying to use the COUNT function as part of a query in relation to the Northwind database. The query should retur

5条回答
  •  無奈伤痛
    2021-01-25 01:21

    First, if you are learning SQL, you should learn proper explicit join syntax. Simple rule: never use a comma in the from clause.

    Second, your query should use group by instead of distinct. In fact, it is more important to learn group by than to learn distinct, because you can generally write select distinct using group by.

    So, where you are heading is:

    SELECT c.CustomerID, c.CompanyName, COUNT(c.CustomerID)
    FROM Orders o JOIN
         Customers c
         ON c.CustomerID = o.CustomerID
    GROUP BY c.CustomerID, c.CompanyName;
    

提交回复
热议问题