Vidyalelo
MySQL · Q623

MySQL

Programming · MySQL · question 623

Q623

Find the error in the following MySQL statement? SELECT cust_id, fed_id FROM customer WHERE cust_id = ’I’ AND fed_id BETWEEN 5000-00-000 AND 9999-999-000;

A.
Yes
B.
No
Answer
C.
Range too high to compare
D.
None of the mentioned

Answer: Option B

Solution

Answer: Option B
Solution:
This question tests your understanding of how to use the WHERE clause in MySQL, specifically when working with numeric data. Let's break down the code and find the error:
The Problem
The error lies in the BETWEEN clause for `fed_id`:
```sql AND fed_id BETWEEN 5000-00-000 AND 9999-999-000; ```
MySQL interprets `5000-00-000` and `9999-999-000` as date values because of the hyphens (-) and leading zeros. Since `fed_id` is likely a numeric field (customer ID), this comparison is incorrect.
Solution
To fix the error, we should remove the hyphens and leading zeros from the `BETWEEN` clause:
```sql AND fed_id BETWEEN 5000000 AND 9999999; ```
Explanation
The corrected statement now compares `fed_id` to a numeric range between 5000000 and 9999999.
Answer
The correct answer is Option A: Yes. There is an error in the statement.