0% found this document useful (0 votes)
344 views2 pages

SQL Select Statements for Vendors and GLAccounts

The document contains 8 examples of SQL SELECT statements with answers. The examples retrieve data from tables like Vendors, Invoices, GLAccounts and InvoiceLineItems. They demonstrate basic and advanced SQL features like joins, sorting, filtering, aliases, unions and self-joins. The SELECT statements return various columns and calculate new columns to group or classify data.

Uploaded by

api-271197487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
344 views2 pages

SQL Select Statements for Vendors and GLAccounts

The document contains 8 examples of SQL SELECT statements with answers. The examples retrieve data from tables like Vendors, Invoices, GLAccounts and InvoiceLineItems. They demonstrate basic and advanced SQL features like joins, sorting, filtering, aliases, unions and self-joins. The SELECT statements return various columns and calculate new columns to group or classify data.

Uploaded by

api-271197487
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

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 [Link] = [Link];
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 [Link] = [Link]
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.

Answer: Select [Link] as Vendor, [Link] as Date,


[Link] as Number, [Link] as #,
[Link] as LineItem From Vendors V, Invoices I,
InvoiceLineItems li
6.

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 [Link], [Link] + [Link] as
Name
FROM Vendors AS V1 JOIN Vendors AS V2
ON [Link] = [Link]
WHERE [Link] <> [Link]
GROUP BY [Link], [Link] + [Link]
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 [Link], AccountDescription
FROM GLAccounts G, InvoiceLineItems LI
Where [Link] = [Link]
Order By [Link]
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

You might also like