5
CXCP 216 Intro to SQL Instructor: Mohamed Khan EXERCISE SET -3 Worth 10 Marks–Week 7 at the end of the class following your Mid-Term Test. Each query must be verified with a Query result to be printed out as you have done with previous exercises. 1

Assignement 3 - JP

  • Upload
    jdphan

  • View
    183

  • Download
    6

Embed Size (px)

DESCRIPTION

SQL

Citation preview

Page 1: Assignement 3 - JP

CXCP 216 Intro to SQL Instructor: Mohamed Khan

EXERCISE SET -3 Worth 10 Marks–Week 7 at the end of the class following your Mid-Term Test. Each query must be verified with a Query result to be printed out as you have done with previous exercises.

1

Page 2: Assignement 3 - JP

CXCP 216 Intro to SQL Instructor: Mohamed Khan

EXERCISE SET -3 Worth 10 Marks–Week 7 at the end of the class following your Mid-Term Test. Each query must be verified with a Query result to be printed out as you have done with previous exercises.Refer to your Textbook for the course: Chapter 6 SubqueriesPp 213- 214 Questions 2, 4, 5, 8 & 9

2) Write a SELECT statement that answers this question: Which invoices have a PaymentTotal that’s greater than the average PaymentTotal for all paid invoices? Return the InvoiceNumber and InvoiceTotal for each invoice.

Query:

SELECT InvoiceNumber, InvoiceTotalFROM InvoicesWHERE PaymentTotal>(SELECT AVG(PaymentTotal)FROM Invoices)

Query Results:

InvoiceNumber

InvoiceTotal

989319-457 3813.3310843 4901.2677290 1750

P02-3772 7125.34RTR-72-3662-

X 16000-2058 37966.1940318 21842

989319-437 2765.3631359783 1575

989319-477 2184.11

4) Write a SELECT statement that returns two coloumns from the GLAccounts table: AccountNo and AccountDescription. The result set should have one row for each account number that has never been used. Use a correlated subquery introduced with the NOT EXISTS operator. Sort the final result set by AccountNo.

Query:

SELECT AccountNo, AccountDescriptionFROM GLAccountsWHERE NOT EXISTS

(SELECT *FROM InvoiceLineItemsWHERE InvoiceLineItems.AccountNo = GLAccounts.AccountNo)

ORDER BY AccountNo ASC

2

Page 3: Assignement 3 - JP

CXCP 216 Intro to SQL Instructor: Mohamed Khan

EXERCISE SET -3 Worth 10 Marks–Week 7 at the end of the class following your Mid-Term Test. Each query must be verified with a Query result to be printed out as you have done with previous exercises.Query Results:

AccountNo AccountDescription100 Cash110 Accounts Receivable120 Book Inventory162 Capitalized Lease167 Software181 Book Development200 Accounts Payable205 Royalties Payable

221401K Employee

Contributions230 Sales Taxes Payable

5) Write a SELECT statement that returns four coloumns: VendorName, InvoiceID, InvoiceSequence, and InvoiceLineItemAmount for each invoice that has more than one line item in the InvoiceLineItems table.

8) Write a SELECT statement that returns four coloumns: VendorName, InvoiceNumber, InvoiceDate, and InvoiceTotal. Return one row per vendor, representing the vendor’s invoice with the earliest date.

9) Rewrite exercise 6 so it uses a common table expression (CTE) instead of a derived table.

Chapter 7 Action QueriesPp 236 -237 Questions: 3, 4, 8, 9

3) Write an INSERT statement that adds a row to the VendorCopy table for each non-California vendor in the Vendors table. (This will result in duplicate vendors in the VendorCopy table.)

4) Write an UPDATE statement that modifies the VendorCopy table. Change the default account number to 403 for each vendor that has a default account number of 400.

Query:

3

Page 4: Assignement 3 - JP

CXCP 216 Intro to SQL Instructor: Mohamed Khan

EXERCISE SET -3 Worth 10 Marks–Week 7 at the end of the class following your Mid-Term Test. Each query must be verified with a Query result to be printed out as you have done with previous exercises.UPDATE VendorCopySET DefaultAccountNo = '403'WHERE DefaultAccountNo = '400'

Query Results:

(6 row(s) affected)

8) Write a DELETE statement that deletes all vendors in the state of Minnesota from the VendorCopy table.

Query:

DELETE VendorCopyWHERE VendorState = 'MN'

Query Results:

(1 row(s) affected)

9) Write a DELETE statement for the VendorCopy table. Delete the vendors that are located in states from which no vendor has sent an invoice.

Hint: Use a subquery coded with “SELECT DISTINCT VendorState” introduced with the NOT IN operator.

A total of 9 Questions

.

4