0
0
PostgreSQLquery~20 mins

Creating and switching schemas in PostgreSQL - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Schema 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 schema creation query?
Consider the following SQL commands executed in PostgreSQL:

CREATE SCHEMA sales;
CREATE TABLE sales.orders (id SERIAL PRIMARY KEY, amount INT);
SELECT * FROM sales.orders;

What will be the result of the SELECT query?
PostgreSQL
CREATE SCHEMA sales;
CREATE TABLE sales.orders (id SERIAL PRIMARY KEY, amount INT);
SELECT * FROM sales.orders;
AAn empty result set with columns id and amount
BError: relation "sales.orders" does not exist
CError: schema "sales" does not exist
DA result set with one row containing id=1 and amount=NULL
Attempts:
2 left
💡 Hint
Think about what happens when you create a new table and select from it without inserting data.
query_result
intermediate
2:00remaining
What is the current schema after SET search_path?
Given these commands:

CREATE SCHEMA marketing;
SET search_path TO marketing, public;
SHOW search_path;

What will the SHOW search_path command output?
PostgreSQL
CREATE SCHEMA marketing;
SET search_path TO marketing, public;
SHOW search_path;
A"default"
B"public, marketing"
C"marketing, public"
DError: invalid schema name
Attempts:
2 left
💡 Hint
The SET search_path command sets the order of schemas PostgreSQL looks into.
📝 Syntax
advanced
2:00remaining
Which option correctly creates a schema and switches to it?
Which of the following SQL command sequences correctly creates a schema named 'archive' and sets it as the current schema for subsequent commands?
ACREATE SCHEMA archive; SET search_path TO archive;
BCREATE SCHEMA archive; USE archive;
CCREATE SCHEMA archive; SWITCH TO archive;
DCREATE SCHEMA archive; SET SCHEMA archive;
Attempts:
2 left
💡 Hint
PostgreSQL uses search_path to control schema lookup, not USE or SWITCH TO.
🔧 Debug
advanced
2:00remaining
Why does this query fail after creating a schema?
A user runs these commands:

CREATE SCHEMA reports;
CREATE TABLE reports.data (id INT);
SELECT * FROM data;

The SELECT query fails with "relation \"data\" does not exist". Why?
PostgreSQL
CREATE SCHEMA reports;
CREATE TABLE reports.data (id INT);
SELECT * FROM data;
ABecause the table 'data' was not created successfully
BBecause the search_path does not include the 'reports' schema, so 'data' is not found
CBecause SELECT * FROM data requires schema qualification even if search_path is set
DBecause CREATE SCHEMA must be followed by SET SCHEMA to access tables
Attempts:
2 left
💡 Hint
Think about how PostgreSQL finds tables without schema prefix.
🧠 Conceptual
expert
2:00remaining
What is the effect of setting search_path to "$user", public?
In PostgreSQL, what does setting search_path to "$user", public do?
AIt causes an error because "$user" is not a valid schema name
BIt searches only the public schema ignoring user schemas
CIt sets the schema to a fixed schema literally named "$user"
DIt first searches a schema named after the current user, then the public schema
Attempts:
2 left
💡 Hint
"$user" is a special placeholder in PostgreSQL search_path.