Which of the following statement contains an error?

2013

Which of the following statement contains an error?

  1. A.

    Select * from EMP where EMPID = 15;

  2. B.

    Select EMPID from EMP where EMPID = 15;

  3. C.

    Select EMPID from EMP;

  4. D.

    Select EMPID where EMPID = 15 and LASTNAME = 'Singh';

Attempted by 16 students.

Show answer & explanation

Correct answer: D

Concept

Every standard SQL SELECT query that reads values from a stored table must name that table in a FROM clause. The FROM clause is what tells the engine which table the column names and the WHERE filter belong to. Omit it while still referencing table columns and the statement is syntactically invalid.

Applying it here

Walk each statement and check whether the table it reads from is declared:

  • Select * from EMP where EMPID = 15; — names the table with from EMP, so every column and the filter resolve against EMP. Valid.

  • Select EMPID from EMP where EMPID = 15; — selects one column from the declared table EMP and filters on it. Valid.

  • Select EMPID from EMP; — selects a column from EMP with no filter, returning that column for all rows. Valid.

  • Select EMPID where EMPID = 15 and LASTNAME = 'Singh'; — selects EMPID and filters on EMPID and LASTNAME, but never declares a FROM clause. There is no table for those columns to come from, so the parser rejects it. This is the statement with the error.

Cross-check

The rule is: literals and expressions can be selected without a FROM (e.g. SELECT 1+1), but the moment you reference a base-table column such as EMPID or LASTNAME, a FROM clause naming that table becomes mandatory. The faulty statement breaks exactly this rule.

Explore the full course: Btsc Lab Assistant