What is the error “Every derived table must have its own alias” in MySQL?
up vote
303
down vote
favorite
I am running this query on MySQL
SELECT ID FROM (
SELECT ID, msisdn
FROM (
SELECT * FROM TT2
)
);
and it is giving this error:
Every derived table must have its own alias.
What's causing this error?
mysql mysql-error-1248
add a comment |
up vote
303
down vote
favorite
I am running this query on MySQL
SELECT ID FROM (
SELECT ID, msisdn
FROM (
SELECT * FROM TT2
)
);
and it is giving this error:
Every derived table must have its own alias.
What's causing this error?
mysql mysql-error-1248
13
Couldn't you just simplify this as "select ID from TT2"?
– DMKing
Dec 11 '09 at 15:29
2
I got this error recently because I had an extra)
in a query with a lot ofUNION ALL
s.
– mpen
Feb 17 '12 at 0:52
10
Seeing as how this is the #1 Google search... The accepted answer doesn't really answer the error 'Every derived table must have its own alias'. Look below for more info.
– Daniel B. Chapman
Aug 30 '12 at 16:01
add a comment |
up vote
303
down vote
favorite
up vote
303
down vote
favorite
I am running this query on MySQL
SELECT ID FROM (
SELECT ID, msisdn
FROM (
SELECT * FROM TT2
)
);
and it is giving this error:
Every derived table must have its own alias.
What's causing this error?
mysql mysql-error-1248
I am running this query on MySQL
SELECT ID FROM (
SELECT ID, msisdn
FROM (
SELECT * FROM TT2
)
);
and it is giving this error:
Every derived table must have its own alias.
What's causing this error?
mysql mysql-error-1248
mysql mysql-error-1248
edited Dec 7 '17 at 16:58
Pale Blue Dot
3,58076195
3,58076195
asked Dec 11 '09 at 15:24
silverkid
3,281195887
3,281195887
13
Couldn't you just simplify this as "select ID from TT2"?
– DMKing
Dec 11 '09 at 15:29
2
I got this error recently because I had an extra)
in a query with a lot ofUNION ALL
s.
– mpen
Feb 17 '12 at 0:52
10
Seeing as how this is the #1 Google search... The accepted answer doesn't really answer the error 'Every derived table must have its own alias'. Look below for more info.
– Daniel B. Chapman
Aug 30 '12 at 16:01
add a comment |
13
Couldn't you just simplify this as "select ID from TT2"?
– DMKing
Dec 11 '09 at 15:29
2
I got this error recently because I had an extra)
in a query with a lot ofUNION ALL
s.
– mpen
Feb 17 '12 at 0:52
10
Seeing as how this is the #1 Google search... The accepted answer doesn't really answer the error 'Every derived table must have its own alias'. Look below for more info.
– Daniel B. Chapman
Aug 30 '12 at 16:01
13
13
Couldn't you just simplify this as "select ID from TT2"?
– DMKing
Dec 11 '09 at 15:29
Couldn't you just simplify this as "select ID from TT2"?
– DMKing
Dec 11 '09 at 15:29
2
2
I got this error recently because I had an extra
)
in a query with a lot of UNION ALL
s.– mpen
Feb 17 '12 at 0:52
I got this error recently because I had an extra
)
in a query with a lot of UNION ALL
s.– mpen
Feb 17 '12 at 0:52
10
10
Seeing as how this is the #1 Google search... The accepted answer doesn't really answer the error 'Every derived table must have its own alias'. Look below for more info.
– Daniel B. Chapman
Aug 30 '12 at 16:01
Seeing as how this is the #1 Google search... The accepted answer doesn't really answer the error 'Every derived table must have its own alias'. Look below for more info.
– Daniel B. Chapman
Aug 30 '12 at 16:01
add a comment |
3 Answers
3
active
oldest
votes
up vote
433
down vote
accepted
Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever
), which can the be used to refer to it in the rest of the outer query.
SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T
In your case, of course, the entire query could be replaced with:
SELECT ID FROM TT2
16
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
1
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
2
Sorry, I didnt see you also fixed the original query and added theAS
statements. I thought you only showed the shorthand. removed my downvote.
– ToBe
May 22 '15 at 15:46
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
add a comment |
up vote
67
down vote
I think it's asking you to do this:
SELECT ID
FROM (SELECT ID,
msisdn
FROM (SELECT * FROM TT2) as myalias
) as anotheralias;
But why would you write this query in the first place?
11
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
9
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
add a comment |
up vote
12
down vote
Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT
).
Imagine a table called purchases
that records purchases made by customers
at stores
, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
GROUP BY customer_id HAVING 1 < SUM(1);
..will break with the error Every derived table must have its own alias
. To fix:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
GROUP BY customer_id HAVING 1 < SUM(1);
( Note the AS custom
alias).
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
add a comment |
protected by ken2k Jan 4 '17 at 16:13
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
433
down vote
accepted
Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever
), which can the be used to refer to it in the rest of the outer query.
SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T
In your case, of course, the entire query could be replaced with:
SELECT ID FROM TT2
16
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
1
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
2
Sorry, I didnt see you also fixed the original query and added theAS
statements. I thought you only showed the shorthand. removed my downvote.
– ToBe
May 22 '15 at 15:46
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
add a comment |
up vote
433
down vote
accepted
Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever
), which can the be used to refer to it in the rest of the outer query.
SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T
In your case, of course, the entire query could be replaced with:
SELECT ID FROM TT2
16
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
1
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
2
Sorry, I didnt see you also fixed the original query and added theAS
statements. I thought you only showed the shorthand. removed my downvote.
– ToBe
May 22 '15 at 15:46
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
add a comment |
up vote
433
down vote
accepted
up vote
433
down vote
accepted
Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever
), which can the be used to refer to it in the rest of the outer query.
SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T
In your case, of course, the entire query could be replaced with:
SELECT ID FROM TT2
Every derived table (AKA sub-query) must indeed have an alias. I.e. each query in brackets must be given an alias (AS whatever
), which can the be used to refer to it in the rest of the outer query.
SELECT ID FROM (
SELECT ID, msisdn FROM (
SELECT * FROM TT2
) AS T
) AS T
In your case, of course, the entire query could be replaced with:
SELECT ID FROM TT2
edited Jun 10 '16 at 9:35
answered Dec 11 '09 at 15:31
Paul
8,881113444
8,881113444
16
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
1
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
2
Sorry, I didnt see you also fixed the original query and added theAS
statements. I thought you only showed the shorthand. removed my downvote.
– ToBe
May 22 '15 at 15:46
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
add a comment |
16
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
1
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
2
Sorry, I didnt see you also fixed the original query and added theAS
statements. I thought you only showed the shorthand. removed my downvote.
– ToBe
May 22 '15 at 15:46
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
16
16
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
Correct answer for the shown sampe code but not the solution for most users looking this question up.
– ToBe
Sep 4 '14 at 7:48
1
1
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
@ToBe I'm curious what you meant by that? The answer holds true in any query, that if you have a derived table in your from clause you need to give it an alias.
– AdamMc331
May 4 '15 at 13:11
2
2
Sorry, I didnt see you also fixed the original query and added the
AS
statements. I thought you only showed the shorthand. removed my downvote.– ToBe
May 22 '15 at 15:46
Sorry, I didnt see you also fixed the original query and added the
AS
statements. I thought you only showed the shorthand. removed my downvote.– ToBe
May 22 '15 at 15:46
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
I am thinking same with @ToBe. The answer is this: "Here, derived table means 'sub-query used in the FROM clause'. In the questioners's case; they are the sub-queries inside the parenthesis. If you do not indicate the alias by using keyword 'as' for those queries, the dbms query engine cannot determine which query is what without their names (or aliases) so, you must give unique names (aliases) for all of your sub-queries to make dbms query engine make it's work properly."
– Bahadir Tasdemir
Apr 25 '16 at 7:33
add a comment |
up vote
67
down vote
I think it's asking you to do this:
SELECT ID
FROM (SELECT ID,
msisdn
FROM (SELECT * FROM TT2) as myalias
) as anotheralias;
But why would you write this query in the first place?
11
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
9
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
add a comment |
up vote
67
down vote
I think it's asking you to do this:
SELECT ID
FROM (SELECT ID,
msisdn
FROM (SELECT * FROM TT2) as myalias
) as anotheralias;
But why would you write this query in the first place?
11
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
9
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
add a comment |
up vote
67
down vote
up vote
67
down vote
I think it's asking you to do this:
SELECT ID
FROM (SELECT ID,
msisdn
FROM (SELECT * FROM TT2) as myalias
) as anotheralias;
But why would you write this query in the first place?
I think it's asking you to do this:
SELECT ID
FROM (SELECT ID,
msisdn
FROM (SELECT * FROM TT2) as myalias
) as anotheralias;
But why would you write this query in the first place?
edited May 5 '14 at 4:33
Amarnath Balasubramanian
6,76762750
6,76762750
answered Dec 11 '09 at 15:28
hometoast
9,29343356
9,29343356
11
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
9
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
add a comment |
11
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
9
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
11
11
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
the actual query is too long.. i have shortened it enough that people here have less time understanding it. the error on the short and long query was same.
– silverkid
Dec 11 '09 at 15:32
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
I understand now. I was also thinking it might have been generated by some code. It should still simplify as Paul and DMKing suggested.
– hometoast
Dec 11 '09 at 15:36
9
9
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
Wow, is this really the unaccepted second answer? To anyone with the problem this is the answer, MySQL requires you to label the "sub query" instead of just leaving it like many other implementations.
– Daniel B. Chapman
Aug 30 '12 at 16:00
add a comment |
up vote
12
down vote
Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT
).
Imagine a table called purchases
that records purchases made by customers
at stores
, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
GROUP BY customer_id HAVING 1 < SUM(1);
..will break with the error Every derived table must have its own alias
. To fix:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
GROUP BY customer_id HAVING 1 < SUM(1);
( Note the AS custom
alias).
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
add a comment |
up vote
12
down vote
Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT
).
Imagine a table called purchases
that records purchases made by customers
at stores
, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
GROUP BY customer_id HAVING 1 < SUM(1);
..will break with the error Every derived table must have its own alias
. To fix:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
GROUP BY customer_id HAVING 1 < SUM(1);
( Note the AS custom
alias).
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
add a comment |
up vote
12
down vote
up vote
12
down vote
Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT
).
Imagine a table called purchases
that records purchases made by customers
at stores
, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
GROUP BY customer_id HAVING 1 < SUM(1);
..will break with the error Every derived table must have its own alias
. To fix:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
GROUP BY customer_id HAVING 1 < SUM(1);
( Note the AS custom
alias).
Here's a different example that can't be rewritten without aliases ( can't GROUP BY DISTINCT
).
Imagine a table called purchases
that records purchases made by customers
at stores
, i.e. it's a many to many table and the software needs to know which customers have made purchases at more than one store:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases)
GROUP BY customer_id HAVING 1 < SUM(1);
..will break with the error Every derived table must have its own alias
. To fix:
SELECT DISTINCT customer_id, SUM(1)
FROM ( SELECT DISTINCT customer_id, store_id FROM purchases) AS custom
GROUP BY customer_id HAVING 1 < SUM(1);
( Note the AS custom
alias).
answered Jul 5 '13 at 23:47
Neil Stockbridge
35733
35733
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
add a comment |
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
What is the effect of SUM(1) on the subquery?
– xssChauhan
Jun 25 '17 at 9:47
add a comment |
protected by ken2k Jan 4 '17 at 16:13
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
13
Couldn't you just simplify this as "select ID from TT2"?
– DMKing
Dec 11 '09 at 15:29
2
I got this error recently because I had an extra
)
in a query with a lot ofUNION ALL
s.– mpen
Feb 17 '12 at 0:52
10
Seeing as how this is the #1 Google search... The accepted answer doesn't really answer the error 'Every derived table must have its own alias'. Look below for more info.
– Daniel B. Chapman
Aug 30 '12 at 16:01