You are on page 1of 3

Query List #2

8. Display Vendor ID and Vendor Names for vendors that have no invoices.

--using a subquery

SELECT Vendors.VendorID, VendorName


FROM Vendors
WHERE VendorID NOT IN (SELECT DISTINCT VendorID
FROM Invoices);

Query List #3

1. Display Vendor name, number of invoices for each vendor, and average invoice
total for each vendor that has average invoice amount greater than $500. Display in
ascending order of average invoice total values. (HAVING)

SELECT VendorName, COUNT(InvoiceID) AS NumOfInvoices, AVG(InvoiceTotal) AS


AvgInvTotal
FROM Vendors INNER JOIN Invoices ON Vendors.VendorID = Invoices.VendorID
GROUP BY Vendors.VendorID, VendorName
HAVING AVG(InvoiceTotal) > 500
ORDER BY AVG(InvoiceTotal);

--or using the column alias in the ORDER BY clause

SELECT VendorName, COUNT(InvoiceID) AS NumOfInvoices, AVG(InvoiceTotal) AS


AvgInvTotal
FROM Vendors INNER JOIN Invoices ON Vendors.VendorID = Invoices.VendorID
GROUP BY Vendors.VendorID, VendorName
HAVING AVG(InvoiceTotal) > 500
ORDER BY AvgInvTotal;

2. Display Vendor name, number of invoices for each vendor, and average invoice
total for each vendor for all invoices with Invoice Total amount greater than $500.
(WHERE)

SELECT VendorName, COUNT(InvoiceID) AS NumOfInvoicesForVendor, AVG(InvoiceTotal) AS


AvgInvTotalForVendor
FROM Vendors INNER JOIN Invoices ON Vendors.VendorID = Invoices.VendorID
WHERE InvoiceTotal > 500
GROUP BY Vendors.VendorID, VendorName;

3. Display invoice date, number of invoices, total of invoice totals for invoice
dates between May 1, 2006 and May 31, 2006. Only display information for those
dates for which there are more than one invoices and the sum of invoice totals is
greater than $100.

SELECT INVOICEDATE, COUNT(INVOICEID) AS NumOfInvoices, SUM(InvoiceTotal) AS


TotalOfInvoices
FROM INVOICES
WHERE INVOICEDATE BETWEEN '05/01/2006' AND '05/31/2006 11:59 PM'
GROUP BY INVOICEDATE
HAVING COUNT(INVOICEID) > 1 AND SUM(INVOICETOTAL) > 100;
4. Display Invoice Number, Invoice Date, and Invoice total for invoices with
invoice total greater than the average invoice totals for all the invoices. Display
in ascending order of invoice totals.

SELECT Invoicenumber, InvoiceDate, InvoiceTotal


FROM Invoices
WHERE InvoiceTotal > (SELECT AVG(InvoiceTotal)
FROM Invoices)
ORDER BY InvoiceTotal;

5. List Vendor names, Invoice numbers and Invoice totals for all invoices that are
larger than the largest invoice total for vendor 34. List output in ascending order
of vendor name.

SELECT VendorName, InvoiceNumber, InvoiceTotal


FROM VENDORS INNER JOIN INVOICES ON VENDORS.VENDORID = INVOICES.VENDORID
WHERE INVOICETOTAL > (SELECT MAX(InvoiceTotal)
FROM INVOICES
WHERE VENDORID = 34)
ORDER BY VendorName;

6. List Invoice number, Invoice date and Invoice totals for vendors from
California. Sort output by Invoice date in ascending order.

SELECT InvoiceNumber, InvoiceDate, InvoiceTotal


FROM VENDORS INNER JOIN INVOICES ON VENDORS.VENDORID = INVOICES.VendorID
WHERE VENDORSTATE = 'CA'
ORDER BY InvoiceDate;

7. Display Vendor state, city, number of invoices, and average invoice total for
vendors located in each city. Display only cities that have more than two vendors.

SELECT VENDORCITY, VENDORSTATE, COUNT(InvoiceID) AS NumOfInvoices,


AVG(InvoiceTotal) AS AvgOfInvoiceTotals
FROM VENDORS INNER JOIN INVOICES ON VENDORS.VENDORID = INVOICES.VendorID
GROUP BY VENDORCITY, VENDORSTATE
HAVING COUNT(DISTINCT VENDORS.VENDORID) > 2;

--Use of implicit INNER JOIN syntax and table alias (correlation name) to
--join the Vendors, Invoices and InvoiceLineItems tables

SELECT *
FROM VENDORS, INVOICES, InvoiceLineItems AS InvLine
WHERE VENDORS.VENDORID = INVOICES.VENDORID AND INVOICES.INVOICEID =
InvLine.InvoiceID
ORDER BY VendorName;
--Use of explicit INNER JOIN syntax and table alias (correlation name) to
--join the Vendors, Invoices and InvoiceLineItems tables

SELECT *
FROM VENDORS INNER JOIN INVOICES ON VENDORS.VENDORID = INVOICES.VENDORID
INNER JOIN InvoiceLineItems AS InvLine ON INVOICES.InvoiceID = InvLine.InvoiceID
ORDER BY VendorName;

--Use of the LIKE operator to find all vendors with VendorNames beginning with
letter I.
--Percent symbol (%) and underscore (_) are two wild characters we can use with the
LIKE operator.

SELECT *
FROM VENDORS
WHERE VENDORNAME LIKE 'I%'

GO

--Creating a VIEW

CREATE VIEW TESTINVOICES AS


SELECT INVOICEDATE, COUNT(INVOICEID) AS NumOfInvoices, SUM(InvoiceTotal) AS
TotalOfInvoices
FROM INVOICES
WHERE INVOICEDATE BETWEEN '05/01/2006' AND '05/31/2006 11:59 PM'
GROUP BY INVOICEDATE
HAVING COUNT(INVOICEID) > 1 AND SUM(INVOICETOTAL) > 100;

GO

--To display data from the VIEW created, use the view TESTINVOICES as the data
source in the FROM clause
--of a SELECT statement

SELECT *
FROM TESTINVOICES;

You might also like