String, Date & CASE Expressions Questions with Answers

19 practice questions on String, Date & CASE Expressions (SQL) with answers and step-by-step solutions. Try 10 samples below, then practise all 19 free in the AptiNest app.

Sample questions

Question 1Easy

What does this query return?

SELECT SUBSTR('Cognizant', 2, 4);
  1. ognizant
  2. gniz
  3. Cogn
  4. ogni

Answer: D. ogni

Explanation

SUBSTR(s, start, length) uses 1-based positions: four characters starting at position 2 give ogni.

Step-by-step solution

  1. SUBSTR starts at position 2, which is the letter o.
  2. It takes 4 characters: o, g, n, i.
  3. So the result is ogni, option D.

Why the other options are wrong

  • A. This would be the text from position 2 to the end, not four characters.
  • B. Gniz starts at position 3 and is the wrong letter range.
  • C. Cogn starts at position 1, not 2.

Tip: SUBSTR positions start at 1, not 0.

Question 2Easy

What does this query return?

SELECT SUBSTR('2024-07-20', 1, 4) || '/' || SUBSTR('2024-07-20', 6, 2);
  1. 2024-07
  2. 2024/20
  3. 2024/07
  4. 2024/7

Answer: C. 2024/07

Explanation

The first SUBSTR takes characters 1–4 (the year) and the second takes 2 characters from position 6 (the month).

Step-by-step solution

  1. SUBSTR('2024-07-20', 1, 4) takes the year, 2024.
  2. SUBSTR('2024-07-20', 6, 2) starts at position 6 and takes 2 characters, giving 07.
  3. Joined with a slash, the result is 2024/07, option C.

Why the other options are wrong

  • A. The slashes replace the dash, so the output is 2024/07, not 2024-07.
  • B. Position 6 starts the month, so the second part is 07, not 20.
  • D. SUBSTR takes 2 characters, so the month is 07, not 7.

Tip: Count SUBSTR positions from 1: the month in 2024-07-20 starts at position 6.

Question 3Easy

Which function returns the first non-NULL value among its arguments in standard SQL?

  1. ISNULL
  2. NULLIF
  3. NVL2
  4. COALESCE

Answer: D. COALESCE

Explanation

COALESCE(a, b, ...) is standard. NVL/NVL2 are Oracle, ISNULL is SQL Server/MySQL-specific, and NULLIF does the opposite job.

Step-by-step solution

  1. COALESCE returns the first non-NULL argument.
  2. It is standard SQL.

Why the other options are wrong

  • A. ISNULL is vendor-specific.
  • B. NULLIF returns NULL when two values are equal.
  • C. NVL2 is Oracle-specific.

Tip: COALESCE = first non-null.

Question 4Easy

What does this query return?

trainees
id | name   | batch | score | joined
1  | Pooja  | B1    | 82    | 2024-01-15
2  | Manish | B2    | 67    | 2024-03-02
3  | Lata   | B1    | NULL  | 2024-01-15
4  | Imran  | B3    | 91    | 2024-07-20
5  | Gita   | B2    | 74    | 2024-03-02
6  | Farhan | B1    | 58    | 2024-02-10
SELECT name,
       CASE WHEN score >= 80 THEN 'A'
            WHEN score >= 60 THEN 'B'
            ELSE 'C' END AS grade
FROM trainees
WHERE id IN (1, 3, 6)
ORDER BY id;
  1. Pooja A, Lata NULL, Farhan C
  2. Pooja A, Lata C, Farhan B
  3. Pooja B, Lata C, Farhan C
  4. Pooja A, Lata C, Farhan C

Answer: D. Pooja A, Lata C, Farhan C

Explanation

CASE stops at the first TRUE branch. For Lata, both comparisons with NULL are UNKNOWN, so ELSE gives C; Farhan’s 58 is below 60.

