1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. Which predicate on an indexed order_date column can use a…

Added 30 Nov -0001

Which predicate on an indexed order_date column can use a B-tree index?

  1. CAST(order_date AS TEXT) LIKE '2024%', which cannot use the index
  2. order_date + INTERVAL '1 day' = '2024-06-01', a computed date
  3. order_date >= '2024-01-01' AND order_date < '2025-01-01' Correct answer
  4. EXTRACT(YEAR FROM order_date) = 2024, which scans every row

Correct answer

C. order_date >= '2024-01-01' AND order_date < '2025-01-01'

17 views

MCQSEXAM

Explanation

Sargability means the indexed column is compared directly with a value, without a function or arithmetic wrapping it, so the engine can seek a range instead of scanning. EXTRACT, CAST and arithmetic on the column force per-row evaluation and usually a full scan. Rewriting as a half-open range on the raw column also handles the time component correctly.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs