Alias — SQL Technical Interview Questions & Answers

Alias

Alias

  • Alias is a nickname. SELECT name AS student FROM students; column alias. FROM students AS s — table alias. Required when the same table is used twice or a derived table.
  • AS is optional: students s. Use aliases in JOIN … ON s.roll = m.roll so every column is qualified.
  • Trap: SELECT marks - 40 fail without quotes — fail looks like a column. AS fail or AS 'fail'. Trap: using the alias in WHERE (WHERE runs before SELECT aliases in MySQL — use HAVING or repeat the expression).
  • On the example next to this theory: Alias: create two demo rows, then SELECT qty >= 2 ordered. Say which labels come back.

Technical Interview Tip: SELECT name AS student FROM students s;

Code Examples for Alias

Alias — sample query

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

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

How to Explain Alias to an Interviewer

Why table aliases?

Shorter names Self-join and JOIN need them