Math — SQL Technical Interview Questions & Answers

Math

Math

  • Math functions: ABS, ROUND, FLOOR, CEIL, MOD, POW. SELECT ROUND(AVG(marks), 1) FROM students;
  • Use DECIMAL for money, then ROUND for display. RAND() is random — not for cryptography.
  • Trap: TRUNCATE(x,d) vs TRUNCATE TABLE. Different commands.
  • On the example next to this theory: Math: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: ROUND / ABS / MOD with one numeric SELECT.

Code Examples for Math

Math — sample query

-- Math
CREATE TABLE demo (
  id INTEGER PRIMARY KEY,
  label TEXT NOT NULL,
  qty INTEGER DEFAULT 0
);
INSERT INTO demo (id, label, qty) VALUES
  (1, 'alpha', 2),
  (2, 'beta', 5);
SELECT label, qty FROM demo WHERE qty >= 2 ORDER BY qty DESC;

Math: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

How to Explain Math to an Interviewer

Round average marks to 1 decimal?

SELECT ROUND(AVG(marks), 1) FROM students;