Comparison Functions and Operators
3 cards- 01
SELECT NULL = NULL AS same;What does the query return?
- A
false - B
NULL - C
true - D
An error:
NULLcannot be compared with=
- A
- 02
CREATE TABLE users (id int, status text); INSERT INTO users VALUES (1, 'active'), (2, 'banned'), (3, NULL); SELECT id FROM users WHERE status <> 'banned';Which ids are returned?
- A
1 only
- B
1 and 3
- C
1, 2 and 3
- D
3 only
- A
- 03
SELECT v FROM t WHERE v BETWEEN 1 AND 10;Which values does the condition accept?
- A
2 through 10
- B
2 through 9
- C
1 through 10, both ends included
- D
1 through 9
- A
Subquery Expressions
2 cards- 04
CREATE TABLE orders (id int, customer_id int); INSERT INTO orders VALUES (1, 10), (2, NULL); SELECT id FROM customers WHERE id NOT IN (SELECT customer_id FROM orders);customershas ids 10, 20 and 30. How many rows are returned?- A
3
- B
2
- C
1
- D
0
- A
- 05
SELECT 1 IN (1, NULL) AS a, 2 IN (1, NULL) AS b;What does the query return?
- A
a = true,b = NULL - B
a = NULL,b = false - C
a = true,b = false - D
a = NULL,b = NULL
- A
Aggregate Functions
4 cards- 06
CREATE TABLE t (v int); INSERT INTO t VALUES (1), (NULL), (1), (NULL); SELECT count(*), count(v), count(DISTINCT v) FROM t;What does the query return?
- A
2, 2, 1 - B
4, 4, 2 - C
4, 2, 2 - D
4, 2, 1
- A
- 07
CREATE TABLE scores (v int); INSERT INTO scores VALUES (10), (NULL), (20); SELECT avg(v) FROM scores;What does the query return?
- A
10 - B
NULL - C
An error: cannot average a column that contains
NULL - D
15
- A
- 08
SELECT sum(amount), count(*) FROM payments WHERE 1 = 0;What does the query return?
- A
One row:
NULL, NULL - B
One row:
0, 0 - C
No rows
- D
One row:
NULL, 0
- A
- 09
SELECT count(CASE WHEN status = 'paid' THEN 1 END) AS paid, sum(CASE WHEN status = 'paid' THEN 1 ELSE 0 END) AS paid_too FROM invoices;Which statement about the two columns is correct?
- A
They are equal:
countignores theNULLtheCASEyields for other rows - B
paid_tooisNULLover an empty table butpaidis 0 — so onlypaid_toois wrong - C
paidisNULLwhenever some invoice is not paid - D
paidcounts every row, becausecountcounts rows, not values
- A
Logical Operators
1 card- 10
SELECT (NULL AND false) AS a, (NULL OR true) AS b, (NOT NULL) AS c;What does the query return?
- A
NULL, true, NULL - B
NULL, NULL, NULL - C
false, true, true - D
false, true, NULL
- A
Sorting Rows (ORDER BY)
2 cards- 11
CREATE TABLE t (v int); INSERT INTO t VALUES (2), (NULL), (1); SELECT v FROM t ORDER BY v DESC;In what order are the values returned in PostgreSQL?
- A
NULL, 1, 2 - B
NULL, 2, 1 - C
2, 1, NULL - D
An error:
NULLcannot be sorted
- A
- 12
CREATE TABLE t (v text); INSERT INTO t VALUES ('10'), ('9'), ('100'); SELECT v FROM t ORDER BY v;In what order are the values returned?
- A
9, 10, 100 - B
10, 100, 9 - C
9, 100, 10 - D
100, 10, 9
- A
Conditional Expressions
1 card- 13
SELECT total / nullif(quantity, 0) AS unit_price FROM lines;What does
nullifdo here?- A
Turns a zero quantity into
NULL, so the division yieldsNULLinstead of raising an error - B
Raises an error when quantity is 0, with a clearer message
- C
Turns a
NULLquantity into 0 - D
Skips rows whose quantity is 0
- A
Table Expressions
8 cards- 14
CREATE TABLE t (region text); INSERT INTO t VALUES ('eu'), (NULL), ('eu'), (NULL); SELECT region, count(*) FROM t GROUP BY region;How many rows are returned?
- A
3:
euand two separateNULLgroups - B
2: one group for
euand one group forNULL - C
4:
NULLnever equalsNULL, so nothing groups - D
1: rows with a
NULLregion are dropped
- A
- 15
SELECT department, name, count(*) FROM employees GROUP BY department;What happens in PostgreSQL?
- A
An error:
namemust appear in theGROUP BYclause or be used in an aggregate function - B
One row per department, with the first
nameby primary key - C
One row per employee
- D
One row per department, with an arbitrary
namefrom that department
- A
- 16
SELECT customer_id, count(*) FROM orders WHERE count(*) > 1 GROUP BY customer_id;What happens?
- A
It returns no rows
- B
It returns every customer, because
WHEREruns before the grouping - C
It returns the customers with more than one order
- D
An error: aggregate functions are not allowed in
WHERE
- A
- 17
SELECT c.name, o.id FROM customers c LEFT JOIN orders o ON o.customer_id = c.id WHERE o.status = 'shipped';What does the
WHEREclause do to theLEFT JOIN?- A
It turns it into an inner join: customers without orders are dropped, because
NULL = 'shipped'is not true - B
It raises an error, because
o.statusmay beNULL - C
Nothing:
LEFT JOINguarantees every customer appears - D
It keeps every customer and blanks out the unshipped orders
- A
- 18
SELECT c.name, count(*) FROM customers c LEFT JOIN orders o ON o.customer_id = c.id GROUP BY c.name;What is reported for a customer with no orders?
- A
0
- B
1:
count(*)counts the padded row - C
The customer is not listed
- D
NULL
- A
- 19
SELECT * FROM a, b;ahas 3 rows andbhas 4. How many rows are returned?- A
12
- B
7
- C
3
- D
4
- A
- 20
SELECT * FROM t1 JOIN t2 ON t1.k = t2.k;t1has 2 rows withk = 1;t2has 3 rows withk = 1. How many rows does the join produce?- A
3
- B
6
- C
2
- D
5
- A
- 21
SELECT count(*) FROM t1 JOIN t2 ON t1.k = t2.k;Both tables have exactly one row, and in both of them
kisNULL. What is returned?- A
1
- B
NULL - C
0
- D
An error: cannot join on a
NULLkey
- A
Date/Time Functions and Operators
2 cards- 22
CREATE TABLE events (at timestamp); INSERT INTO events VALUES ('2024-01-31 00:00:00'), ('2024-01-31 15:30:00'), ('2024-02-01 00:00:00'); SELECT count(*) FROM events WHERE at BETWEEN '2024-01-01' AND '2024-01-31';How many rows are counted?
- A
2
- B
3
- C
0
- D
1
- A
- 23
SELECT date '2024-01-31' + interval '1 month';What does the query return?
- A
2024-02-29 00:00:00 - B
2024-03-02 00:00:00 - C
2024-03-01 00:00:00 - D
An error: February has no 31st
- A
Numeric Types
1 card- 24
SELECT 7 / 2 AS a, 7 / 2.0 AS b, 7 % 2 AS c;What does the query return?
- A
3, 3.5, 1 - B
3, 3, 1 - C
3.5, 3.5, 1 - D
4, 3.5, 1
- A
String Functions and Operators
1 card- 25
SELECT 'Ada' || ' ' || NULL AS a, concat('Ada', ' ', NULL) AS b;What does the query return?
- A
a = NULL,b = 'Ada ' - B
a = 'Ada ',b = 'Ada ' - C
a = 'Ada NULL',b = 'Ada ' - D
a = NULL,b = NULL
- A
Pattern Matching
1 card- 26
SELECT 'abc' LIKE 'a_c' AS a, 'a_c' LIKE 'a\_c' AS b, 'ABC' LIKE 'abc' AS c;What does the query return?
- A
false, true, false - B
true, false, false - C
true, true, true - D
true, true, false
- A
Character Types
2 cards- 27
CREATE TABLE t (code varchar(3)); INSERT INTO t VALUES ('abcdef');What happens on the
INSERT?- A
An error: value too long for type character varying(3)
- B
A warning is raised and
'abc'is stored - C
The value is silently truncated to
'abc' - D
The value is stored in full: the length is only a hint
- A
- 28
SELECT length('ab'::char(5)) AS len, 'ab'::char(5) || '|' AS joined;What does the query return?
- A
2, 'ab |' - B
5, 'ab |' - C
2, 'ab|' - D
5, 'ab|'
- A
Lexical Structure
1 card- 29
CREATE TABLE "Users" (id int); SELECT * FROM users;What happens on the
SELECT?- A
An error: identifiers must be quoted consistently
- B
It returns the empty table
- C
It works, but only if the table was created in the same session
- D
An error: relation "users" does not exist
- A
SELECT
6 cards- 30
SELECT price * quantity AS total FROM lines WHERE total > 100 ORDER BY total;What happens in PostgreSQL?
- A
It works: aliases can be used anywhere after the
SELECTlist - B
An error: column "total" does not exist (in
WHERE); theORDER BYalone would have worked - C
It works, but the filter is applied after sorting
- D
An error in
ORDER BY: aliases can only be used inWHERE
- A
- 31
In what order are the clauses of a
SELECTlogically evaluated?- A
FROM→WHERE→SELECT→GROUP BY→HAVING→ORDER BY→LIMIT - B
SELECT→FROM→WHERE→GROUP BY→HAVING→ORDER BY→LIMIT - C
FROM→WHERE→GROUP BY→HAVING→SELECT→DISTINCT→ORDER BY→LIMIT - D
FROM→SELECT→WHERE→GROUP BY→HAVING→ORDER BY→LIMIT
- A
- 32
SELECT DISTINCT city, country FROM addresses;What does
DISTINCTapply to?- A
Only
city, the column it is written next to - B
Only
country, the last column - C
Each column independently
- D
The whole row:
(city, country)pairs are de-duplicated
- A
- 33
SELECT DISTINCT ON (customer_id) customer_id, placed_at, total FROM orders ORDER BY customer_id, placed_at DESC;What does the query return?
- A
Each customer's earliest order
- B
An error:
DISTINCT ONrequires an aggregate for the other columns - C
One row per customer, with the
placed_atandtotalchosen arbitrarily - D
Each customer's most recent order
- A
- 34
SELECT DISTINCT name FROM users ORDER BY created_at;What happens in PostgreSQL?
- A
It works only if
created_atis unique - B
It works, but the sort is ignored
- C
It works: users are de-duplicated by name and sorted by creation time
- D
An error: for
SELECT DISTINCT,ORDER BYexpressions must appear in the select list
- A
- 35
SELECT count(*) FROM t; SELECT region, count(*) FROM t GROUP BY region;tis empty. How many rows does each query return?- A
0 and 0
- B
1 and 0
- C
0 and 1
- D
1 and 1
- A
LIMIT and OFFSET
1 card- 36
SELECT id FROM users LIMIT 10;Which rows does this return?
- A
The ten rows that were inserted first
- B
Ten rows, but which ten is not defined and may change between runs
- C
The ten rows that were most recently updated
- D
The ten rows with the smallest ids
- A
Combining Queries (UNION, INTERSECT, EXCEPT)
1 card- 37
SELECT 1 UNION SELECT 1; SELECT 1 UNION ALL SELECT 1;How many rows does each query return?
- A
1 and 1
- B
2 and 1
- C
1 and 2
- D
2 and 2
- A
Value Expressions
1 card- 38
SELECT total / quantity FROM lines WHERE quantity <> 0 AND total / quantity > 10;Is the division protected from
quantity = 0?- A
No: the order in which
ANDoperands are evaluated is not guaranteed, so the division may run first - B
No, but only because the
SELECTlist is evaluated beforeWHERE - C
Yes, but only if
quantityis indexed - D
Yes:
ANDevaluates left to right and stops at the first false operand
- A
Window Functions
3 cards- 39
CREATE TABLE scores (name text, score int); INSERT INTO scores VALUES ('a', 90), ('b', 90), ('c', 80); SELECT name, rank() OVER (ORDER BY score DESC) AS r, dense_rank() OVER (ORDER BY score DESC) AS d, row_number() OVER (ORDER BY score DESC) AS n FROM scores;What is returned for
c?- A
r = 2,d = 2,n = 3 - B
r = 3,d = 2,n = 3 - C
r = 3,d = 3,n = 3 - D
r = 2,d = 3,n = 2
- A
- 40
CREATE TABLE sales (day int, amount int); INSERT INTO sales VALUES (1, 10), (2, 20), (2, 30), (3, 40); SELECT day, amount, sum(amount) OVER (ORDER BY day) AS running FROM sales;What is
runningfor the two rows on day 2?- A
30 for both
- B
60 for both
- C
30 and 60
- D
20 and 30
- A
- 41
SELECT name, score FROM scores WHERE rank() OVER (ORDER BY score DESC) <= 3;What happens?
- A
An error: window functions are not allowed in
WHERE - B
It returns the top three scores, but ties are excluded
- C
It returns every row, because the window has no
PARTITION BY - D
It returns the top three scores
- A
UPDATE
1 card- 42
CREATE TABLE p (a int, b int); INSERT INTO p VALUES (1, 2); UPDATE p SET a = b, b = a; SELECT * FROM p;What does the
SELECTreturn?- A
An error:
ais assigned twice - B
2, 2 - C
2, 1 - D
1, 2
- A
Constraints
2 cards- 43
CREATE TABLE users (id int, email text UNIQUE); INSERT INTO users VALUES (1, NULL); INSERT INTO users VALUES (2, NULL);What happens on the second
INSERT?- A
It succeeds:
NULLs are distinct from each other for aUNIQUEconstraint - B
An error: duplicate key value violates unique constraint
- C
It succeeds only because the column is not
NOT NULL— and a warning is raised - D
It succeeds, but the second
NULLis stored as an empty string
- A
- 44
CREATE TABLE items (qty int CHECK (qty > 0)); INSERT INTO items VALUES (NULL);What happens on the
INSERT?- A
An error:
CHECKconstraints requireNOT NULL - B
It succeeds, and
qtyis stored as 0 - C
An error: new row violates check constraint
- D
It succeeds: a
CHECKconstraint only rejects rows for which the condition is false, andNULL > 0is unknown
- A
Sequence Manipulation Functions
1 card- 45
CREATE TABLE t (id serial PRIMARY KEY, v text); BEGIN; INSERT INTO t (v) VALUES ('a'); ROLLBACK; INSERT INTO t (v) VALUES ('b'); SELECT id FROM t;What id does row
bget?- A
1: sequences are only advanced on commit
- B
2: the rolled-back insert consumed sequence value 1
- C
1: the rollback returned the value to the sequence
- D
An error: the sequence is left in an inconsistent state
- A
End of deck · 45 cards