Card type

Comparison Functions and Operators

3 cards
  1. 01
    SELECT NULL = NULL AS same;
    

    What does the query return?

    • A

      false

    • B

      NULL

    • C

      true

    • D

      An error: NULL cannot be compared with =

  2. 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

  3. 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

Subquery Expressions

2 cards
  1. 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);
    

    customers has ids 10, 20 and 30. How many rows are returned?

    • A

      3

    • B

      2

    • C

      1

    • D

      0

  2. 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

Aggregate Functions

4 cards
  1. 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

  2. 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

  3. 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

  4. 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: count ignores the NULL the CASE yields for other rows

    • B

      paid_too is NULL over an empty table but paid is 0 — so only paid_too is wrong

    • C

      paid is NULL whenever some invoice is not paid

    • D

      paid counts every row, because count counts rows, not values

Logical Operators

1 card
  1. 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

Sorting Rows (ORDER BY)

2 cards
  1. 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: NULL cannot be sorted

  2. 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

Conditional Expressions

1 card
  1. 13
    SELECT total / nullif(quantity, 0) AS unit_price FROM lines;
    

    What does nullif do here?

    • A

      Turns a zero quantity into NULL, so the division yields NULL instead of raising an error

    • B

      Raises an error when quantity is 0, with a clearer message

    • C

      Turns a NULL quantity into 0

    • D

      Skips rows whose quantity is 0

Table Expressions

8 cards
  1. 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: eu and two separate NULL groups

    • B

      2: one group for eu and one group for NULL

    • C

      4: NULL never equals NULL, so nothing groups

    • D

      1: rows with a NULL region are dropped

  2. 15
    SELECT department, name, count(*)
    FROM employees
    GROUP BY department;
    

    What happens in PostgreSQL?

    • A

      An error: name must appear in the GROUP BY clause or be used in an aggregate function

    • B

      One row per department, with the first name by primary key

    • C

      One row per employee

    • D

      One row per department, with an arbitrary name from that department

  3. 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 WHERE runs before the grouping

    • C

      It returns the customers with more than one order

    • D

      An error: aggregate functions are not allowed in WHERE

  4. 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 WHERE clause do to the LEFT 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.status may be NULL

    • C

      Nothing: LEFT JOIN guarantees every customer appears

    • D

      It keeps every customer and blanks out the unshipped orders

  5. 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

  6. 19
    SELECT * FROM a, b;
    

    a has 3 rows and b has 4. How many rows are returned?

    • A

      12

    • B

      7

    • C

      3

    • D

      4

  7. 20
    SELECT *
    FROM t1 JOIN t2 ON t1.k = t2.k;
    

    t1 has 2 rows with k = 1; t2 has 3 rows with k = 1. How many rows does the join produce?

    • A

      3

    • B

      6

    • C

      2

    • D

      5

  8. 21
    SELECT count(*)
    FROM t1 JOIN t2 ON t1.k = t2.k;
    

    Both tables have exactly one row, and in both of them k is NULL. What is returned?

    • A

      1

    • B

      NULL

    • C

      0

    • D

      An error: cannot join on a NULL key

Date/Time Functions and Operators

2 cards
  1. 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

  2. 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

Numeric Types

1 card
  1. 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

String Functions and Operators

1 card
  1. 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

Pattern Matching

1 card
  1. 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

Character Types

2 cards
  1. 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

  2. 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|'

Lexical Structure

1 card
  1. 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

SELECT

6 cards
  1. 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 SELECT list

    • B

      An error: column "total" does not exist (in WHERE); the ORDER BY alone would have worked

    • C

      It works, but the filter is applied after sorting

    • D

      An error in ORDER BY: aliases can only be used in WHERE

  2. 31

    In what order are the clauses of a SELECT logically evaluated?

    • A

      FROMWHERESELECTGROUP BYHAVINGORDER BYLIMIT

    • B

      SELECTFROMWHEREGROUP BYHAVINGORDER BYLIMIT

    • C

      FROMWHEREGROUP BYHAVINGSELECTDISTINCTORDER BYLIMIT

    • D

      FROMSELECTWHEREGROUP BYHAVINGORDER BYLIMIT

  3. 32
    SELECT DISTINCT city, country FROM addresses;
    

    What does DISTINCT apply 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

  4. 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 ON requires an aggregate for the other columns

    • C

      One row per customer, with the placed_at and total chosen arbitrarily

    • D

      Each customer's most recent order

  5. 34
    SELECT DISTINCT name FROM users ORDER BY created_at;
    

    What happens in PostgreSQL?

    • A

      It works only if created_at is 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 BY expressions must appear in the select list

  6. 35
    SELECT count(*) FROM t;
    SELECT region, count(*) FROM t GROUP BY region;
    

    t is empty. How many rows does each query return?

    • A

      0 and 0

    • B

      1 and 0

    • C

      0 and 1

    • D

      1 and 1

LIMIT and OFFSET

1 card
  1. 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

Combining Queries (UNION, INTERSECT, EXCEPT)

1 card
  1. 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

Value Expressions

1 card
  1. 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 AND operands are evaluated is not guaranteed, so the division may run first

    • B

      No, but only because the SELECT list is evaluated before WHERE

    • C

      Yes, but only if quantity is indexed

    • D

      Yes: AND evaluates left to right and stops at the first false operand

Window Functions

3 cards
  1. 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

  2. 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 running for the two rows on day 2?

    • A

      30 for both

    • B

      60 for both

    • C

      30 and 60

    • D

      20 and 30

  3. 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

UPDATE

1 card
  1. 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 SELECT return?

    • A

      An error: a is assigned twice

    • B

      2, 2

    • C

      2, 1

    • D

      1, 2

Constraints

2 cards
  1. 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 a UNIQUE constraint

    • 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 NULL is stored as an empty string

  2. 44
    CREATE TABLE items (qty int CHECK (qty > 0));
    INSERT INTO items VALUES (NULL);
    

    What happens on the INSERT?

    • A

      An error: CHECK constraints require NOT NULL

    • B

      It succeeds, and qty is stored as 0

    • C

      An error: new row violates check constraint

    • D

      It succeeds: a CHECK constraint only rejects rows for which the condition is false, and NULL > 0 is unknown

Sequence Manipulation Functions

1 card
  1. 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 b get?

    • 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

End of deck · 45 cards

Save