0
0
PostgreSQLquery~20 mins

Array data type in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
1:30remaining
Output of selecting an array element
What is the output of this query?

SELECT ARRAY[10, 20, 30][2];
PostgreSQL
SELECT ARRAY[10, 20, 30][2];
A20
B10
C30
DARRAY[20]
Attempts:
2 left
💡 Hint
Remember that PostgreSQL arrays are 1-indexed.
query_result
intermediate
1:30remaining
Result of concatenating arrays
What is the result of this query?

SELECT ARRAY[1,2] || ARRAY[3,4];
PostgreSQL
SELECT ARRAY[1,2] || ARRAY[3,4];
AARRAY[1,2,3,4]
BARRAY[4,3,2,1]
CARRAY[1,2]
DARRAY[3,4]
Attempts:
2 left
💡 Hint
The || operator concatenates arrays in PostgreSQL.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in array declaration
Which option contains a syntax error when declaring an array in PostgreSQL?
ASELECT ARRAY['a', 'b', 'c'];
BSELECT ARRAY[1, 2, 3];
CSELECT ARRAY[1,2,3]::int[];
DSELECT ARRAY(1, 2, 3);
Attempts:
2 left
💡 Hint
ARRAY constructor uses square brackets, not parentheses.
optimization
advanced
2:00remaining
Efficiently check if an element exists in an array
Which query is the most efficient way to check if the integer 5 exists in the integer array column 'numbers'?
ASELECT * FROM table WHERE 5 = ANY(numbers);
BSELECT * FROM table WHERE 5 IN (SELECT unnest(numbers));
CSELECT * FROM table WHERE numbers @> ARRAY[5];
DSELECT * FROM table WHERE array_position(numbers, 5) IS NOT NULL;
Attempts:
2 left
💡 Hint
Use the array containment operator for best performance with indexes.
🧠 Conceptual
expert
2:30remaining
Understanding multidimensional arrays in PostgreSQL
Given the array declaration:

ARRAY[[1,2],[3,4]]

What is the result of the query?

SELECT ARRAY[[1,2],[3,4]][1][2];
PostgreSQL
SELECT ARRAY[[1,2],[3,4]][1][2];
A4
B2
C3
D1
Attempts:
2 left
💡 Hint
Remember that arrays are 1-indexed and multidimensional arrays are arrays of arrays.