Which of the following statement contains an error?
2013
Which of the following statement contains an error?
- A.
Select * from EMP where EMPID = 15;
- B.
Select EMPID from EMP where EMPID = 15;
- C.
Select EMPID from EMP;
- 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 withfrom 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';— selectsEMPIDand filters onEMPIDandLASTNAME, but never declares aFROMclause. 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.