Question

Class-In Assignment 3: Chapters 4&5 How to retrieve data from two or more tables Exercises 1....

Class-In Assignment 3:

Chapters 4&5 How to retrieve data from two or more tables Exercises

1. Write a SELECT statement that returns all columns from the Vendors table inner-joined with all columns from the Invoices table.

2. Write a SELECT statement that returns four columns: vendor_name vendor_name from the Vendors table invoice_number invoice_number from the Invoices table invoice_date invoice_date from the Invoices table balance_due invoice_total minus payment_total minus credit_total from the Invoices table The result set should have one row for each invoice with a non-zero balance. Sort the result set by vendor_name in ascending order.

3. Write a SELECT statement that returns three columns: vendor_name vendor_name from the Vendors table default_account default_account_number from the Vendors table description account_description from the General_Ledger_Accounts table The result set should have one row for each vendor, and it should be sorted by account_description and then by vendor_name.

4. Write a SELECT statement that returns five columns from three tables: vendor_name vendor_name from the Vendors table invoice_date invoice_date from the Invoices table invoice_number invoice_number from the Invoices table li_sequence invoice_sequence from the Invoice_Line_Items table li_amount line_item_amt from the Invoice_Line_Items table Use these aliases for the tables: Ven for the Vendors table, Inv for the Invoices table, and LI for the Invoice_Line_Items table. Also, sort the final result set by vendor_name, invoice_date, invoice_number, and invoice_sequence.

5. Write a SELECT statement that returns three columns: vendor_id vendor_id from the Vendors table vendor_name vendor_name from the Vendors table contact_name A concatenation of vendor_contact_first_name and vendor_contact_last_name with a space in between The result set should have one row for each vendor whose contact has the same last name as another vendor’s contact, and it should be sorted by vendor_contact_last_name. Hint: Use a self-join.

6. Write a SELECT statement that returns two columns from the General_Ledger_Accounts table: account_number and account_description. The result set should have one row for each account number that has never been used. Sort the final result set by account_number. Hint: Use an outer join to the Invoice_Line_Items table.

7. Use the UNION operator to generate a result set consisting of two columns from the Vendors table: vendor_name and vendor_state. If the vendor is in California, the vendor_state value should be “CA”; otherwise, the VendorState value should be “Outside CA.” Sort the final result set by vendor_name.

Chapter 5 How to code summary queries Exercises

1. Write a SELECT statement that returns one row for each vendor that contains these columns from the Invoices table: The vendor_id column The sum of the invoice_total column The result set should be sorted by vendor_id.

2. Write a SELECT statement that returns one row for each vendor that contains these columns: The vendor_name column from the Vendors table The sum of the payment_total column in the Invoices table. The result set should be sorted in descending sequence by the payment total sum for each vendor.

3. Write a SELECT statement that returns one row for each vendor that contains three columns: The vendor_name column from the Vendors table The count of the invoices for each vendor in the Invoices table The sum of the invoice_total column for each vendor in the Invoices table Sort the result set so the vendor with the most invoices appears first.

4. Write a SELECT statement that returns one row for each general ledger acQcount number that contains three columns: The account_description column from the General_Ledger_Accounts table The count of the entries in the Invoice_Line_Items table that have the same account_number The sum of the line item amounts in the Invoice_Line_Items table that have the same account-number Filter the result set to include only those rows with a count greater than 1; group the result set by account description; and sort the result set in descending sequence by the sum of the line item amounts.

5. Modify the solution to exercise 4 to filter for invoices dated in the second quarter of 2008 (April 1, 2008 to June 30, 2008). Hint: Join to the Invoices table to code a search condition based on invoice_date.

6. Write a SELECT statement that answers this question: What is the total amount invoiced for each general ledger account number? Use the ROLLUP operator to include a row that gives the grand total. Hint: Use the line_item_amt column of the Invoice_Line_Items table.

7. Write a SELECT statement that answers this question: Which vendors are being paid from more than one account? Return two columns: the vendor name and the total number of accounts that apply to that vendor’s invoices. Hint: Use the DISTINCT keyword to count the account_number column in the Invoice_Line_Items table.

Homework Answers

Answer #1

4.

1. SELECT *
FROM Vendors JOIN Invoices
ON Vendors.VendorID = Invoices.VendorID;

2. SELECT VendorName, InvoiceNumber, InvoiceDate,
InvoiceTotal - PaymentTotal - CreditTotal AS Balance
FROM Vendors JOIN Invoices
ON Vendors.VendorID = Invoices.VendorID
WHERE InvoiceTotal - PaymentTotal - CreditTotal > 0
ORDER BY VendorName;

3. SELECT VendorName, DefaultAccountNo, AccountDescription
FROM Vendors JOIN GLAccounts
ON Vendors.DefaultAccountNo = GLAccounts.AccountNo
ORDER BY AccountDescription, VendorName;

