This answer discusses how combat and non-combat officers fit into a military hierarchy, how this impacts the ability of one to command the other, and how that fits into the ST:TOS hierarchy.
However, at least at two separate times (in Voyager; possibly elsewhere as well) it is stated that in medical matters, the chief medical officer outranks the captain of the ship. That's Starfleet Medical Protocol regulation 121 section A.
- When is that protocol first established and/or stated, in-universe? (Comparisons to real world situations are welcome as well, but are not the focus of this question.)
- What are the specific limits to this protocol? Is it, for example, possible for a properly skilled physician to outrank an admiral?