The 5-4 decision would allow the broadest drug testing the court has yet permitted for young people whom authorities have no particular reason to suspect of wrongdoing. It applies to students who join competitive after-school activities or teams, a category that includes many if not most middle-school and high-school students.
Previously these tests had been allowed only for student athletes.
"We find that testing students who participate in extracurricular activities is a reasonably effective means of addressing the school district's legitimate concerns in preventing, deterring and detecting drug use," Justice Clarence Thomas wrote for himself, Chief Justice William H. Rehnquist and Justices Antonin Scalia, Anthony M. Kennedy and Stephen Breyer.
The court stopped short of allowing random tests for any student, whether or not involved in extracurricular activities, but several justices have indicated they are interested in answering that question at some point.
The court ruled against a former Oklahoma high school honor student who competed on an academic quiz team and sang in the choir. Dartmouth College student Lindsay Earls, a self-described "goodie two-shoes," tested negative but sued over what she called a humiliating and accusatory policy.
The Pottawatomie County school system had considered testing all students. Instead, it settled for testing only those involved in extracurricular activities on the theory that by voluntarily representing the school, those students had a lower expectation of privacy than did students at large.
The ruling is a follow-up to a 1995 case, in which the court allowed random urine tests for student athletes. In that case, the court found that the school had a pervasive drug problem and that athletes were among the users. The court also found that athletes had less expectation of privacy.
Thursday's ruling is the logical next step, the Oklahoma school and its backers said, and the court majority agreed.
"The particular testing program upheld today is not reasonable, it is capricious, even perverse," Justice Ruth Bader Ginsburg wrote for the dissenters.
In a brief, separate dissent, Justices Sandra Day O'Connor and David Souter said they disagreed with the court's ruling in 1995 and disagree now.
Of the estimated 14 million American high school students, better than 50 percent probably participate in some form of organized after-school activity, educators say. The trend is toward ever greater extracurricular participation, largely because colleges consider it a factor in admissions.
Earls and the American Civil Liberties Union argued that the Oklahoma school board could not show that drugs were a big problem at Tecumseh High School. She claimed the "suspicionless" drug tests violated the Constitution's guarantee against unreasonable searches.
Pottawatomie educators, backed by the Bush administration, argued that any drug problem is a concern. Also, the school said, the drug tests were a deterrent for students who knew they could not participate in favorite activities unless they stayed clean.
During oral arguments in the case in March, a Bush administration lawyer said universal testing would be constitutional, even though a lawyer for the Oklahoma school said she doubted that would be so.
Numerous schools installed drug testing programs for athletes after the 1995 ruling, but wider drug testing remains relatively rare among the nation's 15,500 public school districts. Lower courts have reached differing conclusions about the practice.
The Tecumseh testing program ran for part of two school years, beginning in 1998. It was suspended after Earls and another student sued.
The Tecumseh policy covered a range of voluntary clubs and sports, including the Future Farmers of America club, cheerleading and football. Students were tested at the beginning of the school year. Thereafter, tests were random.
Overall, 505 high school students were tested for drug use. Three students, all of them athletes, tested positive.
A federal appeals court ruled against the program, saying it took the Supreme Court's 1995 ruling too far. Sports are different from other extracurricular activities, the lower court said, and the school had not done enough to show that students who participated in those activities were abusing drugs.
The school district appealed to the Supreme Court.
The case is Board of Education of Independent School District No. 92 of Pottawatomie County v. Earls, 01-332.