US District Court Judge William Orrick III questioned the purpose of the president's order as he heard arguments from two large California counties and the Justice Department in San Francisco federal court. Both counties have asked for a nationwide preliminary injunction to the order.
As part of a larger plan to transform how the United States deals with immigration and national security, Mr Trump in January signed an order targeting cities and counties that limit cooperation with federal immigration authorities.
Sanctuary cities in general offer safe harbor to illegal immigrants and often do not use municipal funds or resources to advance the enforcement of federal immigration laws. Sanctuary city is not an official designation.
Santa Clara County, which includes the city of San Jose and several smaller Silicon Valley communities, sued in February, saying Mr Trump's plan to withhold federal funds is unconstitutional. San Francisco filed a similar lawsuit.
On Friday, the counties described the order as a "weapon to cancel all funding to jurisdictions," said John Keker, an attorney representing Santa Clara County. "All around the country, including here, people are having to deal with this right now."
Santa Clara County receives roughly $1.7 billion in federal and federally dependent funds annually, about 35 percent of its total revenues. The county argued that every day it is owed millions of dollars of federal funding, and its budgetary planning process had been thrown into disarray by the order.
The Justice Department said the counties had taken an overly broad interpretation of the president's order, which would impact only Justice Department and Department of Homeland Security funds, a fraction of the grant money received by the counties.
The government also argued that there had been no enforcement action to date, and it was unclear what actions against the counties would entail.
Judge Orrick asked the government what was the purpose of an executive order, if it only impacted a small amount of county funding.
Attorneys for the government said the order had highlighted issues that the Trump Administration deeply cared about and a national policy priority.
To win a nationwide injunction, local governments must demonstrate a high level of harm, the Justice Department noted in court filings last month.
(Reporting by Robin Respaut, Additional Reporting by Dan Levine, Editing by Dan Grebler)