0
0
PostgreSQLquery~20 mins

Why PostgreSQL advanced features matter - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PostgreSQL Advanced Features Master
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 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"}';
ASyntax error due to JSON syntax
BAll colors of products where size is 'M'
CAll products with any size and color 'M'
DAll sizes of products where color is 'M'
Attempts:
2 left
💡 Hint
The operator @> checks if the JSONB column contains the specified JSON.
🧠 Conceptual
intermediate
1:30remaining
Why use PostgreSQL's CTEs (WITH clauses)?
Which of the following best explains why Common Table Expressions (CTEs) are useful in PostgreSQL?
AThey replace the need for transactions in multi-step operations.
BThey automatically create indexes on tables used in the query.
CThey allow breaking complex queries into simpler parts and can improve readability and maintainability.
DThey are used only for creating temporary tables.
Attempts:
2 left
💡 Hint
Think about how CTEs help organize queries.
📝 Syntax
advanced
2: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)
AINSERT INTO users(username, email) VALUES('alice', 'alice@example.com') ON CONFLICT (username) DO UPDATE SET email = EXCLUDED.email;
BINSERT INTO users(username, email) VALUES('alice', 'alice@example.com') ON DUPLICATE KEY UPDATE email = VALUES(email);
CINSERT INTO users(username, email) VALUES('alice', 'alice@example.com') ON CONFLICT DO NOTHING;
DUPDATE users SET email = 'alice@example.com' WHERE username = 'alice' ELSE INSERT INTO users(username, email) VALUES('alice', 'alice@example.com');
Attempts:
2 left
💡 Hint
PostgreSQL uses ON CONFLICT syntax for UPSERT.
optimization
advanced
2: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?
AThe index duplicates the entire table for faster access.
BThe index automatically updates all rows regardless of status.
CThe index prevents insertion of rows with status other than 'pending'.
DThe index is smaller and faster because it only includes rows where status is 'pending'.
Attempts:
2 left
💡 Hint
Think about how indexing fewer rows affects speed and size.
🔧 Debug
expert
3:00remaining
Why does this PostgreSQL query raise an error?
Given the query:
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?
AThe subquery returns a single value, so the error must be from elsewhere.
BThe subquery returns multiple rows because it lacks a GROUP BY clause and is correlated incorrectly.
CThe subquery returns multiple rows because AVG(salary) is not aggregated properly.
DThe subquery returns multiple rows because the table has duplicate salaries.
Attempts:
2 left
💡 Hint
Check if the subquery is correlated or not and what it returns.