Can you execute my query on your created table to check if it works?

|
MasterZiv 2020-06-19 09:17:33
and this one?
MasterZiv 2020-06-19 09:17:33
this is right query
MasterZiv 2020-06-19 09:17:33
mysql_en-3935.jpg

MasterZiv 2020-06-19 09:17:33
select dep,MAX(salary) as max_salary FROM test GROUP BY dep
MasterZiv 2020-06-19 09:17:33
5 min, i will generate example
MasterZiv 2020-06-19 09:17:33
I mean this one?
MasterZiv 2020-06-19 09:17:33
was that original table?
MasterZiv 2020-06-19 09:17:33
this just example
MasterZiv 2020-06-19 09:17:33
1,20
2,30
MasterZiv 2020-06-19 09:17:33
ok want to clarify whether this is the output your query generated?
MasterZiv 2020-06-19 09:17:33
GROUP will NOT sum
MasterZiv 2020-06-19 09:17:33
I’m not summing salaries. I want maximum salary within each department.
MasterZiv 2020-06-19 09:17:33
ok. an online platform would have worked great. Anyway thanx
MasterZiv 2020-06-19 09:17:33
i use Dbeaver it has visual interface
MasterZiv 2020-06-19 09:17:33
@alexwid BTW, is there any easier platfom to create table. I mean those INSERT and CREATE statements really do take some time when creating a table larger no. of columns?
MasterZiv 2020-06-19 09:17:33
Select salary from test group by dep having max(salary);
SQL Error [1055] [42000]: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘test.salary’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
MasterZiv 2020-06-19 09:17:33
this one.
MasterZiv 2020-06-19 09:17:33
can you execute my query on your created table to check if it works?
MasterZiv 2020-06-19 09:17:33
sorry can’t answer about HAVING
MasterZiv 2020-06-19 09:17:33
Select salary from table_name group by dept having salary = max(salary); this?
MasterZiv 2020-06-19 09:17:33
maybe if group by dept but it doent work in my configuration
(this is incompatible with sql_mode=only_full_group_by)
Master Yoda 2020-06-19 11:03:09
Ajay 2020-06-19 07:25:33
ok sorry, i wrote it wrong, is this right then Select salary from table_name group by dept having salary = max(salary);

Select emp_id, E_name, department, max(salary) from tbl group by department having max(salary)

Master Yoda 2020-06-19 11:08:34
Ajay 2020-06-19 07:25:33
ok sorry, i wrote it wrong, is this right then Select salary from table_name group by dept having salary = max(salary);

This would not work because having clause expects aggregate function or so not just column

Master Yoda 2020-06-19 11:09:46
Master Yoda 2020-06-19 11:08:34
This would not work because having clause expects aggregate function or so not just column

Like count, avg or so

Ajay 2020-06-19 11:10:34
Master Yoda 2020-06-19 11:09:46
Like count, avg or so

Ok. So, we shouldn’t use aggregate functions inside where but should write inside the having clause. Right?

Master Yoda 2020-06-19 11:11:01
Ajay 2020-06-19 11:10:34
Ok. So, we shouldn’t use aggregate functions inside where but should write inside the having clause. Right?

Exactly

Master Yoda 2020-06-19 11:11:18
But having will behave like where without group by
Ajay 2020-06-19 11:11:35
ok
Ajay 2020-06-19 11:11:40
Another question
Ajay 2020-06-19 11:11:42
t1 – id(pk),name, age

t2 – id(fk), marks

marks of a person whose age is maximum

select t2.marks

from t1 inner join t2 on t1.id = t2.id

where t1.age = (select max(age) from t1);

Ajay 2020-06-19 11:12:12
t1 is table1 and t2 is table2 and pk is primary key and fk – foreign key
Ajay 2020-06-19 11:12:33
Ajay 2020-06-19 11:11:42
t1 – id(pk),name, age

t2 – id(fk), marks

marks of a person whose age is maximum

select t2.marks

from t1 inner join t2 on t1.id = t2.id

where t1.age = (select max(age) from t1);

Cpt. Jack Sparrow Is that correct?

Master Yoda 2020-06-19 11:14:38
Ajay 2020-06-19 11:11:42
t1 – id(pk),name, age

t2 – id(fk), marks

marks of a person whose age is maximum

select t2.marks

from t1 inner join t2 on t1.id = t2.id

where t1.age = (select max(age) from t1);

Didn’t you run this query?

