QA Interview Hub · Knowledge base · Quizzes · Українська
129 questions on SQL, from the basics to the hard ones. Answers and explanations are in the quiz and the knowledge base.
By level: Junior QA interview questions · Middle QA interview questions · Senior QA interview questions
SELECT (NULL AND FALSE) AS a, (NULL OR TRUE) AS b;SELECT 0.1::double precision + 0.2::double precision = 0.3::double precision;CREATE TABLE enrollments (
student_id int,
course_id int,
PRIMARY KEY (student_id, course_id)
);
INSERT INTO enrollments VALUES (1, 10);
INSERT INTO enrollments VALUES (1, 10);SELECT COUNT(*)
FROM (VALUES (1), (NULL), (3)) AS t(x)
WHERE x <> 1;SELECT 7 / 2 AS a,
7 / 2.0 AS b,
7 % 2 AS c;CREATE TABLE users (id int PRIMARY KEY);
CREATE TABLE accounts (
id int PRIMARY KEY,
user_id int REFERENCES users (id)
ON DELETE CASCADE
);
INSERT INTO users VALUES (1);
INSERT INTO accounts VALUES (10, 1), (11, 1);
DELETE FROM users WHERE id = 1;
SELECT COUNT(*) FROM accounts;Take the quiz for this chapter · Read the explanation in the knowledge base
SELECT * FROM users WHERE deleted_at = NULL;COALESCE(phone, email, 'n/a') return? JuniorAVG(rating) for the values 5, NULL, 3? MiddleSELECT count(*) FROM orders
WHERE created_at BETWEEN '2026-09-01' AND '2026-09-30';SELECT name
FROM (VALUES ('a_b'), ('axb'), ('ab')) AS t(name)
WHERE name LIKE 'a_b';SELECT x
FROM (VALUES (1), (5), (10), (NULL)) AS t(x)
WHERE x BETWEEN 5 AND 10;SELECT x
FROM (VALUES (2), (NULL), (1)) AS t(x)
ORDER BY x DESC;SELECT DISTINCT x
FROM (VALUES (1), (NULL), (NULL), (1)) AS t(x);SELECT x
FROM (VALUES (1), (2), (NULL)) AS t(x)
WHERE x IN (1, NULL);Take the quiz for this chapter · Read the explanation in the knowledge base
SELECT country, COUNT(*) FROM users GROUP BY country;SELECT u.id, COUNT(*) AS cnt
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
GROUP BY u.id;SELECT SUM(qty) / COUNT(qty) AS manual_avg, AVG(qty) AS avg FROM items;SELECT COUNT(*) AS c, SUM(x) AS s
FROM (VALUES (1), (2)) AS t(x)
WHERE x > 5;SELECT user_id
FROM (VALUES (1, 10), (1, 20), (2, 50),
(3, 5), (3, 5), (3, 5)) AS o(user_id, amount)
GROUP BY user_id
HAVING SUM(amount) > 25
ORDER BY user_id;SELECT COUNT(*) FILTER (WHERE s = 'ok') AS ok,
COUNT(*) AS total
FROM (VALUES ('ok'), ('ok'),
('failed'), (NULL)) AS t(s);SELECT COUNT(*), COUNT(x), COUNT(DISTINCT x)
FROM (VALUES (1), (1), (NULL), (2)) AS t(x);SELECT user_id, status, COUNT(*)
FROM (VALUES (1, 'new'), (1, 'paid'))
AS o(user_id, status)
GROUP BY user_id;SELECT SUM(CASE WHEN s = 'failed' THEN 1 END) AS failed
FROM (VALUES ('ok'), ('ok')) AS t(s);Take the quiz for this chapter · Read the explanation in the knowledge base
SELECT u.id, u.email
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE ???;SELECT u.email, o.id
FROM orders o
RIGHT JOIN users u ON u.id = o.user_id;SELECT u.email, o.id
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE o.status = 'PAID';SELECT COUNT(*)
FROM (VALUES (1), (2)) AS a(id)
CROSS JOIN (VALUES ('x'), ('y'), ('z')) AS b(c);SELECT a.id, b.id
FROM (VALUES (1), (2)) AS a(id)
FULL JOIN (VALUES (2), (3)) AS b(id)
ON a.id = b.id;SELECT COUNT(*)
FROM (VALUES (1), (NULL)) AS a(k)
JOIN (VALUES (1), (NULL)) AS b(k)
ON a.k = b.k;SELECT id, email, amount
FROM users u
JOIN orders o ON o.user_id = u.id;Take the quiz for this chapter · Read the explanation in the knowledge base
SELECT * FROM users
WHERE id NOT IN (SELECT user_id FROM blocked_users);WITH paid AS (
SELECT user_id, SUM(amount) AS total
FROM orders WHERE status = 'PAID'
GROUP BY user_id
)
SELECT u.email, p.total FROM users u JOIN paid p ON p.user_id = u.id;SELECT x FROM a
EXCEPT
SELECT x FROM b;WITH RECURSIVE t(n) AS (
SELECT 1
UNION ALL
SELECT n + 1 FROM t WHERE n < 3
)
SELECT n FROM t;WITH recent AS (
SELECT * FROM orders WHERE status = 'paid'
)
SELECT * FROM recent WHERE id = 5;SELECT id
FROM users
WHERE id = (SELECT user_id FROM orders);SELECT id
FROM orders o
WHERE amount > (
SELECT AVG(amount)
FROM orders
WHERE user_id = o.user_id
);SELECT x FROM (VALUES (1), (NULL)) AS a(x)
INTERSECT
SELECT x FROM (VALUES (NULL), (2)) AS b(x);SELECT amount * 2 AS doubled
FROM orders
WHERE doubled > 10;Take the quiz for this chapter · Read the explanation in the knowledge base
UPDATE accounts SET balance = 0;? JuniorUPDATE accounts SET balance = balance - 100 WHERE id = 1;
ROLLBACK;INSERT INTO users (email, name)
VALUES ('a@x.io', 'Anna')
ON CONFLICT (email) DO NOTHING;-- accounts: (id = 1, balance = 100)
-- A: BEGIN;
-- A: UPDATE accounts SET balance = 50
-- WHERE id = 1;
-- B: SELECT balance FROM accounts
-- WHERE id = 1;-- accounts: (id = 1, balance = 100)
-- A: BEGIN ISOLATION LEVEL REPEATABLE READ;
-- A: SELECT balance FROM accounts
-- WHERE id = 1; -- 100
-- B: UPDATE accounts SET balance = 70
-- WHERE id = 1;
-- B: COMMIT;
-- A: UPDATE accounts
-- SET balance = balance - 10
-- WHERE id = 1;-- users already has a@x.io (UNIQUE email)
BEGIN;
INSERT INTO users (email)
VALUES ('a@x.io');
-- ERROR: duplicate key value ...
SELECT 1;
COMMIT;INSERT INTO users (email)
VALUES ('qa@test.io')
RETURNING id;Take the quiz for this chapter · Read the explanation in the knowledge base
timestamptz rather than timestamp for operation times in fintech? MiddleCREATE TABLE products (
id int PRIMARY KEY,
price numeric CHECK (price > 0)
);
INSERT INTO products VALUES (1, NULL);CREATE TABLE orders (
id int PRIMARY KEY,
user_id int REFERENCES users (id)
);
-- users: id 1; orders: (10, 1), (11, 1)
DELETE FROM users WHERE id = 1;-- orders.user_id bigint, orders.created_at timestamptz
CREATE INDEX idx_orders_user_created
ON orders (user_id, created_at);-- users: 3 million rows
CREATE INDEX idx_users_email
ON users (email);
EXPLAIN SELECT * FROM users
WHERE lower(email) = 'qa@test.io';-- users: 1000 rows
ALTER TABLE users
ADD COLUMN phone text NOT NULL;-- orders: 5 million rows, autocommit
EXPLAIN ANALYZE
DELETE FROM orders
WHERE status = 'old';Take the quiz for this chapter · Read the explanation in the knowledge base
SELECT day, amount,
LAG(amount) OVER (ORDER BY day) AS prev
FROM sales;SELECT day, amount,
SUM(amount) OVER (ORDER BY day) AS running
FROM sales
ORDER BY day;SELECT id, x,
LAST_VALUE(x) OVER (ORDER BY id) AS last
FROM t
ORDER BY id;-- employees: (dept, salary)
-- (A,100), (A,100), (A,90), (A,80)
SELECT salary FROM (
SELECT salary,
DENSE_RANK() OVER (
PARTITION BY dept
ORDER BY salary DESC) AS r
FROM employees
) x
WHERE r <= 2;SELECT id,
ROW_NUMBER() OVER (ORDER BY id) AS rn
FROM users
WHERE rn = 1;-- sales: (day, amount)
-- (1, 10), (2, 15), (3, 12)
SELECT day,
amount - LAG(amount, 1, 0)
OVER (ORDER BY day) AS diff
FROM sales
ORDER BY day;-- m: (day, amount)
-- (1, 10), (2, 20), (3, 60)
SELECT day,
AVG(amount) OVER (
ORDER BY day
ROWS BETWEEN 1 PRECEDING
AND CURRENT ROW
) AS ma
FROM m
ORDER BY day;-- r: (acc, day, amount)
-- (A,1,10), (A,2,5), (B,1,7)
SELECT acc, day,
SUM(amount) OVER (
PARTITION BY acc
ORDER BY day) AS running
FROM r
ORDER BY acc, day;Take the quiz for this chapter · Read the explanation in the knowledge base
SELECT id FROM users LIMIT 3;EXPLAIN ANALYZE
SELECT * FROM orders WHERE user_id = 42;-- users: id 1, 2
-- orders (id, user_id):
-- (10, 1), (11, 3), (12, NULL)
SELECT o.id
FROM orders o
LEFT JOIN users u ON u.id = o.user_id
WHERE u.id IS NULL
ORDER BY o.id;-- users: id 1, 2, 3
-- orders.user_id: 1, NULL
SELECT id
FROM users
WHERE id NOT IN (
SELECT user_id FROM orders
);-- payments.created_at timestamptz
-- = '2026-03-01 23:30:00+00'
SET TimeZone = 'Europe/Kyiv';
SELECT created_at::date
FROM payments;-- accounts: (A,100), (B,50), (C,0)
-- transactions (account_id, amount):
-- (A,60), (A,40), (B,30)
SELECT a.id
FROM accounts a
LEFT JOIN transactions t
ON t.account_id = a.id
GROUP BY a.id, a.balance
HAVING a.balance
<> COALESCE(SUM(t.amount), 0);Take the quiz for this chapter · Read the explanation in the knowledge base
employees(id, department_id, salary)employees(id, department_id, salary)purchases(id, user_id, product)-- employees.salary: 300, 300, 300
SELECT MAX(salary)
FROM employees
WHERE salary < (
SELECT MAX(salary) FROM employees
);-- employees.salary:
-- 300, 300, 250, 250, 200
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 2;-- users (id, email):
-- (1,'a@x.io'), (2,'b@x.io'),
-- (3,'a@x.io'), (4,NULL), (5,NULL)
SELECT email, COUNT(*)
FROM users
GROUP BY email
HAVING COUNT(*) > 1;-- users (id, email):
-- (1,'a'), (2,'a'), (3,'b'), (4,'a')
DELETE FROM users x
USING users y
WHERE x.email = y.email
AND x.id > y.id;-- t.id: 1, 2, 4, 6
SELECT s.n
FROM generate_series(1, 6) AS s(n)
LEFT JOIN t ON t.id = s.n
WHERE t.id IS NULL
ORDER BY s.n;-- users: 1, 2, 3
-- orders (id, user_id, status):
-- (1,1,'paid'), (2,2,'pending'),
-- (3,1,'paid')
SELECT u.id, COUNT(o.id) AS paid
FROM users u
LEFT JOIN orders o ON o.user_id = u.id
WHERE o.status = 'paid'
GROUP BY u.id;-- transactions (type, status):
-- card: 3 ok, 1 failed
-- sepa: 1 ok, 1 failed
SELECT type,
COUNT(*) FILTER (
WHERE status = 'failed')
/ COUNT(*) * 100 AS pct
FROM transactions
GROUP BY type;-- employees (id, name, manager_id, salary):
-- (1,Anna,NULL,300) (2,Bohdan,1,350)
-- (3,Olha,1,250) (4,Ivan,2,360)
-- (5,Kira,2,340)
SELECT e.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;WITH salaries(salary) AS (
VALUES (100), (200), (300), (1000)
)
SELECT percentile_cont(0.5)
WITHIN GROUP (ORDER BY salary)
FROM salaries;Take the quiz for this chapter · Read the explanation in the knowledge base