0
0
PostgreSQLquery~10 mins

Dynamic SQL with EXECUTE in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to execute a dynamic SQL query stored in the variable.

PostgreSQL
EXECUTE [1];
Drag options to blanks, or click blank then click option'
Aquery_string
B'query_string'
CEXEC query_string
DEXECUTE query_string()
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the variable name, which makes it a string literal.
Trying to call EXECUTE as a function.
2fill in blank
medium

Complete the code to build a dynamic SQL query that selects all columns from a table named in the variable.

PostgreSQL
query_string := 'SELECT * FROM ' || [1] || ';';
Drag options to blanks, or click blank then click option'
A'TABLE_NAME'
B'table_name'
Ctable_name
DTABLE_NAME
Attempts:
3 left
💡 Hint
Common Mistakes
Adding quotes around the variable name, which breaks the query.
Using uppercase variable names that are not defined.
3fill in blank
hard

Fix the error in the dynamic SQL execution by completing the code.

PostgreSQL
EXECUTE [1] USING user_id;
Drag options to blanks, or click blank then click option'
A'SELECT * FROM users WHERE id = $1'
Bquery_string
C'query_string'
DEXEC query_string
Attempts:
3 left
💡 Hint
Common Mistakes
Putting quotes around the query variable.
Trying to use EXEC instead of EXECUTE.
4fill in blank
hard

Fill both blanks to create a dynamic SQL query that selects a column and table dynamically.

PostgreSQL
query_string := 'SELECT ' || [1] || ' FROM ' || [2] || ';';
Drag options to blanks, or click blank then click option'
Acolumn_name
B'column_name'
Ctable_name
D'table_name'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding quotes around variable names.
Mixing up column and table variable names.
5fill in blank
hard

Fill all three blanks to build and execute a dynamic SQL query with a parameter.

PostgreSQL
query_string := 'SELECT * FROM ' || [1] || ' WHERE ' || [2] || ' = $1'; EXECUTE [3] USING param_value;
Drag options to blanks, or click blank then click option'
Atable_name
Bcolumn_name
Cquery_string
D'table_name'
Attempts:
3 left
💡 Hint
Common Mistakes
Quoting variable names in the query string.
Passing a quoted string instead of the query variable to EXECUTE.