SQL how to find rows which have highest value of specific column - sql

For example, the table has columns MYINDEX and NAME.
MYINDEX | NAME
=================
1 | BOB
2 | BOB
3 | CHARLES
Ho do I find row with highest MYINDEX for specific NAME? E.g. I want to find ROW-2 for name "BOB".

SELECT Max(MYINDEX) FROM table WHERE NAME = [insertNameHere]
EDIT: to get the whole row:
Select * //never do this really
From Table
Where MYINDEX = (Select Max(MYINDEX) From Table Where Name = [InsertNameHere]

There are several ways to tackle this one. I'm assuming that there may be other columns that you want from the row, otherwise as others have said, simply name, MAX(my_index) ... GROUP BY name will work. Here are a couple of examples:
SELECT
MT.name,
MT.my_index
FROM
(
SELECT
name,
MAX(my_index) AS max_my_index
FROM
My_Table
GROUP BY
name
) SQ
INNER JOIN My_Table MT ON
MT.name = SQ.name AND
MT.my_index = SQ.max_my_index
Another possible solution:
SELECT
MT1.name,
MT1.my_index
FROM
My_Table MT1
WHERE
NOT EXISTS
(
SELECT *
FROM
My_Table MT2
WHERE
MT2.name = MT1.name AND
MT2.my_index > MT1.my_index
)

SELECT MAX(MYINDEX) FROM table
WHERE NAME = 'BOB'
For the whole row, do:
SELECT * FROM table
WHERE NAME = 'BOB'
AND MyIndex = (SELECT Max(MYINDEX) from table WHERE NAME = 'BOB')

If you wanted to see the highest index for name = 'Bob', use:
SELECT MAX(MYINDEX) AS [MaxIndex]
FROM myTable
WHERE Name = 'Bob'

If you want to skip the inner join, you could do:
SELECT * FROM table WHERE NAME = 'BOB' ORDER BY MYINDEX DESC LIMIT 1;

Use
FROM TABLE SELECT MAX(MYINDEX), NAME GROUP BY NAME

Related

Select multiple row where one of their columns is the same

i wanna select a row where in another row with same userid is Active . like :
mytable
userid
userfield
fieldstatus
1
Name
Tom
1
account
Active
2
name
Jerry
2
account
Failed
I want to select tom in name user field where its account is active . I'm writing in python
You can use a subquery with exists:
select t.fieldstatus from mytable t where t.userfield = "name" and exists (select 1 from mytable t1 where t1.userid = t.userid and t1.userfield = "account" and t1.fieldstatus = "Active")
Output
fieldstatus
Tom
You can check the same userid value using exists
select *
from mytable t
where exists (
select * from mytable t2
where t2.userid=t.userid
and t2.userfield='account'
and t2.fieldstatus='Active'
)

Sql Query regarding Join

I have two tables as follows:
Table 1:
Name | Specialisation
Table2:
Name | Slot | Date
I take user input of Name, Slot and Date. I want to display the records of Table1 for that Name such that there exists no record corresponding to the entered (Name, Slot, Date) in Table 2. What will be the SQL query for it?
Thanks in advance.
Supposing the input values were input_name, input_slot, and input_data, and the input_date was a suitable date format, one way to do it would be:
select name, specialisation from table1
where (name = input_name)
and (select name from table2
where (table2.name = input_name) and
(table2.slot = input_slot) and
(table2.date = input_date)) is NULL
Or something like that... :)
You can use a not in
select name
from table1
where name not in (
select name from table2
)
or not exists
select name
from table1
where name not exists (
select name from table2
where table2.name = table1.name
)

Sqlite query - How I can select a predecessor?

I have this table below:
ID name Last
0 Joe Doe
1 Hut Nob
2 Lis Hug
3 Edy mur
I use this query to select an ID:
SELECT name FROM myDatabase WHERE ID = 2
In this case the query returns me the string Lis, now, How I can select the predecessor value?
Simple, the predecessor from 2 is 1, so I need only to do WHERE ID < 2 or WHERE ID = 2 - 1.
But this method have a problem! Lets suppose that I delete that row (ID = 1), the query will return null, because that ID not exists.
So, in this example, how I can select the predecessor from ID 2 and return ID 0? (ID 1 is gone)
You can use subquery to find max ID that is lower than ID that you provided:
SELECT *
FROM mytable m
WHERE m.id = (SELECT MAX(m2.id)
FROM mytable m2
WHERE m2.ID < 2);
SqlFiddleDemo
Get all the smaller IDs, and from those, take only the largest one:
SELECT name
FROM MyTable
WHERE ID < 2
ORDER BY ID DESC
LIMIT 1;

How to GROUP multiple records from two different SQL statements

I have a table called tbl which contains all the data I need. I have many columns in this table, but for purposes of this example I will only list the columns necessary to accomplish this task.
Here's how data stored in the tbl (note uID is char(20) and cID is int)*:
uID cID
1 10
1 11
1 12
2 11
We usually query this table like
SELECT * FROM tbl WHERE uID = "1"
So it returns
uID cID
1 10
1 11
1 12
But I also need to return the row where uID is different but cID do match. Or grab the uID of the second row (which is 2) based on cID and do a select statement like this:
SELECT * FROM tbl WHERE uID in ('1','2')
That query will return what I'm looking for
uID cID
1 10
1 11
1 12
2 11
This table contains a lot of rows and I want to be able to do this programatically for every call where cID matches and uID is different.
Any suggestions?
I think this may be what you want:
SELECT *
FROM tbl
WHERE uID = '1'
UNION ALL
SELECT *
FROM tbl
WHERE uID <> '1' AND
EXISTS (select 1 from tbl tbl2 where tbl2.uId = '1' and tbl2.cID = tbl.cID);
or something like this:
SELECT uID, cID
FROM tbl
WHERE uID IN
(
SELECT uID
FROM tbl
INNER JOIN
(
SELECT cID
FROM tbl
GROUP BY cID
HAVING count(*) > 1
) c ON c.cID = tbl.cID
)

SQL Where clause

My application initially had a query similar to this one:-
SELECT column_name from PERSON
WHERE name in (list);
where list is comma separated list.
But, now the requirement has changed and i have to query the Persons table with name and age given.
I have the nameAgeList.
Initially, i thought a query similar to this would work (Create nameList and ageList from nameAgeList)
SELECT column_name from Person
WHERE name in (nameList)
AND age in (ageList)
But after carefully thinking, this seems to be a wrong query.
Please let me know how should I proceed ahead with this query.
Under Oracle, you can do this:
SELECT * FROM Person
WHERE
(name, age) IN (
('name1', age1),
('name2', age2)
-- Etc...
)
You can have up to 1000 tuples in this list.
One option is to create a temporary table (or if SQL Server, a table variable), place your names and ages in this table, and then simply join to it:
SELECT column_name from Person p
INNER JOIN myTempTable t ON t.Name = p.Name AND t.age = p.age
It's not pretty, and this one only works when you can generate your statement in code:
SELECT column from Person
WHERE 1=1
AND ( ( name = name1 and age = age1 )
OR ( name = name2 and age = age2 )
OR ( name = name3 and age = age3 )
OR ( name = name4 and age = age4 )
OR ( name = name5 and age = age5 )
... et cetera
)
Now, if you could put those lists into tables you could do alot better than this. Is there any way you can get those lists into the database? I assume that you really need some Person table that holds name and age for each individual.