I'm pretty new to SQL and Postgres. I have encountered a case where I think I should use JOIN, but it looks more complicated actually and I don't know what query to use.
So i have 3 tables: students, schools and users.
users:
Column | Type | Collation | Nullable | Default
---------------------+------------------------+-----------+----------+-----------------------------------
id | bigint | | not null | nextval('users_id_seq'::regclass)
public_id | uuid | | not null | uuid_generate_v4()
first_name | character varying(50) | | not null |
last_name | character varying(50) | | not null |
username | character varying(50) | | not null |
email | character varying(255) | | |
password | character varying(255) | | not null |
role | character varying(20) | | not null |
username_repetition | integer | | | 1
students:
Column | Type | Collation | Nullable | Default
-----------+--------+-----------+----------+--------------------------------------
id | bigint | | not null | nextval('students_id_seq'::regclass)
class_id | bigint | | not null |
school_id | bigint | | not null |
user_id | bigint | | not null |
schools:
Column | Type | Collation | Nullable | Default
--------------+------------------------+-----------+----------+-------------------------------------
id | bigint | | not null | nextval('schools_id_seq'::regclass)
name | character varying(100) | | not null |
short_name | character varying(50) | | not null |
phone_number | character varying(30) | | not null |
postal_code | character varying(30) | | not null |
adress | character varying(255) | | not null |
city | character varying(50) | | not null |
county | character varying(50) | | not null |
I want to use SELECT query to get back username_repetition of a user that is a student, only if the student is assigned to a specific school. How can I achieve this? Please, let me know if you need further information about this. Thank you!