I have a table, A and B which are shown below,
Table A:
- id
- idB
- name
- faculty
B:
- id
- name
Table B has 2 records as below.
SELECT *
FROM B;
1, 1, 'First'
2, 2, 'Second'
Table A has 8 records as below.
SELECT *
FROM A;
1, 1, A, IT
2, 1, B, IT
3, 1, C, IT
4, 1, D, Medicine
5, 1, E, Medicine
6, 1, F, Business
7, 1, G, Business
8, 1, H, IT
9, 2, A, Medicine
10, 2, B, Medicine
11, 2, C, Medicine
12, 2, D, Medicine
13, 2, E, Medicine
14, 2, F, Medicine
15, 2, G, Business
16, 2, H, Medicine
My question is:
How can I select data from table B where faculty should be IT and if there are multiple it should get with max ID. AND if there is no any IT, it should be get business?
My select view should be look like this below:
A and B records.
- 8, 1, H, IT, First
- 15, 2, G, Business, Second
Please could you advise and help in which way we can retrieve these data?