I have got 2 tables, Security and SecurityTransactions.
Security:
create table security(SecurityId int, SecurityName varchar(50));
insert into security values(1,'apple');
insert into security values(2,'google');
insert into security values(3,'ibm');
SecurityTable:
create table SecurityTransactions(SecurityId int, Buy_sell boolean, Quantity int);
insert into securitytransactions values ( 1 , false, 100 );
insert into securitytransactions values ( 1 , true, 20 );
insert into securitytransactions values ( 1 , false, 50 );
insert into securitytransactions values ( 2 , false, 120 );
I want to find out the security name and it's no of appearance in SecurityTransactions.
The answer is below:
SecurityName | Appearance apple | 3 google | 1
I wrote the below sql query :
select S.SecurityName, count(t.securityID) as Appearance
from security S inner join securitytransactions t on S.SecurityId = t.SecurityId
group by t.SecurityId, S.SecurityName;
this query gave me the desired result, but it was still rejected by a person saying group by should have been s.securityName. why is it so ?
EDIT :
Which one do you this is correct and why ?
a. group by t.SecurityId, S.securityName
b. group by t.SecurityId
c. group by S.securityName
GROUP BY. MySQL's old default behavior is lenient and permits you to include columns inSELECTnot present inGROUP BY, but most other RDBMS would reject that query. Additionally, the behavior is off by default in MySQL 5.7 so it is not recommended to rely on it, and a bad habit to have when you switch to another RDMBS. stackoverflow.com/questions/38907729/…