Interpreting a MySQL nested SELECT statement - mysql

I am not a MySQL expert by any means, but after reading the documentation for the SELECT statement, I did not find an answer to my problem.
I have this statement:
SELECT COUNT(*)=x.c FROM someTable,
(SELECT COUNT(*) c
FROM someTable
WHERE firstId <= secondId) x;
And I'm trying to figure out what the x.c means in the context of the query? Specifically, what is with the x that seems to be hanging out there?
I interpret the nested SELECT as SELECT COUNT(*) as c, making an alias for the row count as c, is that what the x is as well? What would it be an alias for?
Thanks!

The x is a table alias - a name for the nested SELECT statement in parentheses.
COUNT(*)=x.c
Is a boolean condition that the total row count of someTable be equal to the row count of someTable where firstId <= secondId
x.c is the column name for the count returned by the subquery.

They name the subquery "x" and in the subquery they name the count(*) "c". So "x.c" means the count returned by the subquery.

The x is an alias to the subquery - you will note that there is an x just after the subquery, denoting the alias name for it.

x is an alias for the table (SELECT COUNT(*) c FROM someTable WHERE firstId <= secondId).

MySQL requires that table subqueries have a unique alias. You'll notice there's an x at the end of the subquery, which makes the sub queries results appear as coming from table x.
In this case, x.c in the outer query means to refer to field c (the count result) in the aliased table x, which is the subquery.

X is an alias for joined someTable and the select you joined someTable with. I guess so :D
I guessed wrong, guys over me are right :P

Related

Avoiding redundant expressions in SELECT query

Is there any way to avoid repeating column expressions in the SELECT query? I want to divide the sum and count of a column but would like to use the assigned name instead of repeating SUM(value)/COUNT(value) or using a sub query. Is this possible? If so, does that speed up the query by not repeating the calculation of the sum and count or does mysql remember already calculated expressions?
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, type_sum/type_count
FROM (...) AS datalist
GROUP BY datalist.type
throws: #1054 - Unknown column 'type_sum' in field list
Unless you put it in outer query, this is the only way.
SELECT datalist.type, SUM(value) AS type_sum, COUNT(value) AS type_count, SUM(value)/COUNT(value)
FROM (...) AS datalist
GROUP BY datalist.type
One workaround would be to use a alias table with pre-defined calculations and then later call it from outer table such as:
select d.type_sum/d.type_count as dividedValue from (SELECT datalist.type, SUM(value)
AS type_sum, COUNT(value) AS type_count
FROM (...) )AS d
GROUP BY d.type

how can i select double field where different clause

can I use a query like this:
SELECT a AS x, a AS y
FROM table
WHERE x='1' and y='2'
how can I use such as that query...
You have to take main query in temp as follow:
SELECT temp.* FROM (SELECT a AS x, a AS y
FROM table) as temp WHERE temp.x = '1' AND temp.y = '2'
In general, you cannot use column aliases defined in the SELECT in the WHERE clause. This is true in all databases, not only MySQL. One method is to use a subquery (or CTE in databases that support them).
MySQL also extends the HAVING clause. This allows you to do:
SELECT a AS x, a AS y
FROM table
HAVING x ='1' and y = '2';
I assume that this question is made up, because the query can never return any rows.

What is this SQL query syntax mean?

SELECT b.*,
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1
From this sql syntax, what is "," which after "*" mean?
and anyone can explain this query to me or tell me where I can start?
It means all the columns from table Booking, and to the far right (the last column per row) bring in the user_name column from table user relating on the user.user_id matching the booking.booking_inspector. Such that the Booking.confirm is 1, and Booking_inspector is filled in with a parameter passed.
So it limits the rows of output to confirm is 1 and Booking_Inspector is the parameter passed (or bound, etc) depending on the language calling it.
Select * means all columns. So all columns from the one table, and just one column from the other
In this case, (select a.USER_NAME from A.db.USER a where a.USER_ID=b.Booking_Inspector) is the subquery which will return column a.USER_NAME. So this query is selecting everything from b (b.*) and the column a.USER_NAME from the subquery. So like you put comma between column names in select query, it is same.
select all columns from b and one more column from that subquery as USER_NAME.
( select a.USER_NAME
from A.db.USER a
where a.USER_ID=b.Booking_Inspector
) as USER_NAME
That whole thing above as 1 column
SELECT b.*, [USER_NAME]
FROM A.dbo.Booking b
where b.Booking_Inspector=? and b.confirm=1

Is it possible to compare a COUNT() with a single result of a SELECT in HAVING clause?

I have a question on HAVING statement in SQL.
Is a query like this possible?
SELECT COUNT(T.IDtif) AS NumeroTifosi
FROM Tifosi T, Partita P
WHERE T.IDtif=P.Tifoso AND P.Partita=P.Idpar
HAVING COUNT(P.Idpar) = ( SELECT COUNT(Idpar) FROM Partita
WHERE Data BETWEEN “2002101” AND ”20021231”)
I don't understand if it is possible to compare an aggregation function, in a HAVING statement, with a subquery that returns a single value.
That's what the parentheses do. You can compare a single result of a SELECT by turning telling SQL "it's ok, this has one value".
It could be:
SELECT ColA = (SELECT ColB from TableB Where Id = 1)
FROM TableA
or
SELECT *
FROM TableA
WHERE ColA = (SELECT ColB from TableB Where Id = 1)
or even with HAVING as you describe.
Yes. This query if perfectly valid. Are you getting an error when you try to run it?
What you are doing is running a subquery once to return a count. Then for each group, you are checking if the number of rows in that group is equal to the number returned by the subquery.

Subquery returning multiple columns

I am writing a select subquery that returns a COUNT. However, I need to use a HAVING clause in the subquery and as a result need to specify that column in my subquery SELECT. So the result is that I have 2 columns: COUNT and column_for_having_clause.
Is there any way that i can tell SQL to take just the first column. I get an error saying
"Operand should contain 1 column(s)". I figure this must be a common problem, does anyone know a way around it?
Also, is there a way you can execute a HAVING clause without specifying the column in your SELECT query?
Thanks!
You could do
SELECT blah FROM foo WHERE bar = (SELECT cnt FROM (SELECT cnt, column_for_having_clause FROM table .... HAVING ... LIMIT 1))
Quite ugly however.
Sure, you can specify virtually any expression you want in the HAVING clause. It doesn't have to be a column from the select-list.
SELECT ...
FROM foo
WHERE x = (SELECT COUNT(*) FROM bar
GROUP BY y HAVING y = MAX(y));
But you can also, if you're comparing the count to something in the outer query, compare tuples.
SELECT ...
FROM foo
WHERE (x, 1) = (SELECT COUNT(*), y = MAX(y) AS is_max FROM bar
GROUP BY y HAVING is_max = 1);