Shotgun with buckshot. Generally anyone close enough to hear the unmistakable sound of you racking it will go running.
I did some googling on different shells good for apartments. Personally, I have never lived in an apartment but the idea is that you don't want something penetrating through to other apartments. Seems like many people recommend turkey shot for apartments. So maybe others here some some input on that too or experience.
Firearms meant for self defense shall not be unloaded. You were born with a mouth and I assume you are able to speak. Give a verbal warning if needed. Racking your shotgun just shows your lack of training and common sense.
Any round that is capable of penetrating a human body deep enough for the purpose of lethal self/home defense is able to penetrate through 3-4 apartment walls (6-8 dry walls). Bird shot is often recommended as it penetrates significantly less. There is a reason why it is called bird shot. The standard load for a 12GA shotgun is buckshot in sizes between #4 buck - 00 buck.
The key to home defense with a firearm is to identify safe shooting vectors, i.e., your defensive positions, your angle at the threat and your backstop. Simply speaking, don't position yourself such that a miss can land in your neighbor's apartment or house. An exterior wall is a good backstop in most cases.
To the OP, since this is your first firearm, consider the most important part, training. Do you have access to a range for pistol, shotgun or rifle shooting? If so, check which range rents firearms and try them out. Get some instructions on firearm safety and on the efficiency of different firearms, calibers and rounds.
In general, a semi-auto shotgun will be the most efficient, but will also be the heaviest and will have some recoil. A pump-action shotgun will be lighter, but have even more recoil. An AR-15 will be slightly less efficient, but lighter and have less recoil. A 9mm or .45ACP full-size handgun will be lighter and the least efficient with the least amount of recoil.