Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

PL/SQL & T-SQL (Oracle and SQL Server) Module 13

Dynamic SQL and security in PL/SQL interview questions

Interview questions on dynamic SQL and security in PL/SQL: warm-ups, then what each harder question tests and how a good answer starts.

7 questions: 2 warm-ups and 5 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    Why is a bind variable safer than a value concatenated into dynamic SQL?

    Answered in the lesson Dynamic SQL in PL/SQL without injection

  2. Mid

    What changes when a procedure is created AUTHID CURRENT_USER?

    Answered in the lesson Definer's rights, invoker's rights and roles

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Mid to senior

    A report procedure takes p_sort_col from a web page and runs 'SELECT ... FROM accounts ORDER BY ' || p_sort_col. A teammate proposes wrapping p_sort_col in DBMS_ASSERT.SIMPLE_SQL_NAME. Is that enough, and how would you write the procedure?

    What it tests
    Whether the candidate knows a column name cannot be bound, puts an allowlist in code before any DBMS_ASSERT call, quotes the mapped name with the right case, and keeps the sort direction out of the text.
    First step
    It is not enough: SIMPLE_SQL_NAME checks the form of a name, not whether it is a column the report may sort by. Map the caller's key to a column name the code holds, in a CASE or a lookup, and refuse any other key before building any text.
    Follow-ups

    From the lesson Dynamic SQL in PL/SQL without injection

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Mid to staff

    A search procedure takes four optional filters, city, kind, minimum balance and maximum balance, and concatenates the ones that are not NULL into a WHERE clause. How would you rewrite it so that every value is bound, and which PL/SQL tool fits?

    What it tests
    Whether the candidate keeps values out of the text when the shape varies, knows a USING list is fixed at compile time, and can use DBMS_SQL with BIND_VARIABLE and TO_REFCURSOR or weigh a static alternative.
    First step
    Keep building the WHERE clause, but append only fixed text with one placeholder per filter, such as AND c.city = :city. The number of placeholders now varies with the call, so bind them by name with DBMS_SQL, or keep one fixed statement with a NULL test for each filter.
    Follow-ups

    From the lesson Dynamic SQL in PL/SQL without injection

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Mid to senior

    A utility procedure in schema TOOLS is created AUTHID CURRENT_USER and selects from a table named settings. It works when TOOLS runs it but fails with ORA-00942 for every other user who holds EXECUTE on it. Explain why, and give two fixes, saying when each is right.

    What it tests
    Whether the candidate knows how invoker's rights resolve names and check privileges, reads ORA-00942 as either a missing table or a missing privilege, and can choose between a qualified name with grants and definer's rights.
    First step
    Under AUTHID CURRENT_USER the unqualified name settings resolves in each caller's own schema, where no such table exists, so every caller except TOOLS gets ORA-00942. Decide first whether callers should share TOOLS's table or each use their own.
    Follow-ups

    From the lesson Definer's rights, invoker's rights and roles

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 2 minutes
  4. Mid to staff

    A definer's-rights package compiles in development, where its owner holds SELECT on a table directly. In test, the same privilege was granted to the owner through a role, and the package body does not compile. A colleague then rewrites the query as dynamic SQL, which compiles but fails at run time. Explain both failures and the right fix.

    What it tests
    Whether the candidate knows that roles do not count for static SQL at compile time or inside definer's-rights units, and can choose between a direct grant and a role granted to the package.
    First step
    Both failures have one cause: privileges that arrive through a role do not count for a definer's-rights unit. The static query is checked at compile time without roles, and the dynamic one at run time with only PUBLIC enabled.
    Follow-ups

    From the lesson Definer's rights, invoker's rights and roles

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Senior to staff

    Design the database accounts for a small banking application on the practice tables. The application server must read balances and make deposits, analysts must query every table, and developers must deploy code. Who owns what, and what does each account hold?

    What it tests
    Whether the candidate separates data, API and application schemas, grants EXECUTE instead of table privileges, uses direct grants for the API owner, a schema privilege for analysts and DB_DEVELOPER_ROLE for developers.
    First step
    Separate ownership from use: one schema owns the tables, one owns a definer's-rights package that is the only way to change them, and the application's account holds CREATE SESSION and EXECUTE on that package, nothing else.
    Follow-ups

    From the lesson Definer's rights, invoker's rights and roles

    Model answer, Pro.

    In the answer:

    • Model answer
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All PL/SQL & T-SQL (Oracle and SQL Server) interview questions · The PL/SQL & T-SQL (Oracle and SQL Server) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.