Step-by-step solution

  1. Pooja has score 82, which is at least 80, so grade A.
  2. Lata has a NULL score; both comparisons are UNKNOWN, so ELSE gives C.
  3. Farhan has 58, which is below 60, so ELSE gives C; the answer is option D.

Why the other options are wrong

  • A. Lata's NULL score goes to ELSE, so the grade is C, not NULL.
  • B. Lata is C, but Farhan's 58 is below 60, so he is C, not B.
  • C. Pooja's 82 is at least 80, so she is A, not B.

Tip: CASE skips UNKNOWN conditions, so a NULL falls to ELSE.

Question 5Medium

What does this query return?

SELECT ROUND(2.567, 2), ROUND(-2.5), ABS(-7) % 4;

(SQLite.)

  1. 2.57, -2.0, 1
  2. 2.57, -3.0, 3
  3. 2.57, -3.0, -3
  4. 2.56, -2.0, 3

Answer: B. 2.57, -3.0, 3

Explanation

ROUND to 2 places gives 2.57; SQLite rounds halves away from zero, so -2.5 becomes -3.0. ABS(-7) is 7 and 7 % 4 is 3.

Step-by-step solution

  1. ROUND(2.567, 2) rounds to two decimal places, giving 2.57.
  2. SQLite rounds halves away from zero, so ROUND(-2.5) gives -3.0.
  3. ABS(-7) is 7 and 7 % 4 is 3, so the answer is option B, 2.57, -3.0, 3.

Why the other options are wrong

  • A. ROUND(-2.5) is -3.0 because halves round away from zero, not -2.0.
  • C. ABS makes -7 into 7 before the modulo, so the last value is 3, not -3.
  • D. 2.567 rounded to two places is 2.57, not 2.56.

Tip: Round halves away from zero in SQLite, and apply ABS before the modulo.

Question 6Medium

Which names does this query return (SQLite)?

trainees
id | name   | batch | score | joined
1  | Pooja  | B1    | 82    | 2024-01-15
2  | Manish | B2    | 67    | 2024-03-02
3  | Lata   | B1    | NULL  | 2024-01-15
4  | Imran  | B3    | 91    | 2024-07-20
5  | Gita   | B2    | 74    | 2024-03-02
6  | Farhan | B1    | 58    | 2024-02-10
SELECT name FROM trainees
WHERE strftime('%m', joined) = '03'
ORDER BY name;
  1. Manish
  2. No rows, because joined is text
  3. Gita, Manish
  4. Gita, Manish, Imran

Answer: C. Gita, Manish

Explanation

strftime('%m', ...) extracts the two-digit month from an ISO date string; both March joiners are returned.

Step-by-step solution

  1. strftime('%m', joined) gives the two-digit month of each date.
  2. Only Manish and Gita joined in March (2024-03-02).
  3. Ordered by name, the answer is Gita, Manish, option C.

Why the other options are wrong

  • A. Gita also joined in March, so she must be included.
  • B. Joined is text, but the date text is in ISO format, so the function works.
  • D. Imran joined in July, so he is not returned.

Tip: Use strftime with a format such as '%m' to pick out parts of an ISO date in SQLite.

Question 7Medium

Which expression returns 'Pass' when marks >= 40 and 'Fail' otherwise, including 'Fail' for NULL marks?

  1. CASE marks WHEN >= 40 THEN 'Pass' ELSE 'Fail' END
  2. CASE WHEN marks >= 40 THEN 'Pass' END
  3. CASE WHEN marks < 40 THEN 'Fail' ELSE 'Pass' END
  4. CASE WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' END

Answer: D. CASE WHEN marks >= 40 THEN 'Pass' ELSE 'Fail' END

Explanation

For NULL marks the WHEN condition is UNKNOWN, so the ELSE branch gives 'Fail'. A is invalid syntax, B gives NULL for NULL marks, and C gives 'Pass' for NULL marks.

Step-by-step solution

  1. The expression needs a searched CASE with a WHEN for marks at least 40 and an ELSE for Fail.
  2. For NULL marks the WHEN condition is UNKNOWN, so ELSE gives Fail.
  3. Option D has exactly that structure, so the answer is option D.

