A schema is like a folder inside a database that holds tables and other objects. It helps organize data so you can keep things neat and separate.
Click to reveal answer
beginner
How do you create a new schema named 'sales' in PostgreSQL?
Use the command: CREATE SCHEMA sales; This makes a new schema called 'sales' inside your database.
Click to reveal answer
beginner
How can you switch to use a different schema in PostgreSQL?
You change the search path with: SET search_path TO schema_name; This tells PostgreSQL to look in that schema first when you run commands.
Click to reveal answer
intermediate
What does the command SET search_path TO public, sales; do?
It tells PostgreSQL to look first in the 'public' schema, then in the 'sales' schema when searching for tables or other objects.
Click to reveal answer
beginner
Why is using schemas helpful in a database?
Schemas help keep data organized, avoid name conflicts, and let different teams or projects work in the same database without mixing up their tables.
Click to reveal answer
Which command creates a new schema called 'inventory'?
AMAKE SCHEMA inventory;
BNEW SCHEMA inventory;
CCREATE SCHEMA inventory;
DADD SCHEMA inventory;
✗ Incorrect
The correct syntax to create a schema is CREATE SCHEMA schema_name;.
How do you tell PostgreSQL to look in the 'marketing' schema first when running queries?
ACHANGE schema TO marketing;
BUSE marketing;
CSET schema TO marketing;
DSET search_path TO marketing;
✗ Incorrect
You use SET search_path TO schema_name; to set the schema search order.
If you run SET search_path TO sales, public;, which schema is checked first?
Asales
BNone
CBoth at the same time
Dpublic
✗ Incorrect
The first schema listed in search_path is checked first.
What is the default schema in PostgreSQL if you don't change the search path?
Asales
Bpublic
Cdefault
Dmain
✗ Incorrect
By default, PostgreSQL uses the 'public' schema.
Why might you want to create multiple schemas in one database?
ATo organize tables by project or team
BTo make the database slower
CTo delete data faster
DTo avoid using tables
✗ Incorrect
Schemas help organize data and avoid name conflicts by grouping tables logically.
Explain how to create a new schema and switch to it in PostgreSQL.
Think about making a folder and then telling the database to look inside it first.
You got /3 concepts.
Describe why using schemas can be helpful when multiple teams share the same database.
Imagine different teams having their own folders to keep their files separate.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of creating schemas in PostgreSQL?
easy
A. To encrypt the database tables
B. To speed up query execution automatically
C. To create backups of the database
D. To group database objects and organize them logically
Solution
Step 1: Understand what schemas do
Schemas help organize tables, views, and other objects into groups.
Step 2: Identify the main purpose
This organization helps keep the database tidy and manageable.
Final Answer:
To group database objects and organize them logically -> Option D
Quick Check:
Schemas = Organize objects [OK]
Hint: Schemas group objects to keep your database tidy [OK]
Common Mistakes:
Thinking schemas speed up queries automatically
Confusing schemas with backups
Assuming schemas encrypt data
2. Which of the following is the correct syntax to create a new schema named sales?
easy
A. NEW SCHEMA sales;
B. CREATE SCHEMA sales;
C. MAKE SCHEMA sales;
D. CREATE DATABASE sales;
Solution
Step 1: Recall the command to create schemas
The correct SQL command is CREATE SCHEMA schema_name;.
Step 2: Match the syntax
CREATE SCHEMA sales; matches this syntax exactly for schema sales.
Final Answer:
CREATE SCHEMA sales; -> Option B
Quick Check:
CREATE SCHEMA = create schema [OK]
Hint: Use CREATE SCHEMA followed by the schema name [OK]
Common Mistakes:
Using MAKE or NEW instead of CREATE
Confusing CREATE SCHEMA with CREATE DATABASE
Missing the semicolon at the end
3. Given the following commands:
CREATE SCHEMA marketing;
SET search_path TO marketing;
CREATE TABLE campaigns (id SERIAL PRIMARY KEY, name TEXT);
What schema will the table campaigns belong to?
medium
A. marketing
B. None, it will cause an error
C. default
D. public
Solution
Step 1: Understand SET search_path effect
Setting search_path to marketing means new objects are created in that schema by default.
Step 2: Check where the table is created
The CREATE TABLE campaigns command creates the table inside the current schema in search_path, which is marketing.
Final Answer:
marketing -> Option A
Quick Check:
SET search_path = schema for new objects [OK]
Hint: SET search_path sets default schema for new tables [OK]
Common Mistakes:
Assuming tables go to public schema by default
Thinking SET search_path only affects queries, not creation
Believing this causes an error without schema prefix
4. You run the command SET search_path TO sales; but get an error: ERROR: schema "sales" does not exist. What is the most likely cause?
medium
A. The schema sales was not created before setting search_path
B. You need to use USE sales; instead
C. The database connection is closed
D. The table sales does not exist
Solution
Step 1: Understand error meaning
The error says the schema sales does not exist, so PostgreSQL cannot set search_path to it.
Step 2: Identify cause
This usually means the schema was never created with CREATE SCHEMA sales;.
Final Answer:
The schema sales was not created before setting search_path -> Option A
Quick Check:
SET search_path needs existing schema [OK]
Hint: Create schema before setting search_path to it [OK]
Common Mistakes:
Using USE instead of SET search_path in PostgreSQL
Confusing tables with schemas
Ignoring the error and continuing
5. You have two schemas: public and archive. You want to query a table named orders in archive without typing the full name every time. Which command will let you switch your session to use archive schema first, then public?
hard
A. USE archive, public;
B. ALTER SCHEMA archive SET search_path;
C. SET search_path TO archive, public;
D. SET schema TO archive, public;
Solution
Step 1: Recall how to set search_path for multiple schemas
PostgreSQL uses SET search_path TO schema1, schema2; to set schema priority.
Step 2: Identify correct syntax
SET search_path TO archive, public; correctly sets archive first, then public.
Final Answer:
SET search_path TO archive, public; -> Option C
Quick Check:
SET search_path sets schema order [OK]
Hint: Use SET search_path with comma-separated schemas [OK]
Common Mistakes:
Using USE instead of SET search_path
Trying ALTER SCHEMA to set search_path
Using SET schema instead of SET search_path
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.