You are on page 1of 4

CS 276 Spring 2010 PROBLEM SET 1

Problem Set 1 Instructions: Save the answers to the following questions in your schema and name it:
PS1YourName Example: PS1DaveBecker

1. In Application Express:
Create and execute a simple anonymous block that outputs your name.

2. Create and execute a simple anonymous block that does the following:
Declares a variable of datatype DATE and populates it with the date that is six months from today
Outputs In six months, the date will be: <insert date>.

3. Write a SQL statement that will return data that looks like this:



4. Due to a successful global health program, the life expectancy at birth will increase by 7 years for
countries whose median ages are below 20. Display the current and increased life expectancy for these
countries in the wf_countries table. Name the calculated column Improved Expectancy.

5. Write a SQL statement that lists the country names in alphabetical order. Display the results in uppercase
and give the column a heading of NAME.

6. Display all the languages in the wf_languages table that start with f. Use the lowercase f in your
SQL statement.

7. From the wf_world_regions table, display the ID, name, and an abbreviation that is the first three
characters of the region name.

8. Modify your SQL statement so that the abbreviation is the first three characters of the region name
followed by the length of the region name. For example: Western Asia would be Wes12.

9. Display all country names from the wf_countries table, along with the life expectancy, rounded to the
nearest whole number.

10. Write a SQL statement to list country names and capitals. If the capital is null, display it as none listed.

11. Create the following anonymous block:

BEGIN
DBMS_OUTPUT.PUT_LINE('Hello World');
END;

A. Add a declarative section to this PL/SQL block. In the declarative section, declare the following
variables:
The today variable of the DATE type. Initialize today with sysdate.
The tomorrow variable of the today type. Use the %TYPE attribute to declare this variable.

B. In the executable section, initialize the tomorrow variable with an expression that calculates
tomorrows date (add 1 to the value in today). Print the value of today and tomorrow after printing
Hello World.

12. Write and test an equijoin statement that lists each countrys name, currency code, and currency name.
Order the list by country name.

13. Write and test an equijoin statement that lists each language and the country or countries where it is
official. Make sure the list is alphabetical by language and then by country. . (Hint: you need to join 3 tables
wf_countries, wf_spoken_languages, wf_languages)

14. Write a SQL statement that lists the name of the employee with the earliest hire date.

15. Which country has the smallest area?

16. List the name of each country and the number of languages spoken in it. Order the results by the number
of languages, from the most to the least.

17. List the name of each currency and the number of countries it is used in. Restrict the list to those
currencies which are used in more than one country.

18. Write an anonymous PL/SQL block that uses the programmers full name and then returns the number of
characters in the name.

19. Write an anonymous PL/SQL block that uses todays date and outputs it in the format of Month DD,
YYYY. Store the date in a DATE variable called my_date. Create another variable of the date type called
v_last_day. Assign v_last_day to the last day of this month. Display the output.







20. The following code is supposed to display the lowest and highest elevations for a country name entered
by the user. However, the code does not work. Fix the code by following the guidelines for retrieving data:

DECLARE
v_country_name wf_countries.country_name%TYPE
:= United States of America;
v_lowest_elevation wf_countries.lowest_elevation%TYPE;
v_highest_elevation wf_countries.highest_elevation%TYPE;
BEGIN
SELECT lowest_elevation, highest_elevation
FROM wf_countries;
DBMS_OUTPUT.PUT_LINE('The lowest elevation in
'||country_name||' is '||v_lowest_elevation
|| and the highest elevation is '||
v_highest_elevation||'.');
END;

NOTE: Create the endangered_species table by running the following statement in Application Express:

CREATE TABLE endangered_species
(species_id NUMBER(4)
CONSTRAINT es_spec_pk PRIMARY KEY,
common_name VARCHAR2(30)
CONSTRAINT es_com_name_nn NOT NULL,
scientific_name VARCHAR2(30)
CONSTRAINT es_sci_name_nn NOT NULL);

21. Examine the following block. If you were to run this block, what data do you think would be saved in
the database?

BEGIN
INSERT INTO endangered_species
VALUES (100, 'Polar Bear','Ursus maritimus');
SAVEPOINT sp_100;
INSERT INTO endangered_species
VALUES (200, 'Spotted Owl','Strix occidentalis');
SAVEPOINT sp_200;
INSERT INTO endangered_species
VALUES (300, 'Asiatic Black Bear','Ursus thibetanus');
ROLLBACK TO sp_100;
COMMIT;
END;

22. Write a PL/SQL block to find the total monthly salary paid by the company for a given department
number from the employees table. Display a message indicating whether the total salary is greater than or less
than $19,000. Test your block twice using the Administration department (department_id =10) and the IT
department (department_id =60). The IT department should be greater than $19,000, while the
Administration departments total should be less than $19,000.

a. What happens if we use the Marketing department (department_id=20) in the previous script?

b. Alter the PL/SQL code to include an ELSIF to handle this situation.

c. Use a CASE statement:

23. Write a PL/SQL block to select the number of countries using a supplied currency name. If the number of
countries is greater than 20, display More than 20 countries. If the number of countries is between 10 and
20, display Between 10 and 20 countries. If the number of countries is less than 10, display Fewer than 10
countries. Use a CASE statement.

Test your code using the following data:


Fewer than 10
countries

Between 10 and 20
countries

More than 20
countries
US Dollar X
Swiss Franc X
Euro X

24. Write a PL/SQL block to display the country_id and country_name values from the WF_COUNTRIES
table for country_id whose values range from 1 through 3. Use a basic loop. Increment a variable from 1
through 3. Use an IF statement to test your variable and EXIT the loop after you have displayed the first 3
countries.


25. Modify your solution to question 24 above, replacing the IF statement with an EXIT....WHEN statement.

You might also like