Challenge - 5 Problems
Composite Types Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2: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;
Attempts:
2 left
💡 Hint
Access composite type fields using dot notation like
info.name.✗ Incorrect
The query filters employees with age > 30 and selects the
name and age fields from the composite type column info. Only Bob and Carol satisfy the condition.📝 Syntax
intermediate1: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)?Attempts:
2 left
💡 Hint
Use the
CREATE TYPE ... AS syntax for composite types.✗ Incorrect
PostgreSQL uses
CREATE TYPE name AS (field1 type1, ...) to define composite types. Other options are invalid syntax.❓ optimization
advanced2: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?Attempts:
2 left
💡 Hint
Access composite fields directly with dot notation.
✗ Incorrect
Option A correctly accesses the email field inside the composite type and uses LIKE to filter emails ending with '@example.com'. Option A is valid but unnecessary parentheses add overhead. Option A tries to apply LIKE to the whole composite type, which is invalid. Option A checks equality, not pattern matching.
🔧 Debug
advanced2:00remaining
Debugging composite type insertion error
You run this insertion:
and get an error. Why?
INSERT INTO employees (info) VALUES ('John', 30);and get an error. Why?
Attempts:
2 left
💡 Hint
Composite type columns expect a single composite value, not separate values.
✗ Incorrect
You must insert composite type values as a single composite value, e.g., ROW('John', 30) or ('John', 30). Just listing values separated by commas is invalid.
🧠 Conceptual
expert3: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?Attempts:
2 left
💡 Hint
Functions returning composite types return one column of that type unless expanded.
✗ Incorrect
When a function returns a composite type, the result is a single column of that composite type. To get separate columns, you must use
SELECT (func()).* or alias the fields.