Search code examples
sqlsql-serverinner-joinwhere-clause

How could I add a WHERE condition to code with INNER JOINs?


I don't know how to add a where condition for an AS column, is there a

I've tried using the column with square brackets and it doesn't work

SELECT   
    Suppliers.CompanyName,
    SUM([Order Details].UnitPrice*Quantity) AS [Total sales]
FROM
    [Order Details]  
INNER JOIN 
    Products ON products.ProductID = [Order Details].ProductID
INNER JOIN 
    Suppliers ON Products.SupplierID = Suppliers.SupplierID
WHERE 

GROUP BY 
    Suppliers.CompanyName
ORDER BY 
    2 DESC;

I would like to only see the total sales over 10,000


Solution

  • You need to add the condition in a HAVING clause:

    SELECT  
      Suppliers.CompanyName, 
      SUM([Order Details].UnitPrice*Quantity)AS [Total sales]
    FROM [Order Details] 
    INNER JOIN 
    Products ON products.ProductID= [Order Details] .ProductID
    INNER JOIN
    Suppliers ON Products.SupplierID= Suppliers.SupplierID
    GROUP BY Suppliers.CompanyName
    HAVING SUM([Order Details].UnitPrice*Quantity) > 10000
    Order by 2 desc;