SELECT Record — SQL Technical Interview Questions & Answers

SELECT Record

SELECT Record

  • SELECT name, marks FROM students WHERE marks < 40 ORDER BY marks; is the whole language in one line. SELECT = which columns. FROM = which table. WHERE = which rows. ORDER BY = sort.
  • SELECT * is fine at home. In interviews, list columns. DISTINCT removes duplicate rows in the result. LIMIT 10 is ‘first 10’ after sorting.
  • Functions: SELECT COUNT(*), AVG(marks) FROM students; aggregates. They collapse rows unless you GROUP BY.
  • Trap: WHERE after GROUP BY (illegal — use HAVING). Trap: SELECT extra columns not in GROUP BY (ONLY_FULL_GROUP_BY error).
  • On the example next to this theory: SELECT Record — sELECT projects columns; WHERE filters; ORDER BY sorts.
FROM students
       │ WHERE marks < 40
       ▼
  matching rows
       │ SELECT name, marks
       ▼
  result table

Technical Interview Tip: SELECT name, marks FROM students WHERE marks < 40;

Code Examples for SELECT Record

SELECT Record — sample query

CREATE TABLE employees (id INTEGER PRIMARY KEY, name TEXT, dept TEXT, salary INTEGER);
INSERT INTO employees VALUES
  (1, 'Asha', 'Eng', 70000),
  (2, 'Ravi', 'Eng', 65000),
  (3, 'Neha', 'HR', 50000);
SELECT name, salary FROM employees WHERE dept = 'Eng' ORDER BY salary DESC;

SELECT Record — sELECT projects columns; WHERE filters; ORDER BY sorts.

How to Explain SELECT Record to an Interviewer

Write a SELECT for fail students.

SELECT name, marks FROM students WHERE marks < 40;

What does FROM do?

Picks the table