PostgreSQL Database Quiz Questions

Course Name:PostgreSQL
Chapter Name:Chapter 9 - Advanced Topics
Lesson Content Link:DB Function
Current Quiz Count:30
Progress
0%
Q1
True / False

A function in PostgreSQL can return a single value.

Q2
True / False

PostgreSQL functions are created using the CREATE FUNCTION command.

Q3
True / False

Functions in PostgreSQL cannot accept input parameters.

Q4
True / False

In PostgreSQL, functions can return a table as a result.

Q5
True / False

Functions in PostgreSQL can be written in multiple languages, such as SQL, PL/pgSQL, and PL/Python.

Q6
True / False

PostgreSQL functions can include transaction control commands (COMMIT, ROLLBACK).

Q7
True / False

PostgreSQL supports the creation of immutable functions that always produce the same result given the same input.

Q8
True / False

You can create a function in PostgreSQL that performs dynamic SQL execution using the EXECUTE statement.

Q9
True / False

PostgreSQL functions can use the ORACLE-specific DECODE function for conditional logic.

Q10
True / False

When migrating functions from ORACLE to PostgreSQL, all PL/SQL functions can be directly transferred without any modification.

Q11
Single Choice

What is the purpose of the NOW() function in PostgreSQL?

Q12
Single Choice

Which PostgreSQL function is used to convert a string to uppercase?

Q13
Single Choice

How do you calculate the length of a string in PostgreSQL?

Q14
Single Choice

What does the PostgreSQL TO_CHAR() function do?

Q15
Single Choice

Which PostgreSQL function is used to extract the day from a date or timestamp?

Q16
Single Choice

In PostgreSQL, how can you generate a random number between 0 and 1?

Q17
Single Choice

What is the output of the following PostgreSQL query?

SQL Code
SELECT ROUND(123.456, 2);
Q18
Single Choice

Which PostgreSQL function would you use to convert a timestamp to a different time zone?

Q19
Single Choice

How can you concatenate two strings in PostgreSQL?

Q20
Single Choice

Given the following PostgreSQL query, what will be the result? sql

SQL Code
SELECT COALESCE(NULL, 'PostgreSQL', 'SQL');
Q21
Multiple Choice

What is a DB function in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION calculate_tax(price NUMERIC)
RETURNS NUMERIC AS $$
BEGIN
 RETURN price * 0.10;
END;
$$ LANGUAGE plpgsql;
Q22
Multiple Choice

How can you execute a function in PostgreSQL?

SQL Code
SELECT get_customer_balance(101);
Q23
Multiple Choice

What is the difference between a function and a stored procedure in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION get_order_total(order_id INT)
RETURNS NUMERIC AS $$
DECLARE
 total NUMERIC;
BEGIN
 SELECT SUM(amount) INTO total
 FROM order_items
 WHERE order_id = order_id;
 RETURN total;
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE PROCEDURE display_order_total(order_id INT)
LANGUAGE plpgsql AS $$
DECLARE
 total NUMERIC;
BEGIN
 total := get_order_total(order_id);
 RAISE NOTICE 'Order Total: %', total;
END;
$$;
Q24
Multiple Choice

Can functions in PostgreSQL have multiple input parameters?

SQL Code
Write a SQL function named 'calculate_discounted_price' that takes the original price and a discount percentage as input and returns the discounted price. Include the SQL code to create the function.

```sql
CREATE OR REPLACE FUNCTION calculate_discounted_price(original_price NUMERIC, discount_percentage NUMERIC)
RETURNS NUMERIC AS $$
BEGIN
 RETURN original_price - (original_price * discount_percentage / 100);
END;
$$ LANGUAGE plpgsql;
```
Q25
Multiple Choice

How can you handle NULL values in a PostgreSQL function?

SQL Code
CREATE OR REPLACE FUNCTION safe_divide(numerator NUMERIC, denominator NUMERIC)
RETURNS NUMERIC AS $$
BEGIN
 IF denominator IS NULL OR denominator = 0 THEN
 RETURN NULL;
 ELSE
 RETURN numerator / denominator;
 END IF;
END;
$$ LANGUAGE plpgsql;
Q26
Multiple Choice

What are the performance considerations when using functions in PostgreSQL?

SQL Code
CREATE OR REPLACE FUNCTION bulk_calculate_tax(prices NUMERIC[])
RETURNS NUMERIC[] AS $$
DECLARE
 taxes NUMERIC[] := '{}';
BEGIN
 FOREACH price IN ARRAY prices LOOP
 taxes := array_append(taxes, price * 0.10);
 END LOOP;
 RETURN taxes;
END;
$$ LANGUAGE plpgsql;
Q27
Multiple Choice

Can functions in PostgreSQL return composite types?

SQL Code
CREATE TYPE product_details AS (
 name VARCHAR,
 price NUMERIC,
 stock INT
);

CREATE OR REPLACE FUNCTION get_product_details(product_id INT)
RETURNS product_details AS $$
DECLARE
 details product_details;
BEGIN
 SELECT name, price, stock INTO details
 FROM products
 WHERE id = product_id;
 RETURN details;
END;
$$ LANGUAGE plpgsql;
Q28
Multiple Choice

How can you return multiple rows from a PostgreSQL function?

SQL Code
CREATE OR REPLACE FUNCTION get_top_customers()
RETURNS TABLE(customer_id INT, total_amount NUMERIC) AS $$
BEGIN
 RETURN QUERY
 SELECT customer_id, SUM(amount) as total_amount
 FROM orders
 GROUP BY customer_id
 ORDER BY total_amount DESC
 LIMIT 5;
END;
$$ LANGUAGE plpgsql;
Q29
Multiple Choice

Can functions in PostgreSQL execute dynamic SQL?

SQL Code
CREATE OR REPLACE FUNCTION dynamic_query_executor(table_name TEXT, condition TEXT)
RETURNS INT AS $$
DECLARE
 query TEXT;
 result INT;
BEGIN
 query := format('SELECT COUNT(*) FROM %I WHERE %s', table_name, condition);
 EXECUTE query INTO result;
 RETURN result;
END;
$$ LANGUAGE plpgsql;
Q30
Multiple Choice

What is the use of the RETURNS VOID clause in a PostgreSQL function?

SQL Code
CREATE OR REPLACE FUNCTION log_activity(user_id INT, activity_description TEXT)
RETURNS VOID AS $$
BEGIN
 INSERT INTO activity_log(user_id, activity_description)
 VALUES (user_id, activity_description);
END;
$$ LANGUAGE plpgsql;