Using a nested query to get details of two tables - mysql

TABLE 1 TABLE 2
id name mob id course mark
1 joe 0000 1 English 77
2 john 0000 2 maths 89
I need to show the name of the person from table 1 who has the MAX(grade) in table 2 using a nested query.
SELECT t1.name
FROM t1
WHERE t1.id = t2.id = (
SELECT id
FROM t2
WHERE mark =
(
SELECT MAX(mark)
FROM t2
)
);

Well, this satisfies the brief ;-):
SELECT a.*
FROM table_a a
JOIN (SELECT * FROM table_b) b
ON b.id = a.id
ORDER
BY mark DESC
LIMIT 1;

Related

How to return duplicates within a table that share the same value within another column in MySQL?

How would I return the ProductNumbers where the Number is duplicated when it has the same year ?
This is all within the same table.
in this example below, I would expect ProductNumber 123 and 456 to be returned.
Explain reasoning if possible, thank you!
ProductNumber Numb Year
123 45 1
456 45 1
789 45 2
109 54 2
Here's one option using exists:
select *
from yourtable t
where exists (
select 1
from yourtable t2
where t.productnumber != t2.productnumber
and t.numb = t2.numb
and t.year = t2.year
)
Using exists, we check to see if there are other records in the same table whose productnumber is different, but have the same numb and year values.
You can use EXISTS() :
SELECT *
FROM Table T1
WHERE EXISTS
(
SELECT 1
FROM
(SELECT Numb ,Year
FROM Table
GROUP BY Numb
,Year
HAVING COUNT(1)>1
) T2
WHERE T1.Numb = T2.Tumb
AND T1.Year = T2.Year
)
You can also use INNER JOIN
SELECT t1.ProductNumber
FROM Products t1
INNER JOIN Products as t2
ON t1.ProductNumber != t2.ProductNumber
AND t1.Numb = t2.Numb
AND t1.Year = t2.Year

how to exclude first and last row if group it on particular id

