Which of the following MySQL query will display all the NAME from the table…

2023

Which of the following MySQL query will display all the NAME from the table STUDENT, which are having exactly five characters and starting with the alphabet ‘A’?

Answer: A. SELECT NAME FROM STUDENT WHERE NAME LIKE 'A____';Concept. In SQL, pattern matching inside a WHERE clause is the job of the LIKE operator, never of the equality operator. LIKE reads two wildcards: an…

  1. A.

    SELECT NAME FROM STUDENT

    WHERE NAME LIKE 'A____';

  2. B.

    SELECT NAME FROM STUDENT

    WHERE NAME = 'A____';

  3. C.

    SELECT NAME FROM STUDENT

    WHERE NAME = 'A%%%%%';

  4. D.

    SELECT NAME FROM STUDENT

    WHERE NAME LIKE 'A%%%%%';

Attempted by 2052 students.

Show answer & explanation

Correct answer: A

Concept. In SQL, pattern matching inside a WHERE clause is the job of the LIKE operator, never of the equality operator. LIKE reads two wildcards: an underscore (_) stands for a single character of any kind, and a percent sign (%) stands for a run of characters of any length, including an empty run. With = the two strings are compared literally, so an underscore or a percent sign inside the quoted text is just that printable symbol.

Applying it here. The name has to start with A and has to be five characters long, so the length must be pinned by the pattern itself. A length can only be pinned by wildcards that each consume a fixed number of positions, and the underscore is the wildcard that consumes one position. Writing four underscores after the A therefore fixes the four remaining positions, one character each, which is the pattern 'A____', used with LIKE.

Comparing the four statements by value.

Statement

Names it matches

LIKE 'A____'

Names of five characters that begin with A: the four underscores fix four further positions at one character each.

LIKE 'A%%%%%'

Any name that begins with A, at any length: repeated percent signs collapse to a single one, which places no ceiling on the length.

= 'A____'

Only a name spelled A followed by four underscore symbols, because equality does no wildcard expansion.

= 'A%%%%%'

Only a name spelled A followed by five percent symbols, because equality does no wildcard expansion.

Note on the paper’s wording. The original paper printed this stem as “maximum five characters”. Read strictly, maximum five means at most five, that is a length of 1 to 5, and LIKE wildcards alone cannot express an upper bound on length — that needs a length function, for example WHERE NAME LIKE 'A%' AND LENGTH(NAME) <= 5. No statement in the paper’s own set implements that reading, and the official answer key marks the four-underscore LIKE pattern correct, so the requirement is a name of five characters and the stem is stated that way here. Among the four statements offered, that pattern is the one that restricts the length at all: the two equality statements do no pattern matching, and the LIKE pattern built from percent signs leaves the length unbounded.

Answer. SELECT NAME FROM STUDENT WHERE NAME LIKE 'A____';

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Up Lt Grade Assistant Teacher 2025

Loading lesson…