SQL Level 4 Test

Updated · 7 min read
hardPostgreSQL 18sqltestquizassessmentlevel-4

This test checks Level 4 of the course, Professional: the professional lessons: security, running databases, SQL in applications, dialects and the projects. It has two parts. The 15 questions test that you know what each feature does; the 5 hands-on tasks test that you can use it, the way real work does: you run your own SQL in Try it and compare it with the solution. If you pass, you're ready for real projects; if you don't, every question links to the lesson that explains it.

How the Test Works

  • Part 1: 15 multiple-choice questions, marked for you.
  • Part 2: 5 tasks of these kinds: Fix, Optimize and Build. Write your query, run it, then open the solution and compare the rows. Any query that returns the same rows passes; the order matters only when the task asks for one.
  • Pass mark: 15 out of 20 (questions plus tasks). Give yourself one point per task whose result matches.
  • Time: about 45 minutes. No notes for Part 1; Part 2 is open-book, as real work is.
  • Covers: the lessons from SQL Injection to Capstone: Fee Tracker Payment Reconciliation.

Part 1: 15 Questions

Level 4 test: 15 questions

15 questions
1Which stops SQL injection?
2An app should connect as:
3Row-level security lets you:
4A backup you have never restored is:
5To rename a column without breaking running app versions, use:
6Partition pruning happens when the query:
7A user doesn't see the payment they just made. The page reads from:
8An UPDATE hangs. The first thing to check is:
9In JDBC, rs.getInt("mentor_id") for a NULL value returns:
10node-postgres returns a NUMERIC(8,2) value as:
11psycopg returns a NUMERIC(8,2) value as:
12The N+1 problem is:
13Tests that share a database stay independent when each test:
14In Oracle, WHERE middle_name = '' matches:
15A star schema has:

Part 2: 5 Hands-On Tasks

Task 1 (Fix): An injectable query. This PL/pgSQL function builds SQL from text and can be injected. Rewrite it safely with a parameter: EXECUTE 'SELECT count(*) FROM students WHERE last_name = ''' || name || ''''.

Show solution
SQL▶ Try it
CREATE FUNCTION count_by_last_name(name TEXT) RETURNS bigint
LANGUAGE plpgsql AS $$
DECLARE n bigint;
BEGIN
  EXECUTE 'SELECT count(*) FROM students WHERE last_name = $1'
    INTO n USING name;
  RETURN n;
END $$;
 
SELECT count_by_last_name('Anamaneni') AS sisters,
       count_by_last_name('x'' OR ''1''=''1') AS attack;
Output
CREATE FUNCTION
 
 sisters | attack
---------+--------
       3 |      0
(1 row)

Task 2 (Build): A read-only reporting role. Create a role reporter that can read payments and courses but nothing else, and prove it can't read students.

Show solution
SQL▶ Try it
CREATE ROLE reporter;
GRANT SELECT ON payments, courses TO reporter;
 
SET ROLE reporter;
SELECT count(*) AS payments_visible FROM payments;
SELECT count(*) FROM students;
Output
CREATE ROLE
 
GRANT
 
SET
 
 payments_visible
------------------
                7
(1 row)
 
ERROR:  permission denied for table students

Task 3 (Build): A safe migration. Add a currency column to payments that is required and starts as USD for every row, in one transaction, and check the table wasn't rewritten.

Show solution
SQL▶ Try it
CREATE TEMP TABLE before AS SELECT pg_relation_filenode('payments') AS n;
 
BEGIN;
ALTER TABLE payments ADD COLUMN currency CHAR(3) NOT NULL DEFAULT 'USD';
COMMIT;
 
SELECT n = pg_relation_filenode('payments') AS same_file,
       (SELECT count(*) FROM payments WHERE currency = 'USD') AS usd_rows
FROM before;
Output
SELECT 1
 
BEGIN
 
ALTER TABLE
 
COMMIT
 
 same_file | usd_rows
-----------+----------
 t         |        7
(1 row)

Task 4 (Optimize): Bloat after updates. Show that two full-table updates grow a 20,000-row table, and that VACUUM FULL shrinks it.

Show solution
SQL▶ Try it
CREATE TABLE t AS SELECT g AS id, g % 400 AS v FROM generate_series(1, 20000) g;
SELECT pg_size_pretty(pg_relation_size('t')) AS at_start;
UPDATE t SET v = v + 1;
UPDATE t SET v = v + 1;
SELECT pg_size_pretty(pg_relation_size('t')) AS after_updates;
VACUUM FULL t;
SELECT pg_size_pretty(pg_relation_size('t')) AS after_vacuum_full;
Output
SELECT 20000
 
 at_start
----------
 1024 kB
(1 row)
 
UPDATE 20000
 
UPDATE 20000
 
 after_updates
---------------
 2432 kB
(1 row)
 
VACUUM
 
 after_vacuum_full
-------------------
 712 kB
(1 row)

Task 5 (Build): Reconcile one month. Using the reconciliation dataset, match payments to statement lines with the same amount within 3 days, one to one, and report how many matched.

Show solution
SQL▶ Try it
INSERT INTO matches
SELECT DISTINCT ON (p.payment_id) p.payment_id, l.line_id,
       CASE WHEN l.posted_on = p.paid_on THEN 'exact' ELSE 'near' END,
       DATE '2026-01-08'
FROM payments p
JOIN statement_lines l
  ON l.amount_usd = p.amount_usd AND abs(l.posted_on - p.paid_on) <= 3
ORDER BY p.payment_id, abs(l.posted_on - p.paid_on), l.line_id
ON CONFLICT DO NOTHING;
 
SELECT kind, count(*) AS matches FROM matches GROUP BY kind ORDER BY kind;
Output
INSERT 0 7
 
 kind  | matches
-------+---------
 exact |       4
 near  |       3
(2 rows)

What Your Score Means

ScoreWhat to do
18 to 20Excellent. Try the SQL Problem Lab and the SQL Mock Interview.
15 to 17A pass. Reread the lessons linked from the questions you missed.
Below 15Not yet. Revise the linked lessons and take the test again in a day or two.

Was this tutorial helpful?