Challenge - 5 Problems
PostgreSQL SELECT 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 PostgreSQL's DISTINCT ON?
Consider the table employees with columns
department and salary. What rows does this query return?SELECT DISTINCT ON (department) department, salary FROM employees ORDER BY department, salary DESC;
PostgreSQL
CREATE TABLE employees (department TEXT, salary INT); INSERT INTO employees VALUES ('Sales', 5000), ('Sales', 6000), ('HR', 4500), ('HR', 4700), ('IT', 7000); SELECT DISTINCT ON (department) department, salary FROM employees ORDER BY department, salary DESC;
Attempts:
2 left
💡 Hint
DISTINCT ON returns the first row of each group based on the ORDER BY clause.
✗ Incorrect
DISTINCT ON (department) picks the first row per department ordered by salary descending, so highest salary per department is returned.
❓ query_result
intermediate2:00remaining
What does this query using PostgreSQL's RETURNING clause output?
Given the table products with columns
id and price, what is the output of this query?UPDATE products SET price = price * 1.1 WHERE id = 2 RETURNING id, price;
PostgreSQL
CREATE TABLE products (id INT PRIMARY KEY, price NUMERIC); INSERT INTO products VALUES (1, 100), (2, 200); UPDATE products SET price = price * 1.1 WHERE id = 2 RETURNING id, price;
Attempts:
2 left
💡 Hint
RETURNING shows the rows affected by the UPDATE.
✗ Incorrect
Only the row with id=2 is updated, price multiplied by 1.1, so price becomes 220.
📝 Syntax
advanced2:00remaining
Which option correctly uses PostgreSQL's JSONB operators to extract a value?
Given a table data with a JSONB column
info, which query correctly extracts the text value of the key name?PostgreSQL
SELECT info->>'name' FROM data;
Attempts:
2 left
💡 Hint
Use ->> with quotes around the key to get text.
✗ Incorrect
The operator ->> extracts the JSON value as text. The key must be a string literal in quotes.
❓ optimization
advanced2:00remaining
Which index type is best for optimizing full-text search in PostgreSQL?
You want to speed up searches on a text column using PostgreSQL's full-text search. Which index type should you create?
Attempts:
2 left
💡 Hint
Full-text search uses tsvector and GIN indexes.
✗ Incorrect
GIN indexes on to_tsvector expressions are optimized for full-text search queries.
🧠 Conceptual
expert2:00remaining
What is the effect of the PostgreSQL-specific clause FOR UPDATE SKIP LOCKED in a SELECT query?
Consider a SELECT query with
FOR UPDATE SKIP LOCKED. What does this clause do?Attempts:
2 left
💡 Hint
SKIP LOCKED avoids waiting on locked rows.
✗ Incorrect
FOR UPDATE locks rows selected, but SKIP LOCKED skips rows locked by others, allowing non-blocking row selection.