Stored procedures - Example - Part II
As the question was asking about finding the average salary, does this query work as well? I thought that we had to take into account the repeats in emp_no.
SELECT
ROUND(AVG(salary), 2) AS average_salary
FROM
(SELECT
emp_no, salary, MAX(from_date), MAX(to_date)
FROM
salaries
GROUP BY emp_no
ORDER BY emp_no) AS A; Also, I would like to understand more about the Error Code 1248: Every derived table must have its own alias. What's the point of having the alias when I do not use it when executing it? Is there a more elegant way to type the code? Thanks in advance. :)
ROUND(AVG(salary), 2) AS average_salary
FROM
(SELECT
emp_no, salary, MAX(from_date), MAX(to_date)
FROM
salaries
GROUP BY emp_no
ORDER BY emp_no) AS A; Also, I would like to understand more about the Error Code 1248: Every derived table must have its own alias. What's the point of having the alias when I do not use it when executing it? Is there a more elegant way to type the code? Thanks in advance. :)
1 answers ( 0 marked as helpful)
Hi Vivian!
Thanks for reaching out.
Best,
Martin
- This is a great observation. Yes, it does work. In our example, we wanted to focus on creating a MySQL Procedure, hence we have simplified that part of the answer.
- Providing the specific query you are referring to may help in our explanation. However, in general, the idea is that you need to provide aliases when using a subquery, since this is a selection of information that exists only temporarily. You can say that it exists only "in the life of the query", so to speak. Therefore, it needs to be given an alias for a reference.
Best,
Martin