0
0
PostgreSQLquery~20 mins

Type casting with :: operator in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Casting Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What is the output of this type casting query?
Consider the following PostgreSQL query:

SELECT '123'::integer + 10 AS result;

What is the value of result?
PostgreSQL
SELECT '123'::integer + 10 AS result;
A'12310'
B133
CSyntax error
DType error
Attempts:
2 left
💡 Hint
Casting '123' to integer allows arithmetic addition.
📝 Syntax
intermediate
2:00remaining
Which option causes an error in type casting?
Which of the following PostgreSQL queries will cause an error due to invalid use of the :: operator?
ASELECT 'abc'::integer;
BSELECT 123::text;
CSELECT '123'::integer + 10;
DSELECT '2023-01-01'::date;
Attempts:
2 left
💡 Hint
Casting a non-numeric string to integer is invalid.
query_result
advanced
2:00remaining
What is the output of this complex type casting?
Given the query:

SELECT ('2023-06-15 14:30:00'::timestamp)::date AS cast_date;

What is the value of cast_date?
PostgreSQL
SELECT ('2023-06-15 14:30:00'::timestamp)::date AS cast_date;
AType error
B2023-06-15 14:30:00
CSyntax error
D2023-06-15
Attempts:
2 left
💡 Hint
Casting timestamp to date removes the time part.
🔧 Debug
advanced
2:00remaining
Why does this query fail?
Examine the query:

SELECT '12.34'::integer;

Why does this query fail?
PostgreSQL
SELECT '12.34'::integer;
ABecause integer type does not exist in PostgreSQL
BBecause the :: operator is not valid for casting strings
CBecause '12.34' is a string that cannot be cast to integer due to decimal point
DBecause the query is missing a FROM clause
Attempts:
2 left
💡 Hint
Integer casting requires whole numbers without decimals.
optimization
expert
3:00remaining
Which query is more efficient for casting and filtering?
You want to select rows where the text column price_text represents an integer greater than 100.

Which query is more efficient in PostgreSQL?
ASELECT * FROM products WHERE price_text::integer > 100;
BSELECT * FROM products WHERE price_text > '100';
CSELECT * FROM products WHERE CAST(price_text AS integer) > 100;
DSELECT * FROM products WHERE price_text::text > '100';
Attempts:
2 left
💡 Hint
The :: operator is a shorthand for CAST and often preferred for readability and performance.