As the Enterprise attempts to correct the decaying orbit of a moon before it crashes into the populated planet below, Q appears, having been stripped of all his powers by the Q Continuum and made into a mortal human being. Having the choice of where to be banished, Q picked the Enterprise because of "all the fun we had in the past." Q now finds himself among a crew that doesn't like him, experiencing the very non-omnipotent lifestyle of a normal, limited human.
"Deja Q" is one of the rare attempts by TNG to do sustained comedy, and it might also be the most successful. Q as a fish out of water is a gimmick, to be sure, but it's a good one. John de Lancie has natural comic timing, and the story wisely pairs Q with Data for much of the show, which is an inspired choice. Not only is Data the perfect, endlessly patient straight man for Q's nonstop chatter, it allows the story to provide a running commentary on the human condition from the perspective of outsiders.
A successful comedy must also have sharp, funny dialog, which "Deja Q" has. In addition to all of Q's ongoing struggles with human banalities like sleeping and eating ("I'll have 10 chocolate sundaes"), we have the running joke that this formerly omnipotent being still takes omnipotence for granted. (His solution to the decaying moon orbit: "Change the gravitational constant of the universe." And he isn't kidding; he means it.) Q proves to be an insufferable man. We have scene after scene of Q's arrogance, boredom, and sarcasm. The secret to this working is that because of the way de Lancie plays him, Q is likable despite being a constant pain in the ass. (Q on not being able to get along with others: "It's hard to work well in groups when you're omnipotent.")
Even the peril — and no TNG plot would be satisfied without peril — is made amusing. (When Q is attacked by the Calamarain and Data saves him, Data lands on his side, like an object rather than a person, which is a likably goofy gag.) But what ultimately makes this episode work as well as it does is that it's actually about something — Q and Data and their similar plights of trying to figure out what it means to be human while approaching that question from completely different points of view: Data as someone who wants to be human, and Q as someone who definitely does not.