1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. A query divides two integer columns as 7 / 2. What is…

Added 30 Nov -0001

A query divides two integer columns as 7 / 2. What is returned, and how is a decimal result obtained?

  1. An error, because dividing two integers requires an explicit CAST in standard SQL
  2. 3, because integer division truncates; cast one operand to numeric to get 3.5 Correct answer
  3. 3.5, because SQL always performs division in floating point internally
  4. 4, because SQL rounds the exact quotient half up when both operands are ints

Correct answer

B. 3, because integer division truncates; cast one operand to numeric to get 3.5

16 views

MCQSEXAM

Explanation

Integer division truncates toward zero when both operands are integers, so 7/2 yields 3 in PostgreSQL and most engines. Casting either operand, as in 7::numeric / 2, forces decimal arithmetic and returns 3.5. Averaging integer columns behaves similarly, which is why AVG(col) returns a numeric type while a hand-written SUM(col)/COUNT(*) may not.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is B? 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