0
0
PostgreSQLquery~20 mins

Composite types in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composite Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of selecting composite type fields
Given the composite type person defined as CREATE TYPE person AS (name TEXT, age INT); and a table employees with a column info of type person, what is the output of this query?

SELECT info.name, info.age FROM employees WHERE info.age > 30;
PostgreSQL
CREATE TYPE person AS (name TEXT, age INT);
CREATE TABLE employees (id SERIAL PRIMARY KEY, info person);
INSERT INTO employees (info) VALUES (ROW('Alice', 28)), (ROW('Bob', 35)), (ROW('Carol', 40));

SELECT info.name, info.age FROM employees WHERE info.age > 30;
A[{"name": "Bob", "age": 35}, {"name": "Carol", "age": 40}]
B[{"info": {"name": "Bob", "age": 35}}, {"info": {"name": "Carol", "age": 40}}]
C[{"name": "Alice", "age": 28}]
DSyntaxError
Attempts:
2 left
💡 Hint
Access composite type fields using dot notation like info.name.
📝 Syntax
intermediate
1:30remaining
Correct syntax to create a composite type
Which of the following is the correct syntax to create a composite type address with fields street (TEXT), city (TEXT), and zip (INT)?
ACREATE COMPOSITE TYPE address (street TEXT, city TEXT, zip INT);
BCREATE TYPE address AS (street TEXT, city TEXT, zip INT);
CCREATE TYPE address {street TEXT, city TEXT, zip INT};
DCREATE TYPE address (street TEXT, city TEXT, zip INT);
Attempts:
2 left
💡 Hint
Use the CREATE TYPE ... AS syntax for composite types.
optimization
advanced
2:30remaining
Optimizing queries with composite types
You have a table orders with a composite type column customer_info (name TEXT, email TEXT). You want to find all orders where the customer's email ends with '@example.com'. Which query is the most efficient?
ASELECT * FROM orders WHERE customer_info.email LIKE '%@example.com';
BSELECT * FROM orders WHERE (customer_info).email LIKE '%@example.com';
CSELECT * FROM orders WHERE customer_info LIKE '%@example.com';
DSELECT * FROM orders WHERE customer_info.email = '@example.com';
Attempts:
2 left
💡 Hint
Access composite fields directly with dot notation.
🔧 Debug
advanced
2:00remaining
Debugging composite type insertion error
You run this insertion:
INSERT INTO employees (info) VALUES ('John', 30);
and get an error. Why?
AYou need to cast the values to the composite type explicitly.
BThe column info does not exist in employees table.
CThe age field must be a string, not an integer.
DYou must insert a composite type value using ROW() or parentheses, not separate values.
Attempts:
2 left
💡 Hint
Composite type columns expect a single composite value, not separate values.
🧠 Conceptual
expert
3:00remaining
Understanding composite type behavior in functions
Consider a PostgreSQL function that returns a composite type person (name TEXT, age INT). What is the result of calling this function in a SELECT statement without aliasing the composite fields?
AThe function returns JSON objects representing the composite type.
BThe function returns separate columns named name and age automatically.
CThe result is a single column of type person containing composite values.
DThe query will raise a syntax error because composite types cannot be returned.
Attempts:
2 left
💡 Hint
Functions returning composite types return one column of that type unless expanded.