Why the other options are wrong

  • A. CASE marks WHEN >= 40 is invalid; a comparison cannot follow the simple CASE value like that.
  • B. With no ELSE, a failing mark or NULL gives NULL, not Fail.
  • C. NULL marks fall to ELSE, which gives Pass, so the NULL rule is broken.

Tip: A searched CASE with ELSE handles NULL: an UNKNOWN condition never matches a WHEN.

Question 8Medium

What does this query return?

SELECT CASE 3 WHEN 1 THEN 'one' WHEN 3 THEN 'three' WHEN 3 THEN 'again' ELSE 'other' END;
  1. three
  2. three again
  3. again
  4. other

Answer: A. three

Explanation

A simple CASE compares the value with each WHEN in order and returns the first match only.

Step-by-step solution

  1. A simple CASE compares 3 with each WHEN value in order.
  2. WHEN 1 does not match, and WHEN 3 matches, so 'three' is returned.
  3. Later WHEN 3 is never checked, so the answer is option A, three.

Why the other options are wrong

  • B. CASE returns only the first match, so the text is 'three' alone, not 'three again'.
  • C. The first matching WHEN is WHEN 3 'three', not the later 'again'.
  • D. 3 matches a WHEN, so ELSE 'other' is not used.

Tip: A simple CASE returns the first WHEN that matches; later matches are ignored.

Question 9Hard

What does this query return?

matches
mid | home   | away   | hg   | ag
1   | Lions  | Tigers | 2    | 1
2   | Bears  | Lions  | 0    | 0
3   | Tigers | Bears  | 3    | 2
4   | Lions  | Bears  | 1    | 3
5   | Tigers | Lions  | NULL | NULL
SELECT mid,
       CASE WHEN hg > ag THEN home
            WHEN hg < ag THEN away
            ELSE 'Draw' END AS result
FROM matches
WHERE mid = 5;
  1. 5 Draw
  2. 5 Tigers
  3. 5 NULL
  4. No rows

Answer: A. 5 Draw

Explanation

Both comparisons with NULL are UNKNOWN, so ELSE fires and an unplayed match is wrongly reported as a draw — a common bug.

Step-by-step solution

  1. For match 5, hg and ag are NULL.
  2. Hg > ag and hg < ag are both UNKNOWN, so neither WHEN fires.
  3. ELSE gives 'Draw', so the output is 5 Draw (option A).

Why the other options are wrong

  • B. Tigers would need a TRUE comparison, and none is TRUE when the scores are NULL.
  • C. The ELSE branch returns the text 'Draw', not NULL.
  • D. The row for mid 5 exists, so the query returns one row.

Tip: Put an IS NULL check before comparisons in CASE, or the unplayed match is reported as a draw.

Question 10Hard

What does this query return?

SELECT 'Total: ' || NULL, COALESCE('Total: ' || NULL, 'n/a');

(SQLite.)

  1. Total: , n/a
  2. NULL, n/a
  3. Total: , Total:
  4. Total: NULL, Total: NULL

Answer: B. NULL, n/a

Explanation

Concatenating NULL with || yields NULL (Oracle is the notable exception that treats NULL as empty), so COALESCE falls back to n/a.

Step-by-step solution

  1. 'Total: ' || NULL is NULL, because || with NULL gives NULL in SQLite.
  2. COALESCE takes the first non-NULL argument, so the first value is NULL.
  3. The NULL falls back to 'n/a', so the answer is option B.

Why the other options are wrong

  • A. The first column is NULL, not 'Total: ', because || with NULL gives NULL.
  • C. The first column is NULL, and the second is 'n/a', not 'Total:'.
  • D. Concatenating NULL gives NULL, not the text 'NULL'.

Tip: In SQLite, text joined with NULL is NULL; wrap it in COALESCE to get a fallback.

Practise all 19 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all SQL topics · All topics