Challenge - 5 Problems
PostgreSQL Advanced Features Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the output of this query using JSONB operators?
Given a table
products with a details column of type jsonb, what will this query return?SELECT details->>'color' AS color FROM products WHERE details @> '{"size": "M"}';PostgreSQL
SELECT details->>'color' AS color FROM products WHERE details @> '{"size": "M"}';
Attempts:
2 left
💡 Hint
The operator @> checks if the JSONB column contains the specified JSON.
✗ Incorrect
The query filters products whose details JSONB contains the key-value pair size='M'. It then selects the color value from details.
🧠 Conceptual
intermediate1:30remaining
Why use PostgreSQL's CTEs (WITH clauses)?
Which of the following best explains why Common Table Expressions (CTEs) are useful in PostgreSQL?
Attempts:
2 left
💡 Hint
Think about how CTEs help organize queries.
✗ Incorrect
CTEs let you define temporary named result sets that can be referenced within a query, making complex queries easier to read and maintain.
📝 Syntax
advanced2:30remaining
Which query correctly uses the UPSERT feature in PostgreSQL?
You want to insert a new user or update their email if the username already exists. Which query is correct?
PostgreSQL
Table: users(username TEXT PRIMARY KEY, email TEXT)
Attempts:
2 left
💡 Hint
PostgreSQL uses ON CONFLICT syntax for UPSERT.
✗ Incorrect
Option A uses the correct PostgreSQL syntax for UPSERT with ON CONFLICT and EXCLUDED keyword.
❓ optimization
advanced2:00remaining
How does PostgreSQL's partial index improve query performance?
Consider a table
orders with a status column. You create a partial index on status = 'pending'. What is the main benefit?Attempts:
2 left
💡 Hint
Think about how indexing fewer rows affects speed and size.
✗ Incorrect
Partial indexes include only rows matching the condition, making them smaller and faster for queries filtering on that condition.
🔧 Debug
expert3:00remaining
Why does this PostgreSQL query raise an error?
Given the query:
Does it raise an error about subquery returning more than one row? Why?
SELECT * FROM employees WHERE department = 'Sales' AND salary > (SELECT AVG(salary) FROM employees);
Does it raise an error about subquery returning more than one row? Why?
Attempts:
2 left
💡 Hint
Check if the subquery is correlated or not and what it returns.
✗ Incorrect
The subquery uses AVG(), an aggregate function that always returns a single scalar value regardless of the number of rows. It is not correlated and uses proper aggregation, so it does not return multiple rows.