Consider a "CUSTOMERS" database table having a column "CITY" filled with all…

2015

Consider a "CUSTOMERS" database table having a column "CITY" filled with all the names of Indian cities (in capital letters). The SQL statement that finds all cities that have "GAR" somewhere in its name, is:

  1. A. select * from customers where city='%GAR%';
  2. B. select * from customers where city='$GAR$';
  3. C. select * from customers where city like '%GAR%';
  4. D. select * from customers wherecity as'%GAR';

Attempted by 974 students.

Show answer & explanation

Correct answer: C

Answer: select * from customers where city like '%GAR%';

Explanation: Use the LIKE operator with % as the wildcard to match a substring. The pattern '%GAR%' matches any city name that contains the sequence GAR anywhere within it. The % character represents zero or more characters.

  • select * from customers where city='%GAR%'; — Using = compares the entire string; % is treated as a literal character here, not a wildcard.

  • select * from customers where city='$GAR$'; — The $ character is not a SQL wildcard, so this looks for the literal string '$GAR$' and will not find cities that merely contain GAR.

  • select * from customers where city as'%GAR'; — This is invalid syntax. 'AS' is used for aliases in SELECT, not for comparisons in WHERE. Also, to search by pattern you should use LIKE.

Note: Because the CITY column values are given in uppercase, the pattern '%GAR%' will match those uppercase occurrences. If the database collation is case-sensitive and data might be mixed case, consider using an appropriate case-insensitive comparison or convert values to a common case.

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

Explore the full course: Mppsc Assistant Professor