Sql-server – T-SQL for Query that can be done in SQL. What do they want

sql servert-sql

We are given tables Vendors(VendorId, VendorName, ..) , Invoices(InvoiceId, VendorId…)
I am going over a book exercise that asks for a T-SQL statement to find
all vendors with at least one invoice. But I am confused, since this can be perfectly done with standard SQL:

SELECT VendorName FROM Vendors V join Invoices I on V.VendorId=I.VendorID
GROUP BY VendorName having Count(InvoiceId) > = 1

So, what could they be asking? Should I come up with a procedure to select all vendors with more than n invoices; n= 0,1,2 ?

If so, why is this not working for the general case of n invoices:

 CREATE Procedure InvoiceCount @NumberofInvoices Int
AS 
BEGIN
SELECT VendorName FROM Vendors V join Invoices I on V.VendorId=I.VendorID
WHERE Count(InvoiceId) > = @Number of Invoices
EXEC InvoiceCount @Number of Invoices = n

Why doesn't this work?
Thank you.

Best Answer

You don't need the WHERE clause because you are doing an inner join, which ensures you will only get vendors that have an invoice:

SELECT DISTINCT VendorName FROM Vendors V join Invoices I on V.VendorId=I.VendorID

OR

SELECT DISTINCT VendorName 
FROM Vendors V 
WHERE EXISTS (SELECT 1 FROM Invoices I WHERE V.VendorId=I.VendorID)