4. SELECT VendorName, InvoiceNumber, InvoiceDate,
InvoiceTotal - PaymentTotal - CreditTotal AS Balance
FROM Vendors, Invoices
WHERE Vendors.VendorID = Invoices.VendorID
AND InvoiceTotal - PaymentTotal - CreditTotal > 0
ORDER BY VendorName;

5. SELECT VendorName AS Vendor, InvoiceDate AS Date,
InvoiceNumber AS Number, InvoiceSequence AS [#],
InvoiceLineItemAmount AS LineItem
FROM Vendors AS v JOIN Invoices AS i
ON v.VendorID = i.VendorID
JOIN InvoiceLineItems AS li
ON i.InvoiceID = li.InvoiceID
ORDER BY Vendor, Date, Number, [#];

6. SELECT DISTINCT v1.VendorID, v1.VendorName,
v1.VendorContactFName + ' ' + v1.VendorContactLName AS Name
FROM Vendors AS v1 JOIN Vendors AS v2
ON (v1.VendorID <> v2.VendorID) AND
(v1.VendorContactFName = v2.VendorContactFName)
ORDER BY Name;

7. SELECT GLAccounts.AccountNo, AccountDescription
FROM GLAccounts LEFT JOIN InvoiceLineItems
ON GLAccounts.AccountNo = InvoiceLineItems.AccountNo
WHERE InvoiceLineItems.AccountNo IS NULL
ORDER BY GLAccounts.AccountNo;

8. SELECT VendorName, VendorState
FROM Vendors
WHERE VendorState = 'CA'
UNION
SELECT VendorName, 'Outside CA'
FROM Vendors
WHERE VendorState <> 'CA'
ORDER BY VendorName;

========================================================

/*1 - select statement that returns one row for each vendor in the invoices table that contains these columns
#the vendor_id column from the vendors table
#the sum of the invoice_total columns in the invoices table for that vendor */

select distinct v.vendor_id, i.invoice_total
from vendors v, invoices i
where v.vendor_id = i.vendor_id
group by i.vendor_id;

/*2 - select statement that returns one row for each vendor that contains these columns
# the vendor_name column from the vendors table
# the sum of the payment_total columns in the invoices table for that vendor
#sort the result set in descending sequence by the payment total sum for each vendor */

select v.vendor_name, sum(i.payment_total)
from vendors v, invoices i
where v.vendor_id = i.vendor_id
group by v.vendor_id
order by sum(i.payment_total) desc;

/*3 - select statement that returns one row for each vendor that contains three columns:
# vendor_name column from the vendors table
# count of the invoices in the invoices table for each vendor
# sum of the invoice_total columns in the invoices table for each vendor
#sort the result set so teh vendor with the most invoices appears first */

select vendor_name as "vendor name", count(i.vendor_id) as "num of invoices",
        sum(invoice_total) as "invoice total"
from vendors v, invoices i
where v.vendor_id = i.vendor_id
group by vendor_name
order by count(i.vendor_id) desc;

/*4 - select statement that returns one row for each general ledger account number and contains
# account_description from the gen ledger table
# count of items in the line items table that have the same account_number
# sum of the line_item_amount columns in the invoice line items table w/ same account number
#return rows with more than 1 line item
#group by account description
#sort by sum of line item amounts in desc sequence */

select gl.account_description as "account description", count(li.account_number) as "num of line items", 
        sum(li.line_item_amount) as "total line items"
from general_ledger_accounts gl, invoice_line_items li
where gl.account_number = li.account_number
group by gl.account_description
having count(li.account_number) > 1
order by sum(li.line_item_amount) desc;

-- modified solution to 4 that returns only invoices dated in the 2nd quarter of 2011
select gl.account_description as "account description", count(li.account_number) as "num of line items", 
        sum(li.line_item_amount) as "total line items", i.invoice_date as "invoice date"
from general_ledger_accounts gl, invoice_line_items li, invoices i
where gl.account_number = li.account_number
        and i.invoice_id = li.invoice_id
group by gl.account_description
having count(li.account_number) > 1
        and i.invoice_date >= '2011-04-01'
        and i.invoice_date <= '2011-06-30'
order by sum(li.line_item_amount) desc;

/*6 - question: what is the total amount invoiced for each general ledger account number? return these columns:
# account number from the invoice_line_items table
# sum of the line item amounts */

select li.account_number as "account number", sum(li.line_item_amount) as "line item total"
from invoice_line_items li
group by li.account_number
with rollup;

/*7 - question: which vendors are being paid from more than one account.
# returns: vendor name, count of distinct general ledger accounts */

select distinct v.vendor_name as "vendor name", count( distinct gl.account_number) as "num of distinct general ledger accounts"
from general_ledger_accounts gl
        join invoice_line_items li
                on gl.account_number = li.account_number
        join invoices i
                on li.invoice_id = i.invoice_id
        join vendors v                                          
                on i.vendor_id = v.vendor_id
group by v.vendor_id
        having count(distinct gl.account_number) > 1;
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a SELECT statement that returns one row for each general ledger account number that contains...
Write a SELECT statement that returns one row for each general ledger account number that contains three columns: The account_description column from the General_Ledger_Accounts table The count of the items in the Invoice_Line_Items table that have the same account_number. The sum of the line_item_amount columns in the Invoice_Line_Items table that have the same account_number Return only those rows where the count of line items is greater than 1. This should return 10 rows. Group the result set by the account_description...
1. Write a SELECT statement to count SKUs for each department, i.e., a table of two...
1. Write a SELECT statement to count SKUs for each department, i.e., a table of two columns, departments and their SKU counts. use cape_codd; select department, count(*) from sku_data group by department ; 2. List buyers who is responsible for at least two SKUs, in three columns, department, buyer, sku count. Sort buyers by their department names in ascending order. select department, buyer, count(sku) from sku_data group by buyer ; 3. Write a SQL statement which returns all the orders....
in mysql 3. Write a SELECT statement that returns one row for each customer that has...
in mysql 3. Write a SELECT statement that returns one row for each customer that has orders with these columns:   The email_address column from the Customers table   The sum of the item price in the Order_Items table multiplied by the   quantity in the Order_Items table   The sum of the discount amount column in the Order_Items table   multiplied by the quantity in the Order_Items table   Sort the result set in descending sequence by the item price total for each customer.  
In SQL working with data types 1. Write a SELECT statement that returns these columns from...
In SQL working with data types 1. Write a SELECT statement that returns these columns from the Instructors table: a. The monthly salary (the AnnualSalary column divided by 12) b. A column that uses the CAST function to return the monthly salary with 1 digit to the right of the decimal point c. A column that uses the CONVERT function to return the monthly salary as an integer d. A column that uses the CAST function to return the monthly...
Lab 5 Queries with Multiple Tables In this lab, we do queries more than one table....
Lab 5 Queries with Multiple Tables In this lab, we do queries more than one table. SQL provides two different techniques for querying data from multiple tables: • The SQL subquery • The SQL join As you will learn, although both work with multiple tables, they are used for slightly different purposes. We used WMCRM database which is what we created in Lab 4. Here is the summary of the database schema (where schema is used in its meaning of...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to...
In java //Create a New Project called LastNameTicTacToe.// //Write a class (and a client class to test it) that encapsulates a tic-tac-toe board. // A tic-tac-toe board looks like a table of three rows and three columns partially or completely filled with the characters X and O. // At any point, a cell of that table could be empty or could contain an X or an O. You should have one instance variable, a two-dimensional array of values representing the...
Saul wants to buy a new writer's desk, but he doesn't want to spend more than...
Saul wants to buy a new writer's desk, but he doesn't want to spend more than $350. Write a select statement to find products matching these requirements For each customer, list the customer name and city and state in this format: City, State (one column) Display only those orders where more than one different product has been ordered Find the average price of bookcases A. List all the customers and list the number of different shipping addresses for each. Customers...
Save your select statements as a script. Place the semicolon at the end of each SQL...
Save your select statements as a script. Place the semicolon at the end of each SQL statement. Please number your select statements from 1 to 8 in your script and comment out each number. Include your name and student number as a comment at the top of your script. The name of the script has to be Assignment1_JohnSmith. Instead of JohnSmith use your First Name and Last Name. Upload your script trough Blackboard. Use SQL Developer to create the My...
Data For Tasks 1-8, consider the following data: 7.2, 1.2, 1.8, 2.8, 18, -1.9, -0.1, -1.5,...
Data For Tasks 1-8, consider the following data: 7.2, 1.2, 1.8, 2.8, 18, -1.9, -0.1, -1.5, 13.0, 3.2, -1.1, 7.0, 0.5, 3.9, 2.1, 4.1, 6.5 In Tasks 1-8 you are asked to conduct some computations regarding this data. The computation should be carried out manually. All the steps that go into the computation should be presented and explained. (You may use R in order to verify your computation, but not as a substitute for conducting the manual computations.) A Random...
True False Select the most correct answer. Question 1 (1 point) Most databases are just in...
True False Select the most correct answer. Question 1 (1 point) Most databases are just in 1NF. Question 1 options: True False Question 2 (1 point) A subquery is always preferred over a join. Question 2 options: True False Question 3 (1 point) A database may consist only of multiple files. Question 3 options: True False Question 4 (1 point) A database may exist without a database management system. Question 4 options: True False Question 5 (1 point) The insert...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT