Top Banner
 Self-Paced SQL Exercise Version 3.5
42

SQL_Self_Paced_Training

Apr 08, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 1/42

 

Self-Paced SQL Exercise 

Version 3.5

Page 2: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 2/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 2

Contents

1  Introduction 4 1.1  Reference Material ................................................................. 4 1.2  Install Ingres ......................................................................... 4 

2  Setup 5 2.1  Extract setup Materials ........................................................... 5 2.2  Creating a Database and loading data....................................... 7 

2.2.1  Creating database and Connecting ................................... 7 2.2.2

 Building Tables .............................................................. 8

 2.2.2.1  Getting Help........................................................ 8 2.2.2.2  Creating Tables ................................................... 9 

2.2.3  Loading Tables with Data .............................................. 10 2.2.4  Verify the Data Load .................................................... 10 2.2.5  Troubleshooting .......................................................... 11 

3  Database Design 13 4  Exercises 14 

4.1  Simple Retrievals ................................................................. 14 4.1.1  Using Simple Views...................................................... 16 

4.2  Data Manipulation ................................................................ 17 4.2.1  Adding and changing data ............................................ 17 

4.3  Transaction Processing ......................................................... 20 4.4  Joining Tables ...................................................................... 22 4.5  Views Based on More Than One Table ..................................... 23 SQL Functions ............................................................................. 24 

4.5.1  Conversion and date functions ...................................... 25 4.6  Unions and Subqueries ......................................................... 26 

4.6.1  Changing Data Using Subqueries ................................... 28 5  Solutions 29 

5.1  Simple Retrievals ................................................................. 29 5.1.1  Using Simple Views...................................................... 30 

5.2  Data Manipulation ................................................................ 31 5.2.1  Adding and changing data ............................................ 31 

5.3  Transaction Processing ......................................................... 34 

Page 3: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 3/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 3

5.4  Joining Tables ...................................................................... 36 5.5  Views Based on More than One Table ..................................... 37  5.6  SQL Functions ..................................................................... 38 

5.6.1  Conversion and date functions ...................................... 39 5.7  Unions and Subqueries ......................................................... 40 

5.7.1  Changing Data Using Subqueries ................................... 41 

Page 4: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 4/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 4

1  Introduction

This aim of this document is to recap or introduce the basics of SQL using

Ingres SQL.

1.1  Reference Material

The Ingres SQL reference guide (SQLREF) is available online athttp://docs.ingres.com/

Ingres Corporation also provides instructor-led training on Ingres SQL. Acourse description is published at:

http://www.ingres.com/services/course-IngresSQL.php .

More information on how to attend this training is available athttp://www.ingres.com/services/education.php , or contact IngresEducation at [email protected] .

1.2  Install Ingres

This self-paced training assumes that you have already installed Ingres.

To install Ingres, please download the Ingres installation media from:

http://esd.ingres.com/product/Community_Projects/Ingres_Database  

To find out how to get started with Ingres, check the Ingres webinars:http://www.ingres.com/about/ondemand-webcasts.php  Under the “IngresDBMS” category - “How to Get Started with Ingres”  

Page 5: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 5/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 5

2  Setup

2.1  Extract setup Materials

The self-paced training pack contains this document and a set of setupfiles. Extract these files into an empty folder/directory and open acommand prompt within this area.

Example

Open the zip file:

Click Extract

Choose a target

folder, eg c:\tmp 

Click Extract.

This will create a new folder called setups.

Page 6: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 6/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 6

Open a CommandPrompt in the newfolder

Page 7: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 7/42

Page 8: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 8/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 8

2.2.2  Building Tables

2.2.2.1  Getting Help

1.  Within ISQL, type help; commit; 

eg.

 

Use the Go menu item to execute the statements.

In the output screen, there should be 0 rows,

This verifies that there are no tables available to use yet in thisdatabase.

Use the End menu item to return to the input screen.

Use the same End key to end the ISQL connection. This will return tothe command line, eg.

Page 9: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 9/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 9

2.2.2.2  Creating Tables

1.  From the command line run the command

sql trainingdb < tables.sql

eg

This will create the tables used in your training and will list thecreated tables.

Verify that the tables have been created. The output should show:

Page 10: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 10/42

Page 11: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 11/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 11

2.2.5  Troubleshooting

