Practice PLSQL SEC 4
Practice PLSQL SEC 4
1
1. What is the purpose of a conditional control structure in PL/SQL?
2. List the three categories of control structures in PL/SQL.
3. List the keywords that can be part of an IF statement. A
4. List the keywords that are a required part of an IF statement.
5. Write a PL/SQL block to find the population of a given country in the countries table.
Display a message indicating whether the population is greater than or less than 1
billion (1,000,000,000). Test your block twice using India (country_id = 91) and
United Kingdom (country_id = 44). India’s population should be greater than 1
billion, while United Kingdom’s should be less than 1 billion.
6. Modify the code from the previous exercise so that it handles all the following cases:
A. Population is greater than 1 billion.
Run your code using the following country ids. Confirm the indicated results.
China (country_id = 86): Population is greater than 1 billion.
United Kingdom (country_id = 44): Population is greater than 0
Antarctica (country_id = 672): Population is 0.
Europa Island (country_id = 15): No data for this country.
7. Examine the following code:
DECLARE
v_country_id wf_countries.country_name%TYPE := <a value>;
v_ind_date wf_countries.date_of_independence%TYPE;
v_natl_holiday wf_countries.national_holiday_date%TYPE;
BEGIN
SELECT date_of_independence, national_holiday_date
INTO v_ind_date, v_natl_holiday
FROM wf_countries
WHERE country_id=v_country_id;
IF v_ind_date IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('A');
ELSIF v_natl_holiday IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('B');
ELSIF v_natl_holiday IS NULL AND v_ind_date IS NULL THEN
DBMS_OUTPUT.PUT_LINE('C');
END IF;
END;
A. What would print if the country has an independence date equaling NULL and
a national holiday date equaling NULL? C
B. What would print if the country has an independence date equaling NULL and
a national holiday date containing a value? B
C. What would print if the country has an independence date equaling a value
and a national holiday date equaling NULL? A
D. Run a SELECT statement against the COUNTRIES table to determine
whether the following countries have independence dates or national holiday
dates, or both. Predict the output of running the anonymous block found at the
beginning of this question.
Independence National Output
Country Country_ID
Date Holiday Date Should be
Antartica 672 N N
Iraq 964 Y N
Spain 34 N Y
United States 1 Y Y
E. Finally, run the anonymous block found at the beginning of this question using
each of the above country ids as input. Check whether your output answers are
correct.
8. Examine the following code. What output do you think it will produce?
DECLARE
v_num1 NUMBER(3) := 123;
v_num2 NUMBER;
BEGIN
IF v_num1 <> v_num2 THEN
DBMS_OUTPUT.PUT_LINE('The two numbers are not equal');
ELSE
DBMS_OUTPUT.PUT_LINE('The two numbers are equal');
END IF;
END;
Run the code to check if your prediction was correct. What was the result and why?
Modify the code to use various comparison operators to see different results.
9. Write a PL/SQL block to accept a year and check whether it is a leap year. For
example, if the year entered is 1990, the output should be “1990 is not a leap year.”
Hint: A leap year should be exactly divisible by 4, but not exactly divisible by 100.
However, any year exactly divisible by 400 is a leap year.
Test your solution with the following years:
Practice 4.2
1. Write a PL/SQL block:
A. To find the number of airports from the countries table for a supplied
country_name. Based on this number, display a customized message as
follows:
B. Test your code for the following countries and confirm the results
2. Write a PL/SQL block:
A. To find the amount of coastline for a supplied country name. Use the countries
table. Based on the amount of coastline for the country, display a customized
message as follows:
B. Test your code for the following countries and confirm the results.
3. Use a CASE statement:
A. 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.
B. Test your code using the following data:
4. Examine the following code.
A. What do you think the output will be? Test your prediction by running the
code
DECLARE
x BOOLEAN := FALSE;
y BOOLEAN;
v_color VARCHAR(20) := 'Red';
BEGIN
IF (x OR y)
THEN v_color := 'White';
ELSE
v_color := 'Black';
END IF;
DBMS_OUTPUT.PUT_LINE(v_color);
END;.
B. Change the declarations to x and y as follows. What do you think the output
will be? Test your prediction by running the code again
x BOOLEAN ;
y BOOLEAN ;
C. Change the declarations to x and y as follows. What do you think the output
will be? Test your prediction by running the code again.
x BOOLEAN := TRUE;
y BOOLEAN := TRUE;
D. Experiment with changing the OR condition to AND.
Practice 4.3
1. What purpose does a loop serve in PL/SQL?
2. List the types of loops in PL/SQL
3. What statement is used to explicitly end a loop?
4. Write a PL/SQL block to display the country_id and country_name values from the
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.
2. Write a PL/SQL block to display the country_id and country_name values from the
COUNTRIES table for country_id whose values range from 51 through 55 in the
reverse order. Use a FOR loop.