Each book of J. S. Bach's Well-Tempered Clavier contains twenty-four Preludes and Fugues—adding up to what one might casually describe as two such pairs of pieces in each major and minor key. Dmitri Shostakovich also wrote two books of piano pieces in all twenty-four major and minor keys: the early (and lightweight) Preludes op. 34, and the more mature (masterpiece) Preludes and Fugues op. 87. Similar examples could be cited. Each of these major works demonstrates the full range of musical tonality by centering a piece, or a pair of pieces, on the major and minor scale that starts on each of the 12 tones of the western musical tuning system—the seven white notes on the piano from A to G, plus the five black notes in between. Each is a demonstration of the possibility, within a certain system of musical temperament, of doing "Do Re Mi Fa Sol La Ti" on all twelve of these notes, as well as the minor-scale equivalent (the less well-known "Do Re Me Fa Sol Le Te").
Because the major and minor scales are series of pitch-intervals going up and down, they sound similar (i.e., like a major or a minor scale) regardless of what black or white note they start on. Because each scale consists of only seven out of twelve possible notes, they keep the same intervals by adding or subtracting black notes. If you're reading one of the preludes or fugues in the works mentioned above, you can tell which major or minor key it is by observing the "key signature" at the beginning of the piece, where the clef signs are followed by a more or fewer sharp-signs or flat-signs. How many notes are sharp or flat, and which ones, will determine which major or minor key the piece is in.
I'm going somewhere with this, but for the sake of the musically unformed among us, let me lay a bit of groundwork from "Music 101." If it goes Do Re Mi (with a whole-step between the second and third note of the ascending scale), it's a major key. If it goes Do Re Me (with a half-step up to the third note), it's a minor key. For each key signature, each series of sharps and flats laid out at the beginning of a piece, there are two possibilities: either it's the major key that goes Do Re Mi, or the minor key that goes Do Re Me. To tell which of these possibilities applies in a given piece, you have to look at the opening and/or closing notes or chords of the piece.
So, if there are no sharps or flats in the key signature—if the scale uses only white notes—you have a choice between the "Do Re Mi" of C Major or the "Do Re Me" of A Minor. Each time the "tonic" note goes up by an interval of a fifth, the key signature adds one sharp or subtracts one flat. So a fifth up, one sharp, would be either G Major or E Minor. Another fifth up, two sharps, would be D major or B Minor. Another fifth up, three sharps, is either A major or—careful, now!—F-sharp Minor. Four sharps, up another fifth, is E major or C-sharp Minor. With five sharps—B Major or G-sharp Minor—you have used up all the black notes going in the sharp direction. But you haven't used up all the possible keys. You could, for example, re-interpret the white note commonly called F as an E-sharp, making six sharps: F-sharp Major or D-sharp Minor. And you could also make all seven notes of the scale a sharp, adding a B-sharp (what musicians say is "enharmonic" with a C) to make the key C-sharp Major or A-sharp Minor.
You can also cycle the other way, going downward by fifths to subtract sharps and add flats to the key signature. A fifth down from C Major or A Minor is either F Major or D Minor, each with one flat. Another fifth down takes you to B-flat Major or G Minor with two flats. Three flats takes you down another fifth to E-flat Major or C Minor. Four flats gives you A-flat Major or F Minor. Five flats, again using all five black notes on the piano (but in a different order), gives you D-flat Major or B-flat Minor. And again, you can keep going a bit further to six flats, G-flat Major or E-flat Minor, with a C-flat at the end of the key-signature, enharmonic to B; and even seven flats, where the major key of C-flat actually starts on a white note (elsewhere known as B-natural) and has an F-flat (a.k.a. E) as well. The minor key of that key-signature is A-flat Minor.
So there you have all the major and minor keys, right? But already we have a problem, if there are supposed to be 24 of them. You see, I've just described a range of major/minor keys ranging from 7 flats to 7 sharps, plus a pair of keys with no black notes in the key signature. That adds up to 15 major and 15 minor keys, a total of 30. How can this be, when there are only twelve notes to start with? I've already given you a clue. It's the word "enharmonic." Though the note may be "spelled" more than one way in the score, it's the same key on the piano. So, for example, the scale of C-flat Major (seven flats) shares the same keys on the piano as B Major (five sharps). The same goes for D-flat Major (five flats) and C-sharp Major (seven sharps). G-flat Major and F-sharp Major are, literally, six of one and a half-dozen of the other; but the keys on the piano are the same.
And that's not even discussing all the theoretically possible, but stupid, keys that have double-sharps or double-flats in their key signature. There are tons of ways the same sequence of seven notes could be spelled in musical notation. Some of them are simply impractical, because they require the performer to do too many mental operations between reading the note in the score and playing the corresponding key on the instrument. But the six major keys above, plus their corresponding "relative minor" keys (i.e. whichever minor keys have the same sharps or flats in their key-signatures), are examples of entire keys that, for the purposes of real and practical music, could be spelled more than one way. And so they constitute the additional six major and minor keys, bringing the total number of reasonably meaningful keys up from 24 to 30.
Is that insane? Only a little. Obviously, most people are going to find it easier to read a piece in B Major than to read the identical piece (in terms of the keys to be pressed on the instrument) in C-flat Major. Technically it would be the same piece, but psychologically it would require more effort from the note-reading musician. Some pianists and organists have a bias either towards sharps or flats, finding the one easier to read than the other. So one pianist might prefer to play a Bach prelude in G-flat (six flats) rather than in the enharmonic key of F-sharp (six sharps), even though they end up being the same sequence of black and white notes on the piano. He or she might even prefer the seven sharps of C-sharp Major over the five flats of D-flat, as Bach evidently did, to judge by both books of the WTC. And even if the musician is equally comfortable playing these pieces in either key, there is anecdotal evidence that if you show him the same piece in both keys—say F-sharp and G-flat Major, for example—what he plays will sound like two different pieces. For even with a full mastery of note-reading in all the keys, even using the same fingering to play the notes, the psychological difference between six flats and six sharps will register in the way he performs each version. What I'm stumbling at is this: F-sharp and G-flat Major may share the same notes on the instrument, but they are not the same key.
And so we find that a description of Bach's WTC and Shostakovich's op. 87 as works that exploit "all 24 major and minor keys" is inaccurate. And that isn't just a theoretical quibble; it's a matter of record. In the first book of Bach's WTC, Prelude No. 8 is in E-flat Minor (six flats). Fugue No. 8, however, is in the enharmonic key of D-sharp Minor (six sharps). So WTC Book I, all by itself, exploits 25 different keys. To be sure, Prelude and Fugue No. 8 in Book II are both in D-sharp Minor. But given a choice between sharps and flats, Bach seems to prefer sharps. In his Inventions and Sinfonias, each a set of pieces in 15 different keys, he avoids using key-signatures with more than four sharps or flats; but in both sets, the keys he chooses include both E Major and C-sharp Minor (four sharps), but only F Minor and not A-flat Major (four flats). Thin this evidence may be, but then consider: In both books of the WTC, Prelude and Fugue No. 3 are in C-sharp Major (seven sharps) rather than the arguably easier-to-read D-flat Major (five flats). Again, in both books, for Prelude and Fugue No. 13 he chooses the six sharps of F-sharp rather than the equally challenging six flats of G-flat Major. And while it may be a no-brainer to opt for B Major (five sharps) rather than C-flat Major (seven flats) for Prelude and Fugue No. 23 of both books, his preference in the relative-minor instances of No. 18 (G-sharp Minor rather than A-flat Minor) isn't as easy to rationalize away, given his preference for seven sharps over five flats in the case of No. 3. The only times, other than Prelude No. 8 in Book I, that Bach uses a key signature with more than four flats in the WTC are the Preludes and Fugues No. 22 in B-flat Minor in both books (five flats, as opposed to the seven sharps of A-sharp Minor). This could be his way of making up for snubbing D-flat Major in both books. But it doesn't erase the impression that Bach leaned more towards the sharp side of the Circle of Fifths than to the flat.
One notices that in both op. 34 and op. 87, Shostakovich arranged things a bit differently. Bach started both books of WTC in the key of C Major and, alternating between the major keys and their "parallel minor" keys (e.g. C Major and C Minor), went up by half-step through all twelve tones of the chromatic scale, ending with B Major and B Minor. He did something similar with the Inventions and Sinfonias, raising the tonic note by half-steps from C up to B, with parallel major and minor keys appearing in that order, and only skipping a few of the harder-to-play keys. Shostakovich, likewise, begins both his sets with C Major, but from there on things go differently. Alternating the relative major and minor keys (those that have the same key signature, such as C Major and A Minor), he travels around the circle of fifths in the sharp direction until he gets to F-sharp Major; then, again in both op. 34 and op. 87, he side-slips into the flat side of the circle with E-flat minor (enharmonic to D-sharp Minor, and hence the relative minor of F-sharp Major), and continues subtracting flats from the key-signature until he ends with the pieces in F Major and D Minor (one flat each). So, unlike Bach, Shostakovich chooses D-flat Major in both sets rather than C-sharp Major, and E-flat Minor three times out of three, instead of the D-sharp Minor that Bach preferred three times out of four. Shostakovich avoids any hint of the sharps-over-flats bias suggested by Bach's choice of keys.
It is therefore technically accurate to say that Shostakovich exploits exactly 24 major and minor keys in these two sets of piano pieces. But many of the keys that aren't featured in the index of the book, make guest appearances in the middle of some tonally adventurous pieces. The same could be said of Bach, and of other composers. Just because they didn't write preludes and fugues "in" C-flat Major doesn't mean you won't find that key, and others like it—full of tricky enharmonic spellings of white notes, double-flats, double-sharps, and the psychological shadings they bring with them—in the midst of a musical argument that often explores distant realms of harmony. There are indeed more than 24 major and minor keys; there are, in fact, more than the 30 keys for which a responsible case can be made for learning to play pieces written in all of them. The Circle of Fifths spirals into infinity—though it quickly passes out of the territory well occupied by reason and intuition. If a human performer could but learn to think he was playing in the key of F-double-sharp (rather than G Major), or D-double-flat (rather than C), the psychology of what he is doing might produce some strange effects that, I believe, would make a difference to the mind's ear of a sensitive listener.
POSTSCRIPT: I mentioned above that many keyboardists have a preference for either sharp keys or flat keys. Some organists and pianists that I have known carried this so far as to develop an downright aversion to one side of the Circle of Fifths. It's strange to relate, but it's a fact that many musicians—particularly those who might describe themselves as less experienced or less talented—develop a work-around that actually involves transposing a whole piece of music, by sight, from one key to another. It's one of the first tricks I was taught as a beginning organist, by a lady who was petrified of key signatures with more than two sharps: You play the notes on the same lines and spaces as printed, but you pretend the key signature has flats instead of sharps. In the music, if you see an accidental sharp sign, you interpret it as a natural; a natural sign, you interpret as a flat. The reverse works if you want to go from flats to sharps; and I have known organists to use this trick, not because they liked flats better than sharps or vice versa, but to avoid playing a key with five black notes when two was possible, or to avoid playing four sharps or flats when three of the opposite kind was possible. The difference between this tactic and someone who is genuinely afraid of, say, sharps, is that the latter will actually prefer to play a piece with five or four flats rather than two or three sharps. In either case, the irony has often tickled me: for all their musical insecurity, these people are falling back on a sophisticated and mentally demanding process—sight transposition—albeit with the aid of a simple rule-of-thumb.
One of the little mathematical tricks of music, made possible by the principle of the Circle of Fifths and whatnot, is what I like to call the Rule of Seven. Take any two major or minor keys that can be read on the same lines and spaces of the musical staff; add together the number of sharps or flats in both their key signatures; the sum is always seven. I'm not sure if there is a word for the relationship between these two keys, like "enharmonic" or "parallel" or "relative" (none of which apply). If I were to coin a word for them, it would probably be something like "homographic" or "mirror," or maybe "flipsy." So in short, the sum of the sharps and flats in the key signatures of two flipsy keys is always seven. The rule holds even when you get into those theoretical keys with double-sharps and -flats in them—only then you find the difference between the two key-signatures, taking care to count double-sharps or -flats as two of each.
So you can read a piece in B-flat minor (five flats) as B minor (two sharps), and as long as you remember the Rule of Seven, you won't have to strain your brain much to figure out how many sharps to lock into your mental key-signature: seven minus five is two. It's a simple formula that has come to the rescue of many a novice or shaky amateur. It has also, unfortunately, caused disasters. For example, take the organist who took a well-known three-part Te Deum setting (beginning and ending in B-flat Major, with a passage of B-flat Minor in the middle) and transposed the middle section into B Minor. She found it easier to play, no doubt; but the key change at the heart of the piece was wrong.
More math-e-musical rules of thumb, while I'm blathering on... There's a Relative Major/Minor Rule of Three: The tonic note of any minor key is a tonal third down from that of its relative major key (which has the same key signature). There's a Parallel Major/Minor Rule of Three: The key-signature of any minor key has a net total of three fewer sharps and/or three more flats than its parallel major key (which has the same tonic note). These are really the same rule viewed from opposite points of view. Then there's the Rule of Twelve: The sum of the key signatures of two enharmonically equivalent keys is always twelve. C-flat Major (7 flats) plus B major (5 sharps): 12. G-flat Major plus F-sharp Major: 6 flats + 6 sharps = 12. Here, again, you have to be prepared to count double-sharps or -flats as two each; so you could deduce, and correctly too, that the theoretical key of B-sharp Major (enharmonic to the white-note key of C) must have five double-sharps, plus two single-sharps (E and B). And again, when the total number of sharps or flats in a key signature (counting doubles as two) exceeds 12, you subtract instead of adding and the rule holds. Thus, the difference between the theoretical key of C-double-sharp (14 sharps!) and its enharmonic key of D (2 sharps) is 12. Isn't that cool?
P.P.S. Okay, smart-aleck. As if I haven't gone far enough off topic already... What happens to the Rule of Twelve when you're comparing the key-signatures of two equally stupid, theoretical keys? Like C-double-sharp Major and E-double-flat Major, which both happen to be enharmonic to D Major... Solution: Don't compare two stupid keys to each other. Therein lies madness.