Examveda

Is there any error in the following MySQL statement?
SELECT e.emp_id, e.fname,e.lname,d.name
FROM employee AS e INNER JOIN department AS d
ON e.dept_id=e.dept_id;

A. NO

B. YES

C. DEPEND

D. None of the mentioned

Answer: Option A

Solution (By Examveda Team)

This question asks if there's an error in a MySQL query. Let's break down the query and see if anything is wrong:
The Query:
```sql SELECT e.emp_id, e.fname, e.lname, d.name FROM employee AS e INNER JOIN department AS d ON e.dept_id = e.dept_id; ```
Explanation:
* SELECT: This part tells MySQL what data to retrieve. * e.emp_id, e.fname, e.lname, d.name: These are the columns we want to get. * FROM employee AS e INNER JOIN department AS d: This joins the `employee` and `department` tables. The `INNER JOIN` keeps only rows where the `dept_id` matches in both tables. * ON e.dept_id = e.dept_id: This is the JOIN condition – it specifies how the tables are connected.
The Error:
The error lies in the JOIN condition. It says `e.dept_id = e.dept_id`. This compares the `dept_id` column within the same table (`employee`)! You should compare `dept_id` in the `employee` table with `dept_id` in the `department` table for the join to work correctly.
Correct Join Condition:
```sql ON e.dept_id = d.dept_id ```
Answer:
Therefore, the answer is Option B: YES. There is an error in the MySQL statement.

This Question Belongs to MySQL >> MySQL Miscellaneous

Join The Discussion

Related Questions on MySQL Miscellaneous