Syntax — SQL Technical Interview Questions & Answers

Syntax

Syntax

  • MySQL statements: keyword, names, ; at the end. Keywords are not case-sensitive (select vs SELECT). String values are. Names: letters, numbers, underscore. Use backticks `order` if you must use a reserved word.
  • Comments: -- to end of line, or /* block */. Strings in single quotes. Double quotes depend on sql_mode ANSI_QUOTES.
  • Trap: semicolon missing in the CLI so the next line ‘is part of the query’. Trap: “smart quotes” from Word.
  • On the example next to this theory: Syntax: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: SELECT … ; -- comment. Quotes for strings, backticks for names.

Code Examples for Syntax

Syntax — sample query

-- Syntax
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;

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

How to Explain Syntax to an Interviewer

How do you comment in MySQL?

-- line /* block */