Arrays let you store multiple values in one column. This helps keep related data together.
Array data type in PostgreSQL
CREATE TABLE table_name ( column_name data_type[], ... ); -- Example: CREATE TABLE students ( id SERIAL PRIMARY KEY, name TEXT, grades INTEGER[] );
The square brackets [] after the data type mean this column holds an array of that type.
You can use any data type inside the array, like INTEGER[], TEXT[], or BOOLEAN[].
CREATE TABLE employees ( id SERIAL PRIMARY KEY, name TEXT, phone_numbers TEXT[] );
INSERT INTO employees (name, phone_numbers) VALUES ('Alice', ARRAY['123-4567', '987-6543']);
SELECT name, phone_numbers[1] AS first_phone FROM employees;
CREATE TABLE empty_array_example ( id SERIAL PRIMARY KEY, tags TEXT[] ); INSERT INTO empty_array_example (tags) VALUES (ARRAY[]::TEXT[]);
This program creates a books table with an array column for authors. It inserts two books, one with two authors and one with one author, then selects all rows.
CREATE TABLE books ( id SERIAL PRIMARY KEY, title TEXT, authors TEXT[] ); INSERT INTO books (title, authors) VALUES ('Learn SQL', ARRAY['John Doe', 'Jane Smith']), ('Cooking 101', ARRAY['Chef Mike']); SELECT id, title, authors FROM books;
Access array elements using square brackets, like column_name[1] for the first element.
Arrays start at index 1 in PostgreSQL, not 0.
Arrays can make queries simpler but can be harder to search deeply compared to normalized tables.
Arrays store multiple values in one column using square brackets.
Use arrays when you want to keep related lists together without extra tables.
Remember PostgreSQL arrays start at index 1, and you can access elements with column[index].
Practice
scores in a PostgreSQL table?Solution
Step 1: Understand PostgreSQL array syntax
PostgreSQL usestype[]to declare an array column, for exampleinteger[].Step 2: Match the syntax to the options
Onlyscores integer[]uses the correct syntaxscores integer[].Final Answer:
scores integer[] -> Option AQuick Check:
Array declaration = type[] [OK]
type[] to declare arrays in PostgreSQL [OK]- Using parentheses instead of square brackets
- Writing 'array(integer)' which is invalid syntax
- Trying to put brackets before the type
{10, 20, 30} into a column numbers in PostgreSQL?Solution
Step 1: Know how to insert arrays in PostgreSQL
Arrays are inserted as string literals with curly braces, like'{10,20,30}'.Step 2: Check each option
INSERT INTO table_name (numbers) VALUES ('{10,20,30}');uses the correct string literal format.INSERT INTO table_name (numbers) VALUES ([10, 20, 30]);uses square brackets which is invalid.INSERT INTO table_name (numbers) VALUES (ARRAY(10, 20, 30));uses wrong ARRAY syntax (should be ARRAY[10,20,30]).INSERT INTO table_name (numbers) VALUES (10, 20, 30);inserts separate values, not an array.Final Answer:
INSERT INTO table_name (numbers) VALUES ('{10,20,30}'); -> Option AQuick Check:
Array insert = string with curly braces [OK]
- Using square brackets instead of curly braces
- Using ARRAY() with parentheses instead of ARRAY[]
- Inserting multiple values without array syntax
students with a column grades integer[], what will the query SELECT grades[2] FROM students WHERE id = 1; return if the row with id = 1 has grades = {85, 90, 78}?Solution
Step 1: Understand PostgreSQL array indexing
PostgreSQL arrays start at index 1, sogrades[2]accesses the second element.Step 2: Identify the second element in the array
The array is{85, 90, 78}, so the second element is 90.Final Answer:
90 -> Option CQuick Check:
Array index 2 = 90 [OK]
- Assuming array index starts at 0
- Confusing element positions
- Expecting entire array instead of single element
SELECT numbers[0] FROM data_table; where numbers is an integer array column. What is the likely result?Solution
Step 1: Recall PostgreSQL array indexing rules
PostgreSQL arrays start at index 1, so index 0 is invalid but does not cause syntax error.Step 2: Understand behavior of accessing index 0
Accessing index 0 returns NULL, not an error or the first element.Final Answer:
Returns NULL because PostgreSQL arrays start at index 1 -> Option BQuick Check:
Index 0 access = NULL [OK]
- Expecting index 0 to return first element
- Thinking it causes syntax error
- Assuming it returns entire array
orders with a column item_ids integer[]. How would you write a query to find all orders that include the item ID 42 anywhere in the array?Solution
Step 1: Understand how to check if an array contains a value
PostgreSQL provides theANY()operator to check if a value exists in an array.Step 2: Analyze each option
SELECT * FROM orders WHERE 42 = ANY(item_ids);correctly uses42 = ANY(item_ids).SELECT * FROM orders WHERE item_ids[42] IS NOT NULL;incorrectly tries to access index 42.SELECT * FROM orders WHERE ARRAY[42] @> item_ids;uses@>which checks if array contains another array, but syntax is reversed (should beitem_ids @> ARRAY[42]).SELECT * FROM orders WHERE item_ids[1] = 42;only checks first element.Final Answer:
SELECT * FROM orders WHERE 42 = ANY(item_ids); -> Option DQuick Check:
Use ANY() to check value in array [OK]
value = ANY(array) to check membership [OK]- Using wrong operator like @> with reversed operands
- Checking only first element instead of whole array
- Trying to access array at index equal to value
Ready to prove it?
Pass the test and this pattern moves unknown
