SQLite how to get same groups by id? - sql

I have a question about query. Here is my table:
I get id by onClick method and I want result like this. For example if id is 2 get all rows who has same group.
Result:

You can use a subquery:
select t.*
from mytable t
where t.group = (select t2.group from mytable t2 where t2.id = 2);
Note that group is a really bad name for a column, because it is a SQL keyword. It is fine for illustration.

Related

Finding MAX id from table with conditions

There is a table with columns: id, name, age.
I need to find the max value of id and display name and id, where name starts with just A.
I used the query below to get the results, but this query doesn't return the unique row.
Its will retrieve from DB all rows where name start with, for example A, and all id's will be listed in the results set. My query HQL is:
SELECT t.name, MAX(t.id)
FROM table t
WHERE t.name LIKE 'A%' AND t.age= :age
GROUP BY t.name
The WHERE clause is filtering your data by the criteria you want, but it's SELECT and GROUPING by the name instead of just the first letter.
So what you need to do is 2 steps. First report the Max(ID) that satisfies your criteria. Next, you'll want to join that ID back to the data to retrieve the name.
So, this is your first into into the wonderful world of subqueries:
SELECT t1.*
FROM
table t1
INNER JOIN
(
SELECT MAX(id) AS max_id
FROM table
WHERE name LIKE 'A%' AND age= :age
) AS toprecord
ON t1.id = toprecord.max_id
I think you want this:
SELECT t.*
FROM table t
WHERE t.name LIKE 'A%' AND t.age= :age
ORDER BY t.id DESC
LIMIT 1;

Select row with max value with condition

I have a table that contains results of some sports competition. Here it is:
And I need to get a table with winner teams. It means, from rows with same MatchIds select entries where Score is maxium for these MatchIds.
Result should look like this:
I have no idea of correct SQL query.
I'm using MSSQL Server 2018. Thank you.
One method is a correlated subquery:
select t.*
from t
where t.score = (select max(t2.score) from t t2 where t2.matchid = t.matchid);

Use value of a field from a table dynamically in the 'where' clause of a HQL query?

Can one filter a table dynamically with a 'where' clause acting on a value of a field from another table under some other conditions such that it is made sure only one row is returned? Can I do something like this?
SELECT COUNT(*) FROM stud t1
WHERE t1.name==SELECT name FROM (
SELECT name, row_number() over (PARTITION BY name) AS rn
FROM stud t2) t3
WHERE t3.rn==1;
Of course, the above query is just a dummy one, but is filtering on where clause like above possible theoretically? If not how could one achieve such a functionality in the cases of more complex queries?
Yes. Query can be made like this:
SELECT COUNT(*) FROM stud t1
WHERE t1.name = (SELECT name
from sometable
where somecondition);
but you need to make sure that the subquery return zero or one row. If your query may return more than one row, you can use IN instead:
SELECT COUNT(*) FROM stud t1
WHERE t1.name IN (SELECT name
from sometable
where somecondition);

How to get the index of some element in select query without fetching all the data

Let we have some select statement:
select id, data from mytable order by data;
Is it possible to get the index of the row with given id in so ordered data set, without fetching all rows up to the id and counting?
I am working with sqlite3 and answers for sqlite dialect of SQL are better, but the answers about general ANSI SQL are ok as well.
Yes. You can use an aggregation query:
select count(*)
from mytable t
where t.data <= (select data from mytable where id = $id);
Note: This assumes that data is unique. If there are duplicates, then you might want the equivalent of a rank instead. This look like:
select 1 + count(*)
from mytable t
where t.data < (select data from mytable where id = $id);

Explain how this SELECT WHERE subquery works?

Here's the query:
SELECT ID, Name, EventTime, State
FROM mytable as mm Where EventTime IN
(Select MAX(EventTime) from mytable mt where mt.id=mm.id)
Here is the fiddle:
http://sqlfiddle.com/#!3/9630c0/5
It comes from this S.O. question:
Select distinct rows whilst grouping by max value
I would like to hear in plain english how it works. I'm missing some fundamental understanding of part of it.
I don't really understand what the aliases are doing in the mt.id=mm.id part. It selects rows where the id is equal to the id?
The mt.id=mm.id part makes it a correlated subquery, hence the subquery is re-evaluated for each ID.
The query, then, selects the most recent event for each ID.
It is basically translated into "Get me the data for each id with maximum EventTime associated with."
You can also rewrite the code as
SELECT t1.ID, t1.Name, t1.EventTime, t1.State FROM mytable as t1
inner join
(
select id,max(EventTime) as EventTime from mytable group by id
) as t2 on t1.id=t2.id and t1.EventTime=t2.EventTime