Write a query to perform a case-insensitive search for a specific name in a database.
Instruction: Given a table 'contacts' with a column 'name', write a SQL query to find all records where 'name' matches 'john doe' regardless of case.
Updated
Official answer
For the specified name in PostgreSQL:
SELECT *
FROM contacts
WHERE LOWER(name) = LOWER('john doe');
This matches John Doe, JOHN DOE and john doe under the relevant case-conversion rules. It compares the whole value: John Doe Jr and a value with extra spaces do not match. NULL names are excluded.
Case conversion follows database locale rules and is not a complete solution for every Unicode equivalence, accent or normalization requirement. Define the intended comparison policy and test the supported languages. Trimming whitespace would be an additional behavior, not an automatic part of case-insensitive equality.
Bind a supplied search value. For a frequent search, a PostgreSQL expression index on LOWER(name) may support this expression; verify the plan and collation requirements. Other databases may offer case-insensitive collations with different behavior, so name the engine and required semantics rather than assuming identical results everywhere.