I have sample table with data like this
id uniqueid values
1 6 0
2 6 1
3 6 2
4 6 0
5 6 1
I want result like this
id uniqueid values
2 6 1
3 6 2
4 6 0
I tried like this
select id,uniqueid,values
FROM t1
WHERE
id not in(SELECT concat(MAX(message_id_pk),',',min(message_id_pk)) FROM t1
where uniqueid=6)
and `uniqueid`=6
GROUP BY uniqueid
but its not working
You can achieve the desired results by doing self join, Inner query will get the the max and min ids for per group and outer query will filter out the results by using minid and maxid
select a.*
from demo a
join (
select `uniqueid`,min(id) minid, max(id) maxid
from demo
where uniqueid=6
group by `uniqueid`
) b using(`uniqueid`)
where a.id > b.minid and a.id < b.maxid /* a.id <> b.minid and a.id <> b.maxid */
Demo
Also you can do it by using 2 sub-queries with EXISTS to exclude the min and max id of each uniqueid.
Query
select `id`, `uniqueid`, `values`
from `your_table_name` t1
where exists (
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` > t1.`id`
)
and exists(
select 1 from `your_table_name` t2
where t2.`uniqueid` = t1.`uniqueid`
and t2.`id` < t1.`id`
);
Here is a sql fiddle demo
Try this -
SELECT id, uniqueid, values
FROM YOUR_TABLE
WHERE id NOT IN (MIN(id), MAX(id));

How to equiry data using sql when facing condition?

type cost
A 10
A 11
A 12
B 10
B 10
I have this small sample table. I want to select data where the cost of the same type is different.So the expected outcome should be:
type cost
A 10
A 11
A 12
The cost for A is different so I need to select these "A" out.
So what is the "select" sentence?
Thanks for the replies. Actually my table is little more complex like this
type cost people
A 10 jack
A 11 frank
A 12 lucy
B 10 amy
B 10 tom
I need to select the data meet one of the requirements below:
Same type with different cost
Same type with people "amy"
So the outcome should be like :
type cost people
A 10 jack
A 11 frank
A 12 lucy
B 10 amy
B 10 tom
Select all of type A because the cost is different
Select all of type B because the people has "amy"
I have firgure out how to select for amy like this:
select type, cost, people
from table
where type in
(select type from table where people = 'amy')
I don't know how to combine these conditions.
SQL Fiddle
You can use EXISTS to look for another row with same type but other cost:
select t1.type, t1.cost
from tablename t1
where exists (select * from tablename t2
where t2.type = t1.type
and t2.cost <> t1.cost)
Or have a sub-query that returns type values having different costs, and join with that result:
select t1.type, t1.cost
from tablename t1
join (select type
from tablename
group by type
having max(cost) <> min(cost)) t2
on t1.type = t2.type
Another way is:
select
t.type, t.cost
from t
left join t t1 on t.type = t1.type
and (t.cost <> t1.cost or t1.people = 'amy')
where
not t1.cost is null
group by
t.type, t.cost;
[SQL Fiddle Demo]
Or also:
select *
from t
where exists (
select 1
from t t1
where t1.type = t.type
group by t1.type
having count(distinct t1.cost) > 1
-- below code added your new criteria
union all
select 1
from t t2
where t2.people = 'amy'
);
[SQL Fiddle Demo]

Fetch data from Table 1 column's count in Table 2

Table 1 :
ID City State
1 NewYork NY
2 Oklahama OK
3 california CA
4 new jersey NJ
5 Las Vegas LA
Table 2 :
ID City
1 NewYork
2 NewYork
3 NewYork
4 Oklahama
5 Oklahama
NewYork is 3 times and Oklahama is 2 times in table 2.
So I want to fetch City List from Table 1 which Cities are used less then 5 times in Table 2
So what will exact query in Mysql?
I am using below code :
select *
from Table1
where Table1.city in
(select Table2 .city,count(*)
from Table2
having count(*) < 5
group by Table2.city )
You can use a in clause with a subselect
select * from table1
where city in (select city from table2 having count(*) < 5 group by city)
In your code you have a space between the Table2 and .city
select *
from Table1 where Table1.city in (select
Table2 .city,count(*) from Table2 having count(*) < 5 group by Table2.city )
must be
select *
from Table1
where Table1.city in (select Table2.city
from Table2
group by Table2.city
having count(*) < 5 )
no space between tablename and column ..
you can seee http://sqlfiddle.com/#!9/556cb6/10
How about
select Table1.city
from Table1 inner join Table2 on Table1.city = Table2.city
group by Table1.city
having count(Table2.city) < 5
SQLFiddle

Joining two tables on interval basis

In SQL, suppose that I have table A
ID
--
1
3
5
and table B
ID2
---
1
2
3
4
5
6
To get the result similar to:
ID | ID2
----------
1 | 1
1 | 2
3 | 3
3 | 4
5 | 5
5 | 6
For an explanation, an element in column ID2 will be mapped to the highest value in the column ID that is less than or equal to the said element in ID2.
For example, 4 in column ID2 is mapped to 3 from column ID, because 3 is the largest value in column ID which is less than or equal to 4.
Is it possible at all to do this in sql?
What I would do is start by joining the two tables on the condition that the id in the first table is less than or equal to that in the second table, like this:
SELECT t1.id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id;
Once you have that, you can select the maximum id from the first table, and group by the id from the second table to get the largest pairs:
SELECT MAX(t1.id) AS id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id
GROUP BY t2.id;
SQL Fiddle seems to be down but I will update with a link as soon as I can.
SELECT MAX(A.ID) ID, B.ID2
FROM A
INNER JOIN B ON B.ID2 >= A.ID
GROUP BY B.ID2
If you only need the matching ID column:
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
If you need more columns:
select *
from a
join
(
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
) as b
on a.Id = b.a_Id