The order of sharps and flats
The accidentals in a key signature always appear in the same order. Sharps: F, C, G, D, A, E, B. Flats, the other way round: B, E, A, D, G, C, F. If a key signature has two sharps, they are F and C; never any others.
With sharps: a half step above the last one
The last sharp is the leading tone of the major key. The tonic is a half step above it. With two sharps, the last one is C♯: the key is D major.
With flats: the second-to-last one
With flats, the major tonic is the second-to-last flat. With three (B, E, A), the second-to-last is E♭: E-flat major. With a single flat there is no second-to-last: it is F major, and that one you learn by heart.
Major or its relative minor
Each key signature serves two keys: a major key and its relative minor, a minor 3rd below (three half steps). D major and B minor share the same two sharps.
To tell which one it is, look at the music: which note and which chord it ends on, and whether the leading tone of the minor key shows up with an accidental. In B minor you will see A♯, which is not in the key signature.
| Key signature | Major | Relative minor |
|---|---|---|
| None | C major | A minor |
| 1 sharp (F) | G major | E minor |
| 2 sharps (F, C) | D major | B minor |
| 3 sharps (F, C, G) | A major | F♯ minor |
| 4 sharps (F, C, G, D) | E major | C♯ minor |
| 1 flat (B) | F major | D minor |
| 2 flats (B, E) | B♭ major | G minor |
| 3 flats (B, E, A) | E♭ major | C minor |
| 4 flats (B, E, A, D) | A♭ major | F minor |