Every Key Signature, Sharps and Flats
Fifteen signatures, not twelve. Each one serves a major key and its relative minor, and three of the fifteen are second spellings of a pitch that already has one — which is the part of this topic that most often causes confusion, so those pairs are shown here rather than tidied away.
The two rules, and the two exceptions
Sharps: look at the last sharp written and go up one semitone. A signature ending in F♯ is G major; one ending in D♯ is E major. Sharps are always added in the order F, C, G, D, A, E, B, so the last one is fixed by the count.
Flats: the second-to-last flat names the major key directly. Flats are added in the exact reverse order — B, E, A, D, G, C, F — so a signature running B♭, E♭, A♭ has E♭ as its second-to-last flat and is E♭ major.
The exceptions: an empty signature has nothing to count and simply means C major or A minor, and a single flat has no second-to-last flat to read, so one flat means F major and has to be memorised. Every other signature yields to one of the two rules.
For the relative minor, count three semitones down from the major tonic — and check that the answer is a note actually in the key. Three semitones below A is F♯, not F, because A major contains F♯.
Sharp keys
C major / A minor
No sharps or flats
G major / E minor
1 sharp — F♯
D major / B minor
2 sharps — F♯ C♯
A major / F♯ minor
3 sharps — F♯ C♯ G♯
E major / C♯ minor
4 sharps — F♯ C♯ G♯ D♯
B major / G♯ minor
5 sharps — F♯ C♯ G♯ D♯ A♯
F♯ major / D♯ minor
6 sharps — F♯ C♯ G♯ D♯ A♯ E♯
C♯ major / A♯ minor
7 sharps — F♯ C♯ G♯ D♯ A♯ E♯ B♯
Rarely written — the other spelling of this pitch needs fewer accidentals
Flat keys
F major / D minor
1 flat — B♭
B♭ major / G minor
2 flats — B♭ E♭
E♭ major / C minor
3 flats — B♭ E♭ A♭
A♭ major / F minor
4 flats — B♭ E♭ A♭ D♭
D♭ major / B♭ minor
5 flats — B♭ E♭ A♭ D♭ G♭
G♭ major / E♭ minor
6 flats — B♭ E♭ A♭ D♭ G♭ C♭
C♭ major / A♭ minor
7 flats — B♭ E♭ A♭ D♭ G♭ C♭ F♭
Rarely written — the other spelling of this pitch needs fewer accidentals
The three pairs that sound identical
At the bottom of the circle of fifths the sharp side and the flat side meet, and the same twelve pitches can be spelled either way. B major (5♯) and C♭ major (7♭) are the same sound; so are F♯ major (6♯) and G♭ major (6♭), and C♯ major (7♯) and D♭ major (5♭).
Two of those three have a clear winner on accidental count: B major beats C♭ major by two, and D♭ major beats C♯ major by two. The middle pair is an exact tie at six each, and there is genuinely no arithmetic argument between them — which spelling gets written is decided by what the surrounding music is already using. String and guitar writing tends toward the sharp spelling; wind and brass writing toward the flat one, because that is what each group's instruments read most easily.
The deeper written treatment of how signatures are built and why they take these shapes is in Key Signatures Explained, and the whole set is arranged as a wheel in the interactive circle of fifths.