Mis SQL HW
Mis SQL HW
Write a Select statement that returns three columns from the Vendors table:
VendorContactFName, VendorContactLName, and VendorName. Sort the result set
by last name, then by first name
Answer: SELECT VendorContactFName, VendorContactLName, and
VendorName
FROM Vendors
ORDER By VendorContactLName, VendorContactFName
2. Write a SELECT statement that determines whether the PaymentDate column of
the Invoices table has any invalid values. To be valid, PaymentDate must be a null
value if there is a balance due and a non-null value if there is no balance due.
Answer: SELECT *
FROM Invoices
WHERE PaymentDate is null
3. Write a SELECT statement that returns all columns from the Vendors table innerjoined with the Invoices table.
Answer: SELECT *
FROM Vendors JOIN Invoices
ON Vendors.VendorID = Invoices.VendorID;
4. Write a SELECT statement that returns three columns:
VendorName
From the Vendors table
DefaultAccountNo From the Vendors table
AccountDescription From the GLAccounts table
The result set should have one row for each vendor, with the account number and
account description for that vendor's default account number. Sort the result set by
AccountDescription, then by VendorName.
Answer: Select VendorName, DefaultAccountNo, AccountDescription
From Vendors, GLAccounts
Where Vendors.DefaultAccountNo = GLAccounts.AccountNo
Order by AccountDescription, VendorName
5. Write a SELECT statement that returns five columns from three tables, all using
column aliases:
VendorName column with alias Vendor, InvoiceDate column with alias Date,
InvoiceNumber column with alias Number, InvoiceSequence column with alias #
and InvoiceLineItemAmount column with alias LineItem.
Assign the following correlation names to the tables:
v for Vendors table, i for Invoices table and li for InvoiceLineItems table.
Write a SELECT statement that returns three columns: VendorID from Vendors, VendorName fro
VendorContactLName, with a space in between.
The result set should have one row for each vendor whose contact has the same first name as an
Hint: Use a self-join.
Answer:
SELECT v1.VendorName, v1.VendorContactFName + v1.VendorContactLName as
Name
FROM Vendors AS V1 JOIN Vendors AS V2
ON V1.VendorContactFName = V2.VendorContactFName
WHERE v1.VendorID <> V2.VendorID
GROUP BY v1.VendorName, v1.VendorContactFName + v1.VendorContactLName
7. Write a SELECT statement that returns two columns from the GLAccounts table.
AccountNo and AccountDescription. The result set should have one row for each
account number that has never been used. Sort the final result set by AccountNo.
Hint: Use the outer join to the InvoiceLineItems table.
Answer: SELECT DISTINCT G.AccountNo, AccountDescription
FROM GLAccounts G, InvoiceLineItems LI
Where G.AccountNo = LI.AccountNo
Order By G.AccountNo
8. Use the UNION operator to generate a result set consisting of two columns from
the Vendors table: VendorName and VendorState. If the vendor is in California, the
VendorState value should be "CA"; otherwise, the VendorState value should be
"Outside CA". Sort the final result set by VendorName.
Answer: SELECT VendorName, VendorState
FROM Vendors
WHERE VendorState = CA
UNION
SELECT VendorName, Outside CA AS VendorState
FROM Vendors
WHERE VendorState != CA'
ORDER BY VendorName