LOCATE()() — SQL Technical Interview Questions & Answers

LOCATE()()

LOCATE()()

  • LOCATE() position of substring, optional start. You use it in SELECT (and sometimes WHERE/UPDATE). It does not change stored data unless you UPDATE … SET col = LOCATE(col).
  • Board: SELECT LOCATE('a', name) FROM students; Run it. Say the result out loud. If the column is NULL, the result is often NULL — wrap with IFNULL if you need a blank instead.
  • String indexes in MySQL start at 1, not 0 (SUBSTR, INSTR, LEFT). 1-based. 0 = not found.
  • Interview: name LOCATE, one argument list, one example on the students table. Don’t recite every string function.
  • On the example next to this theory: LOCATE()(): create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: Say what LOCATE returns, then SELECT LOCATE('a', name) FROM students;

Code Examples for LOCATE()()

LOCATE()() — sample query

-- LOCATE()()
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;

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

How to Explain LOCATE()() to an Interviewer

What does LOCATE do?

position of substring, optional start SELECT LOCATE('a', name) FROM students;

Does it UPDATE the table?

No, unless you write UPDATE SELECT only prints