There are many "rules" one might encounter in terms of stocking a fish tank with fish, probably the most common being the "inch of fish per gallon" rule. Others attempt to do a better job by being slightly more complex, such as rules that use the volume or mass of fish or the surface area of the tank rather than the volume of water it contains. In reality, none of these rules address the myriad factors that come into play when stocking your fish tank. Rather than trying to use a simple and easy to remember rule, one is better off considering the factors themselves. In this article I will list some of these factors and explain how they affect your potential stocking list.
Different fish require different water parameters. All fish can adapt to a range of water parameters but the extent of this range is quite variable. Some fish prefer warmer water (discus) while some prefer much lower temperatures (goldfish). Fish should only be kept in the same tank if they have temperature ranges that overlap. However, if Fish A prefers a temperature of 70-75 and Fish B prefers a temperature of 75-80, both fish can technically live in 75 degree water but both fish are living on the fringes of their comfort zones so it might not be best for either species to keep both in the same tank. pH and hardness are equally as important; some fish prefer soft, acidic water while others prefer hard, alkaline water. Unlike temperature, which can be regulated with a heater, pH and hardness can be difficult to adjust and even more difficult to maintain once adjusted. pH and hardness swings can kill fish just as easily as temperature swings so it is always better to stock your tank around the pH and hardness of your water source than to try to adjust these levels for the fish you want to stock. It is relatively easy to raise pH and hardness because materials like dolomite or limestone can be added to a tank but it is substantially more difficult to lower pH and hardness as doing so often destroys your water's buffering capacity which leads to wild pH swings. For this reason, your local pH and hardness levels are probably the first thing you should consider when selecting fish.
This is what the "inch of fish per gallon" rule attempts to cover. Fish excrete waste, which puts a load on your aquarium's filtration (hence the term bioload). This waste comes both as solids and as ammonia released in your water. Small fish generally have smaller bioloads than larger fish, but that's only a general rule. Growing fish also produce a disproportionately high amount of waste and certain types of fish (cichlids, plecos, goldfish, etc.) produce huge amounts of waste even relative to their sizes. A fish's bioload can be counteracted by larger tank volume, increased filtration capacity or the addition of live plants and water changes can be made more frequent in tanks with a heavy bioload. However, there is a point where the filtration you'd require on a tank and the number of water changes you'd need to do in order to keep nitrate levels low would become simply unwieldy. Also, there are a limited number of places on which beneficial bacteria can live in your tank, so a large enough bioload may actually produce ammonia faster than your bacteria can process it into nitrate, which would be very bad.
Fish often have a preferred level of the aquarium in which they swim. Some fish spend most of their time at the top of the tank, others swim about in the middle, others stick to the substrate and others swim where they please. A 20g tank could have ten corydoras catfish and ten lemon tetras and be just fine; replace the tetras with ten more corydoras (which have a similar bioload) and you'd have a very crowded tank floor. Plus, it's nice to have activity at all levels of the tank as it makes the tank look more active.
Many types of fish live in schools or shoals. Keeping them in groups is paramount to keeping them happy and healthy in your aquarium. Characins, most barbs, loaches, rasboras, danios, many catfish (including corydoras and otocinclus), rainbows and certain other fish all need the safety of a school in order to thrive. In species prone to fin nipping, having a larger school size can cut down on nipping behavior directed toward other fish outside the school and can spread out aggression within the group.
Generally, big fish eat little fish. This is true in most cases but especially true in the case of fish that are particularly voracious piscivores. Cichlids, non-suckermouth catfish and many of the ancient fish are prone to eating smaller fish. Angelfish will surely eat neon tetras. Take fish size and predation tendencies into account when making your stocking list.
Some species of fish are territorial with their own kind or similar species. Generally, this behavior increases during spawning. Cichlids, some catfish, many anabantids, puffers and many other fish are generally territorial. Tanks can be set up to provide multiple hiding places with distinct territories and broken sight lines so that multiple territorial fish can be kept in the same tank but there are limits to what territorial fish will tolerate. Sometimes fish will mistake unrelated species for their own kind. For example, male bettas might mistake flashy male guppies for other bettas and provoke an aggressive response even though the fish are distantly related. Also falling under this category are the "fin nippers." Some fish, including some barbs, tetras and most puffers, are notorious for nipping the fins of other fish which can damage them and cause infection. Nipping can be mitigated to a degree, in the schooling species anyway, by increasing the number of fish in the school of nippers. However, keeping fish with long, flowing fins with fish species known for their nipping is almost always a bad idea.
Fish require different types of foods and have different eating tendencies which can clash with one another. For example, fish that are slow to eat may be very difficult to keep with fish that have big appetites and are pushy at mealtimes. If you keep fish that have strict dietary needs but will eat anything presented to them, it's not a good idea to keep them with fish that eat other types of foods. Carnivorous fish should not be kept with mbuna as the the mbuna (which need a vegetable-based diet) will eagerly eat meaty foods intended for the carnivores.
Water Flow and Oxygen Levels
Depending on the body of water in which the fish lives in nature, fish have preferences for varying amounts of water flow. Generally fish from slower-moving water can tolerate lower oxygen levels and dislike lots of current while fish from fast-moving waters need that current and the high levels of dissolved oxygen associated with it.
Burrowing fish or fish that root through the substrate for food require a substrate that won't wear on their skin and barbels. Smooth gravel is good; sand is better.
Compatibility with Plants
Not all fish are plant-friendly. Central American cichlids tend to burrow and uproot plants while African rift lake cichlids will munch on them. Other species of herbivorous fish, notably silver dollars, will eat just about any aquarium plants.
Some fish are quite active and playful (zebra danios) while others are much more shy and even skittish (discus). Mixing fish with very different temperaments can stress out the shy fish, leading to illness. Fish that are active swimmers (danios, barbs, rainbows, some tetras, etc.) benefit from having longer tanks so that they can have more room to swim.
For large fish, the adult size of the fish has to be considered. Fish stunting is a topic for another article but generally it's best to keep fish in a tank that can comfortably house them as adults. For large fish, the tank should have enough room for them to easily move. If a fish grows to 18" in length, a tank with a minimum width of 18" is needed (although more is always better). For tall fish like angelfish, which can grow to 10" in height, the vertical dimension of the tank is equally as important.
This seems like a lot of information to process at once but after spending enough time in the hobby one learns to keep these factors at hand when making stocking choices. Additionally, the forums here at TFK are a great resource when it comes to asking questions about fish compatibility and are useful for both beginners and those with lots of experience simply looking for second opinions on fish stocking.