Master Yoda 2020-06-19 11:14:47
If you , you would have know!
Ajay 2020-06-19 11:14:59
Ajay 2020-06-19 11:11:42
t1 – id(pk),name, age

t2 – id(fk), marks

marks of a person whose age is maximum

select t2.marks

from t1 inner join t2 on t1.id = t2.id

where t1.age = (select max(age) from t1);

I see a slight problem here, that this will give me the marks of the student whose age is maximum in the t1 table and not the inner join of t1 and t2 tables.

Master Yoda 2020-06-19 11:16:11
Ajay 2020-06-19 11:14:59
I see a slight problem here, that this will give me the marks of the student whose age is maximum in the t1 table and not the inner join of t1 and t2 tables.

Not inner join? but you are using inner so engine will parse it as inner join

Ajay 2020-06-19 11:27:32
Master Yoda 2020-06-19 11:14:38
Didn’t you run this query?

Yeah, I ran the query and it is giving right result but not in the sense that I want it to.

Ajay 2020-06-19 11:28:04
create table t1(id integer primary key, name varchar(40), age integer);
create table t2(id integer references t1(id), marks integer);
insert into t1 values(1, “a”, 15);
insert into t1 values(2, “b”, 16);
insert into t1 values(3, “c”, 17);
insert into t1 values(4, “d”, 18);

insert into t2 values(1, 54);
insert into t2 values(2, 54);
insert into t2 values(3, 23);
— insert into t2 values(4, 23); this is commented
insert into t2 values(5, 54);

— select * from t1;
— select * from t2;

select t2.marks from t1 inner join t2 on t1.id = t2.id where t1.age = (select max(age) from t1);

Ajay 2020-06-19 11:29:54
The problem is that max(age) from t1 returns 18(id is 4) and the inner join of t1 and t2 does not contain the row with id=4 and that’s why it isn’t giving me any output.
Ajay 2020-06-19 11:30:30
What I want is that after they are joined, then find the age marks of a student with maximum age.
MasterZiv 2020-06-19 11:39:43
Ajay 2020-06-19 11:11:40
Another question

you should not learn SQL in a chat! read books!

Ajay 2020-06-19 11:40:25
MasterZiv 2020-06-19 11:39:43
you should not learn SQL in a chat! read books!

But, above one is a doubt

MasterZiv 2020-06-19 11:40:50
Ajay 2020-06-19 11:11:42
t1 – id(pk),name, age

t2 – id(fk), marks

marks of a person whose age is maximum

select t2.marks

from t1 inner join t2 on t1.id = t2.id

where t1.age = (select max(age) from t1);

quite correct, yes

Master Yoda 2020-06-19 11:52:37
Ajay 2020-06-19 11:30:30
What I want is that after they are joined, then find the age marks of a student with maximum age.

Use left join

Master Yoda 2020-06-19 11:52:45
You will get the desired output
Master Yoda 2020-06-19 11:52:59
Not inner
brijeshJoshiii 2020-06-19 12:05:06
Ajay 2020-06-19 11:28:04
create table t1(id integer primary key, name varchar(40), age integer);
create table t2(id integer references t1(id), marks integer);
insert into t1 values(1, “a”, 15);
insert into t1 values(2, “b”, 16);
insert into t1 values(3, “c”, 17);
insert into t1 values(4, “d”, 18);

insert into t2 values(1, 54);
insert into t2 values(2, 54);
insert into t2 values(3, 23);
— insert into t2 values(4, 23); this is commented
insert into t2 values(5, 54);

— select * from t1;
— select * from t2;

select t2.marks from t1 inner join t2 on t1.id = t2.id where t1.age = (select max(age) from t1);

in where clause instead of selecting max(age) from t1 select max(age) from join of t1 and t2

Ajay 2020-06-19 12:05:11
Master Yoda 2020-06-19 11:52:37
Use left join

I’ll get NULL because now id=4 is not present in t2 and the maximum age from t1 is of the person with id=4.

Ajay 2020-06-19 12:05:27
brijeshJoshiii 2020-06-19 12:05:06
in where clause instead of selecting max(age) from t1 select max(age) from join of t1 and t2

yes. that should work

Ajay 2020-06-19 12:06:06
brijeshJoshiii 2020-06-19 12:05:06
in where clause instead of selecting max(age) from t1 select max(age) from join of t1 and t2

with ON t1.id = t2.id. Isn’t it?

|