Skip to content
Exercises · Q2

Q.Write the output produced by the following SQL commands:

(a) SELECT POW(2,3);
(b) SELECT ROUND(123.2345, 2), ROUND(342.9234, -1);
(c) SELECT LENGTH("Informatics Practices");
(d) SELECT YEAR("1979/11/26"), MONTH("1979/11/26"), DAY("1979/11/26"), MONTHNAME("1979/11/26");
(e) SELECT LEFT("INDIA", 3), RIGHT("Computer Science", 4);
(f) SELECT MID("Informatics", 3, 4), SUBSTR("Practices", 3);
Tamil Nadu DgeTextbookSubjective· 4mImportance★★★★★
40% · 16/40 Questions
✓ Free question

Predicting the output of SQL built-in functions for numeric operations, string manipulation, and date extraction.

Why these functions matter

SQL provides a rich library of built-in functions that transform data directly in queries. Numeric functions like POW and ROUND handle mathematical operations; string functions like LENGTH, LEFT, RIGHT, MID, and SUBSTR slice and measure text; date functions like YEAR, MONTH, DAY, and MONTHNAME extract components from dates. Understanding their exact behavior—return types, indexing conventions, rounding rules—is essential for writing correct queries and predicting results.

Each function below is deterministic: given the same input, it always returns the same output. The key is knowing the signature and the edge cases.


(a) SELECT POW(2,3);

POW(base, exponent) raises the base to the power of the exponent: 23=82^3 = 8.

SELECT POW(2,3);

Output:

POW(2,3)
8

(b) SELECT ROUND(123.2345, 2), ROUND(342.9234, -1);

ROUND(number, decimals) rounds a number to the specified number of decimal places. When decimals is negative, it rounds to the left of the decimal point.

  • ROUND(123.2345, 2): round to 2 decimal places → 123.23123.23 (the third decimal is 4, which is < 5, so we round down).
  • ROUND(342.9234, -1): round to the nearest ten (one place left of the decimal) → 340340 (the units digit is 2, which is < 5, so the tens place stays 4, giving 340).
SELECT ROUND(123.2345, 2), ROUND(342.9234, -1);

Output:

ROUND(123.2345, 2)ROUND(342.9234, -1)
123.23340

(c) SELECT LENGTH("Informatics Practices");

LENGTH(string) returns the number of characters in the string, including spaces.

"Informatics Practices" has:

  • "Informatics" = 11 characters
  • " " (space) = 1 character
  • "Practices" = 9 characters
  • Total = 11+1+9=2111 + 1 + 9 = 21
SELECT LENGTH("Informatics Practices");

Output:

LENGTH("Informatics Practices")
21

(d) SELECT YEAR("1979/11/26"), MONTH("1979/11/26"), DAY("1979/11/26"), MONTHNAME("1979/11/26");

These functions extract components from a date string in the format YYYY/MM/DD:

  • YEAR("1979/11/26") → 1979
  • MONTH("1979/11/26") → 11
  • DAY("1979/11/26") → 26
  • MONTHNAME("1979/11/26") → "November" (the name of the 11th month)
SELECT YEAR("1979/11/26"), MONTH("1979/11/26"), DAY("1979/11/26"), MONTHNAME("1979/11/26");

Output:

YEAR("1979/11/26")MONTH("1979/11/26")DAY("1979/11/26")MONTHNAME("1979/11/26")
19791126November

(e) SELECT LEFT("INDIA", 3), RIGHT("Computer Science", 4);

  • LEFT(string, n) returns the leftmost n characters.
    • LEFT("INDIA", 3) → "IND"
  • RIGHT(string, n) returns the rightmost n characters.
    • RIGHT("Computer Science", 4) → "ence" (the last 4 characters of "Computer Science")
SELECT LEFT("INDIA", 3), RIGHT("Computer Science", 4);

Output:

LEFT("INDIA", 3)RIGHT("Computer Science", 4)
INDence

(f) SELECT MID("Informatics", 3, 4), SUBSTR("Practices", 3);

  • MID(string, start, length) extracts length characters starting from position start. In MySQL, string positions are 1-indexed.

    • MID("Informatics", 3, 4): start at position 3 (the letter 'f'), take 4 characters → "form"
      • Positions: I(1), n(2), f(3), o(4), r(5), m(6), a(7), t(8), i(9), c(10), s(11)
      • Characters 3–6: "form"
  • SUBSTR(string, start) (or SUBSTRING) extracts from position start to the end of the string.

    • SUBSTR("Practices", 3): start at position 3 (the letter 'a'), take the rest → "actices"
      • Positions: P(1), r(2), a(3), c(4), t(5), i(6), c(7), e(8), s(9)
      • From position 3 onward: "actices"
SELECT MID("Informatics", 3, 4), SUBSTR("Practices", 3);

Output:

MID("Informatics", 3, 4)SUBSTR("Practices", 3)
formactices
Watch out

SQL string functions are 1-indexed, not 0-indexed like Python. MID("Informatics", 1, 1) returns "I", not "n".


✓Final answer

(a) 8

(b) 123.23, 340

(c) 21

(d) 1979, 11, 26, November

(e) IND, ence

(f) form, actices

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.