I’m new to HQL. I need to fetch all the records from table A based on the following 2 condition using HQL/SQL query: Person ID which satisfies both these conditions “(Music < 100) and (Dance != Normal)” (in Table B) and whose Place and Country is A and AAA (in Table C).
Tables below:
[
[
[
How can I fetch these records based on this three conditions. Could someone help me.
The output should be
Record having ID as 100 in Table A since it has place and value as ‘A’ and ‘AA’. And it also has both Music and Dance skills with Music value greater than 100 and Dance value is not like ‘Normal’
Advertisement
Answer
select * from a inner join b as music on a.id = music.person_id and music.skills = 'Music' inner join b as dance on a.id = dance.person_id and dance.skills = 'Dance' inner join c on a.id = c.id where c.place = 'A' and c.country = 'AAA' and music.score < '100' and dance.score <> 'Normal'
You will have a problem attempting to use less than on the column “score” which is text and not numeric, see test cases below
CREATE TABLE mytable( Value VARCHAR(6) NOT NULL PRIMARY KEY ); INSERT INTO mytable(Value) VALUES ('100'); INSERT INTO mytable(Value) VALUES ('a'); INSERT INTO mytable(Value) VALUES ('aa'); INSERT INTO mytable(Value) VALUES ('bbb'); INSERT INTO mytable(Value) VALUES ('cccc'); INSERT INTO mytable(Value) VALUES ('99'); INSERT INTO mytable(Value) VALUES ('9'); INSERT INTO mytable(Value) VALUES ('1'); INSERT INTO mytable(Value) VALUES ('19');
select * from mytable where value < '100'
| value | | :---- | | 1 |
select * from mytable where value > '100'
| value | | :---- | | a | | aa | | bbb | | cccc | | 99 | | 9 | | 19 |
select * from mytable where cast(value as integer) > 100
ERROR: invalid input syntax for integer: "a"
db<>fiddle here