Sie sind auf Seite 1von 4

BSYS602 Business Data Management

Additional SELECT Statement Practice

Use the sales schema below to work on queries. Before you start writing queries,
you need to run the sql script file “customer_invoice.sql” to create tables and
generate data for those tables. Familiarize yourself with the database structure
including tables, primary keys, foreign keys and relationships.

W7_CUSTOMER W7_INVOICE

PK CUS_CODE PK INV_NUMBER

CUS_LNAME FK CUS_CODE

CUS_FNAME INV_DATE

CUS_INITIAL

CUS_AREACODE

CUS_PHONE
W7_LINE

CUS_BALANCE
PK INV_NUMBER

PK LINE_NUMBER

FK P_CODE

LINE_UNITS
W7_PRODUCT
LINE_PRICE
PK P-CODE

P_DESCRIPT

P_INDATE
W7_VENDOR
P_QOH
PK V_CODE
P_MIN
V_NAME
P_PRICE
V_CONTACT
P_DISCOUNT
V_AREACODE
FK V_CODE
V_PHONE

V_STATE

V_ORDER

1
BSYS602 Business Data Management
Write SQL commands for the following queries.

(1) List invoice number and total amount for each invoice. The result should be
ordered by invoice number with appropriate headings. The partial output is
shown below.

(2) List invoice number, line number, line units, product code, product description,
and product quantity on hand for each row of data in the LINE table. The result
should be ordered by invoice number and line number.

(3) List all product attributes from all those products in the LINE table that have line
units greater than or equal to 3.

(4) Write a query to list each invoice and its associated details including invoice
number, invoice date. For each invoice line item, display product description,
line units and line price. The result should be ordered by invoice number
followed by product description. The partial output is shown below.

2
BSYS602 Business Data Management
(5) Write a query to display invoice records for each customer. The output should
have appropriate headings and list customer code, customer first name,
customer last name, invoice number and total amount from each invoice. The
output should be sorted by customer code and invoice number. For example,
customer code 10012 has one invoice (#1003). In that invoice, there are 3
lines:

INV_NUMBER LINE_NUMBER P_CODE LINE_UNITS LINE_PRICE


1003 1 13-Q2/P2 1 38.95
1003 2 1546-QQ2 1 39.95
1003 3 13-Q2/P2 5 14.99

In this case, the total amount for this customer can be derived from (1*38.95) +
(1*39.95) + (5*14.99) = 153.85.

The partial output is shown below.

(6) Write a query to display the total amount from all invoices for customers whose
first name begins with A or M. The output should show customer code,
customer first name, customer last name and the total amount for each
customer. The output should be displayed in an alphabetical order by customer
first name similar to the one shown below.

3
BSYS602 Business Data Management
(7) Write a query to display product code, quantity on hand, the number of
products ordered, and adjusted quantity on hand (quantity on hand – the
number of products ordered). The output should be sorted by product code. For
example, the product code “13-02/P2” was in invoice 1001 (1 unit), invoice
1003 (5 units), invoice 1007 (2 units). So the number of products ordered is 8.
Since its quantity on hand is 32, the adjusted quantity on hand is 24. The
output should be similar to the one shown below.

(8) Write a query to display invoice number, line number, the line amount, and the
difference between the line amount and the average line amount. The output
should have appropriate headings and two decimal places for numbers. The
partial output is shown below.

(9) Create your own SELECT statement that joins data from at least two tables
using two conditions in the WHERE clause. The query must include the
GROUP BY clause. The output must be appropriately formatted. Your query
has to make logical and business sense.

(10) Create your own SELECT statement that uses one of the subqueries (i.e.,
WHERE, IN, HAVING or attribute list subqueries). The query must include the
ORDER BY clause. The output must be appropriately formatted. Your query
has to make logical and business sense.

4
BSYS602 Business Data Management

Das könnte Ihnen auch gefallen