Ingres errors are reported in the format E_xxxxxx where xxxxxx  is anerror number. If you see an error when doing setups, check the onlinedocumentation (http://docs.ingres.com ).

The following checklist might also provide a good starting point forspecific problems with the setups:

How to start Ingres?

o  If the ingres system is not running, type ingstart 

If successful, you will see the following message.

Or if ingres is already running, you will see the followingmessage:

How to connect to an Ingres database?

o  To test that ingres is running, try a connection to the masterdatabase: sql iidbdb 

If this is successful, you will see a command line prompt:

To quit out of the command line interface, type \q and pressreturn.

Training database does not exist?

o  To check that the database trainingdb has been created, trya simple connection: sql trainingdb 

If the connection is successful, you will see a command lineprompt (as above). Quit out of the command line interface -

type \q and press return.

Page 12: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 12/42

Page 13: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 13/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 13

3  Database Design

For reference, you will be using a Purchase Order database comprising of 

the following tables:

vendor

PK vendorno integer 4

vnamestreetcityregionzipcontact

varchar 30varchar 20varchar 15char 2varchar 10varchar 10

priceinfo

PK, FK2PK, FK1

partnovendorno

integer 4integer 4

catnoprice

varchar 6decimal 10,2

part

PK partno integer 4

description varchar 30

poheader

PK orderrno integer 4

FK1orddatevendornoinvoicenostatus

ingresdateinteger 4varchar 10char 1

podetail

PK, FK2PK, FK1

ordernopartno

integer 4integer 4

qtyunit_pr

integer 2decimal 10,2

There is also a pohistory table which contains data archived from the

poheader table:pohistory

PK orderno integer 4

orddatevendornoinvoicenopototal

ingresdateinteger 4varchar 10money

Page 14: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 14/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 14

4  Exercises

This section contains all the exercises to be carried out. The exercise

solutions are provided in Section 5.

4.1  Simple Retrievals

Either

a.  Using VisualSQL, launch Ingres VisualSQL against the trainingdbdatabase (Start Programs Ingres Ingres VisualSQL).

Select the trainingdb from the dropdown box, eg.

Or

b.  Start up the ISQL Terminal Monitor from the operating system:

isql trainingdb 

Page 15: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 15/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 15

1.  Display all the rows in the part table, sorted by description.(9 rows)

2.  Display the part description for the part number 4.(1 row)

3.  Display the part numbers currently priced at 230.45 (Hint - currentprices are stored in the priceinfo table).

(4 rows)

4.  Redisplay the numbers eliminating the duplicates.(3 rows)

5.  Display all parts with descriptions beginning with „Pine‟. (3 rows)

Page 16: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 16/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 16

4.1.1  Using Simple Views

1.  Select all columns of the podetail  table that have a quantity greater

than 500.

(3 rows) 

2.  Using the last SQL statement, create a view called vw_large_orders,which includes all columns in the podetail  table which have aquantity greater than 500.

3.  Using the new view, retrieve all details for part numbers greater than2.

(2 rows) 

Page 17: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 17/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 17

4.2  Data Manipulation

4.2.1  Adding and changing data

1.  Add a new row to the part  table with the following information:

Part number 10 Description „Desk chair’ 

(1 row) 

2.  Check the result by retrieving the part information for part number10.

3.  Add a new row to the poheader table with the following information:

Order Number 3010 Order Date ‘today’ Vendor 205 (Furniture HeavenInvoice number „inv0101’ (Leave the status blank)

(1 row)

4.  Check the result by retrieving the order information for order 3010.

Note that the order date value will be different in your output. Notealso that the status column has a default value of „P‟ (Pending). 

5.  Vendor 205 (Furniture Heaven) is no longer supplying Part 5 (ElmTables).Remove this information from the priceinfo table; (Hint: If you areunsure, select the data, check the result of the select, and then alterthe SQL statement from a SELECT to a DELETE).

(1 row)First check the data.

Then delete the data

Page 18: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 18/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 18

6.  Check the result by retrieving all vendors who still supply part 5.(3 rows)

7.  Select the details from the poheader   table that have a status of „c’ (completed). Do not include the status in the results

(3 rows)

8.  Add these rows of data from the poheader  table into the pohistory .Do not worry about the pototal column at this point.

(3 rows) 

9. 

Check the result by retrieving the orders from the pohistory table.

10.  Delete from the poheader table, those orders that are now duplicatedin the pohistory table (i.e. with a status of „c’).

(3 rows)

11.  Check the result of the last statement by retrieving all remainingrows in the poheader table.

(4 rows)

Page 19: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 19/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 19

12.  Change the invoiceno for order number 3010 to „inv00824‟. 

(1 row)

13.  Check the result by retrieving the details for order number 3010.

Page 20: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 20/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 20

4.3  Transaction Processing

1  Change the price entries in the priceinfo table which are supplied byVendor no 201. Increase these prices by 1000.00. DO NOTCOMMIT YOUR CHANGES. 

(5 rows) 

2  Verify your Results but DO NOT COMMIT YOUR CHANGES. 

3  Vendor 201 did not raise their prices by 1000.00. Rollback the abovechanges. Verify your results

(5 rows)

4  Change the prices of the parts in the priceinfo table which aresupplied by Vendor no 201. Increase these prices by 100.00. Verifyand commit your changes.

(5 Rows)

5  Delete all rows in the part  table but DO NOT COMMIT YOUR 

CHANGES. (10 rows) 

Page 21: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 21/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 21

6  Verify that all rows are missing from the part  table but DO NOTCOMMIT YOUR CHANGES.

(0 rows) 

7  Rollback the statement you just entered. Verify that all rows in thepart table still exist.

(10 rows)

Page 22: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 22/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 22

4.4  Joining Tables

1.  Retrieve Order, Part and Quantity information from the viewvw_large_orders.

(3 rows) 

2.  Modify the last command to also display Part descriptions. Do not

display the quantity information(3 rows)

3.  Amend the last command so that we see all part numbers, and anylarge orders for those parts.

(? rows)

Page 23: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 23/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 23

4.5  Views Based on More Than One Table

1.  Create a view called vw_part_prices to display the all part numbers,descriptions, prices, catalogue numbers and vendor numbers andnames for priceinfo entries.

2.  Verify your result by retrieving all the data; sort the result ondescription.

(22 rows)

3.  Use the view vw_part_prices to display all the information, sorted bypart number, for parts priced 300.00 or less.

(4 rows)

Page 24: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 24/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 24

SQL Functions

1.  How many vendors are located in the city of Sheffield?(1 row)

2.  Find the unit price and quantity information for order number 3001.All the information is in the podetail table.

(3 rows)

3.  Amend the last query to also include the line item totals for eachpart. Rename the new column “line_total”. 

(3 rows)

4.  Find the total price for order number 3001. Rename the resultcolumn “total price”  

(1 row)

5.  Use the view vw_part_prices to display the lowest price for each partwith a description beginning with „Pine‟  (Note: there are some partswhich do not have price information. These will be shown with a nullprice).

(3 rows) 

Page 25: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 25/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 25

4.5.1  Conversion and date functions

1.  Select the 4 rightmost letters of each part description.

(? rows)

2.  Select the 4 rightmost letters of each part description again butconverted to upper case (Hint: string functions can be nested).

(? rows)

Page 26: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 26/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 26

4.6  Unions and Subqueries

1.  Display the order numbers, dates and status for all orders,completed and pending. Remember that all orders in the pohistorytable are completed. (Hint: Use a union).

(6 rows)

2.  Using the priceinfo table, retrieve prices, Part Numbers and VendorNumbers, sorted on price.

(18 rows) 

Page 27: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 27/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 27

3.  Which parts are priced at the lowest price?(2 rows) 

4.  Which parts are priced at lower than the average price?(12 rows) 

5.  Amend the last command to also include the part descriptions.(12 rows) 

6.  Amend the last command to also include the vendor name, streetand city information.

(12 rows) 

Page 28: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 28/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 28

4.6.1  Changing Data Using Subqueries

1.  All vendors located in „Leeds‟ are increasing their prices by 200.00.Apply this increase to the database.

(12 rows)

2.  Check the result by retrieving all of the updated prices.(12 rows)

End of exercise

Page 29: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 29/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 29

5  Solutions

SQL often provides more than one method of correctly solving a request.

For this reason, there might be alternative solutions to the solutions whichare provided within this section.

5.1  Simple Retrievals

1.  Display all the rows in the part table, sorted by description.

select *from partorder by description;

commit;

2.  Display the part description for the part number 4.

select descriptionfrom partwhere partno = 4;

commit;

3.  Display the part numbers currently priced at 230.45 (Hint - currentprices are stored in the priceinfo table).

select partnofrom priceinfowhere price = 230.45;

commit;

4. Redisplay the numbers eliminating the duplicates

select distinct partnofrom priceinfowhere price = 230.45;

commit;

Page 30: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 30/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 30

5. Display all parts with descriptions beginning with „Pine‟ .

select *

from partwhere description like „Pine%‟; 

commit;

5.1.1  Using Simple Views

1.  Select all columns of the podetail table which a quantity greater than500.

select * from podetail

where qty > 500;

commit;

2.  Using the last SQL statement, create a view called vw_large_orders,which includes all columns in the podetail  table which have aquantity greater than 500.

create view vw_large_orders asselect * from podetailwhere qty > 500;

commit;

3.  Using the new view, retrieve all details for part numbers greater than2.

select * from vw_large_orderswhere partno > 2;

commit;

Page 31: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 31/42

Page 32: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 32/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 32

5.  Vendor 205 (Furniture Heaven) is no longer supplying Part 5 (ElmTables).Remove this information from the priceinfo table; (Hint: If you are

unsure, select the data, check the result of the select, and then alterthe SQL statement from a SELECT to a DELETE).

First check the dataselect *from priceinfowhere vendorno = 205and partno = 5;

Then delete the datadeletefrom priceinfo

where vendorno = 205and partno = 5;

commit;

6.  Check the result by retrieving all vendors who still supply part 5.

select *from priceinfowhere partno = 5;

commit;

7.  Select the details from the poheader   table that have a status of „c’ (completed). Do not include the status in the results.

select orderno, orddate, vendorno, invoicenofrom poheaderwhere status = 'c';commit;

Page 33: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 33/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 33

8.  Add these rows of data from the poheader  table into the pohistory .Do not worry about the pototal column at this point.

insert into pohistory (orderno, orddate, vendorno, invoiceno )select orderno, orddate, vendorno, invoicenofrom poheaderwhere status = 'c';

commit;

9.  Check the result by retrieving the orders from the pohistory table.

select * from pohistory;

commit;

10.  Delete from the poheader table, those orders that are now duplicatedin the pohistory table (i.e. with a status of „c‟) 

delete from poheaderwhere status = 'c';

commit;

11.  Check the result of the last statement by retrieving all remainingrows in the poheader table.

select *from poheader;

commit;

12.  Change the invoiceno for order number 3010 to „inv00824‟. 

update poheaderset invoiceno = „inv00824‟ where orderno = 3010;

commit;

13.  Check the result by retrieving the details for order number 3010.

select *from poheaderwhere orderno = 3010;

commit;

Page 34: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 34/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 34

5.3  Transaction Processing

1.  Change the price entries in the priceinfo table which are supplied byVendor no 201. Increase these prices by 1000.00. DO NOTCOMMIT YOUR CHANGES. 

update priceinfoset price = price + 1000where vendorno = 201;

2.  Verify your results but DO NOT COMMIT YOUR CHANGES. 

select *

from priceinfowhere vendorno = 201;

3.  Vendor 201 did not raise their prices by 1000.00. Rollback the abovechanges. Verify your results.

rollback;

select *from priceinfowhere vendorno = 201;

4.  Change the prices of the parts in the priceinfo table, which aresupplied by Vendor no 201. Increase these prices by 100.00. Verifyand commit your changes.

update priceinfoset price = price + 100where vendorno = 201;

select *from priceinfo

where vendorno = 201;

commit;

5.  Delete all rows in the part  table but DO NOT COMMIT YOUR CHANGES. 

delete from part;

6.  Verify that all rows are missing from the part  table but DO NOTCOMMIT YOUR CHANGES. 

select *from part;

Page 35: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 35/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 35

7.  Rollback the statement you just entered. Verify that all rows in thepart table still exist.

rollback;

select *from part;

commit;

Page 36: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 36/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 36

5.4  Joining Tables

1.  Retrieve Order, Part and Quantity information from the view

vw_large_orders.

select orderno, partno, qtyfrom vw_large_orders;

commit;

2.  Modify the last command to also display part descriptions. Do notdisplay the quantity information.

select v.orderno, p.partno, p.descriptionfrom vw_large_orders v join part pon v.partno = p.partno;

commit;

3.  Amend the last command so that we see all part numbers, and anylarge orders for those parts.

select v.orderno, p.partno, p.descriptionfrom vw_large_orders v right join part pon v.partno = p.partno;

commit;

Page 37: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 37/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 37

5.5  Views Based on More than One Table

1.  Create a view called vw_part_prices to display the all part numbers,descriptions, prices, catalogue numbers and vendor numbers andnames for priceinfo entries.

create view vw_part_prices asselectp.partno,p.description,pi.price,pi.catno,v.vendorno,v.vname

from priceinfo pi join vendor von pi.vendorno = v.vendornoright join part pon p.partno = pi.partno;

commit;

2.  Verify your result by retrieving all the data; sort the result ondescription.

select *

from vw_part_pricesorder by description;

commit;

3.  Use the vw_part_prices t o display all the information, sorted by partnumber, for parts prices 300.00 or less.

select *from vw_part_priceswhere price <= 300

order by partno;

commit;

Page 38: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 38/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 38

5.6  SQL Functions

1.  How many vendors are located in the city of Sheffield?

select count(*)from vendorwhere city = „Sheffield‟; 

commit;

2.  Find the unit price and quantity information for order number 3001.All the information in the podetail table.

select unit_pr, qtyfrom podetailwhere orderno = 3001;

commit;

3.  Amend the last query to also include the line item for each part.Rename the new column “line-total”. 

select unit_pr

, qty, (qty * unit_pr) as line_totalfrom podetailwhere orderno = 3001;

commit;

4.  Find the total price for order number 3001. Rename the resultcolumn “total price”. 

select sum(unit_pr * qty) as “total price” 

from podetailwhere orderno = 3001;

commit;

Page 39: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 39/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 39

5.  Use the view vw_part_prices to display the lowest price for each partwith a description beginning with „Pine‟ (Note: there are some partsthat do not have price information. These will be shown with a null

price).

select description, min(price)from vw_part_priceswhere description like „Pine%‟ group by description;

commit;

5.6.1  Conversion and date functions

1.  Select the 4 rightmost letters of each part description.

select right(description,4)from part;

commit;

2.  Select the 4 rightmost of each part description again, but convertedto upper case (Hint: string functions can be nested).

select uppercase(right(description,4))

from part;

commit;

Page 40: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 40/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 40

5.7  Unions and Subqueries

1.  Display the order numbers, dates and status for all orders,

completed and pending. Remember that all orders in the pohistorytable are completed. (Hint: Use a union).

select orderno, orddate, statusfrom poheaderunionselect orderno, orddate, „c‟ from pohistory;

commit;

2.  Using the priceinfo table, retrieve prices, Part Numbers and VendorNumbers, sorted on price.

select price, partno,vendornofrom priceinfoorder by price

3.  Which parts are prices at the lowest price?

select price, partno,vendornofrom priceinfowhere price =( select min(price)

from priceinfo);

commit;

4.  Which parts are priced at lower than the average price?

select price, partno,vendornofrom priceinfowhere price <( select avg(price)

from priceinfo);

commit;

Page 41: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 41/42

Basic SQL Exercises © Ingres Corporation

© Ingres Corporation Page 41

5.  Amend the last command to also include the part descriptions.

select pa.description, pi.price, pa.partno,pi.vendornofrom priceinfo pi join part paon pi.partno = pa.partnowhere pi.price <( select avg(price)

from priceinfo);

6.  Amend the last command to also include the vendor name, streetand city information.

select pa.description, pi.price, pa.partno,pi.vendorno, ve.vname, ve.street, ve.cityfrom priceinfo pi join part paon pi.partno = pa.partno

join vendor veon pi.vendorno = ve.vendornowhere pi.price <( select avg(price)

from priceinfo);

5.7.1  Changing Data Using Subqueries

1.  All vendors located in „Leeds‟ are increasing their prices by 200.00.Apply this increase to the database.

update priceinfoset price = price + 200where vendorno in(select vendornofrom vendorwhere city = „Leeds‟ );

commit

Page 42: SQL_Self_Paced_Training

8/7/2019 SQL_Self_Paced_Training

http://slidepdf.com/reader/full/sqlselfpacedtraining 42/42

Basic SQL Exercises © Ingres Corporation

2.  Check the result by retrieving all of the updated prices.

select vendorno, pricefrom priceinfowhere vendorno in(select vendornofrom vendorwhere city = „Leeds‟ );

End of Solutions