Q
Problem solve Get help with specific problems with your technologies, process and projects.

Sequence of WHERE and ORDER BY clauses

In this query, I can see that the WHERE clause takes place before the ORDER BY. Is there a way I can reverse this? My objective is to first sort the rows and then to pick the first one in the sorted rows.

In the following query, I can see that the WHERE clause takes place before the ORDER BY.

```SELECT column1, column2, column3
FROM MyTable
WHERE rownum < 2
ORDER BY column1
```

Is there a way I can reverse this? My objective is to first sort the rows and then to pick the first one in the sorted rows.

Before I answer this, please allow me to make a general comment. Our objective is not to first sort the rows and then do something. That's a procedural solution. In other words, that's thinking like an old-fashioned programmer: do this, then do that, then do something else, and this will produce the result. It might work, but often it's not the best solution.

Sorting an entire table means that the database will actually read each row (and perhaps rewrite/copy it several times, depending on the sort algorithm). Sorting a large table is an immensely expensive operation, as computer operations go. Yes, sorting the entire table to see which row ends up in first place does work. But it's not the right approach.

Instead, let's try stating the problem not in terms of how to get what we want, but rather, just what we want. In this case it's simple. We want the row which has the lowest value of column1.

```select column1, column2, column3
from MyTable
where column1
= ( select min(column1)
from MyTable )
```

Okay, to be fair, this is the "Top 1" problem, which is a special—and simpler—case of the more general "Top N" problem. Interestingly enough, various database vendors have "solved" the more general problem with special but proprietary syntax such as TOP, LIMIT, or ROWNUM. See FIRST N rows, TOP N rows, LAST N rows, BOTTOM N rows... Note, however, that TOP, LIMIT, or ROWNUM all involve sorting the table, so they might not perform very well.

SearchDataManagement

• Era Software raises \$15.25M for enterprise data management

Startup that began as the EraDB time series database is advancing its efforts with new funding and a cloud service for its ...

• Dgraph GraphQL database users detail graph use cases

Graph DB vendor Dgraph Labs is expanding its AWS cloud footprint with new regions and adding change data capture capabilities in ...

• Pandemic triggered data security movement to DBaaS

Database-as-a-service technology has aided enterprises tasked with keeping data secure with IT professionals working from home ...

• Startup Veezoo emerges from stealth with NLQ-based platform

Aiming to be 'Siri for enterprises,' an analytics startup emerged from stealth with a platform that enables users to interact ...

• 15 data science tools to consider using in 2021

Numerous tools are available for data science applications. Read about 15, including their features, capabilities and uses, to ...

• Insightsoftware acquisition of Izenda targets embedded BI

ERP vendor Insightsoftware acquired embedded analytics specialist Izenda on April 15, marking its second purchase of an embedded ...

SearchSAP

• S/4HANA Cloud SaaS ERP: Buying team overview

SAP's multi-tenant SaaS ERP, S/4HANA Cloud, is a viable choice for companies that need ease in their infrastructure management. ...

• SAP forms financial services partnership with Dediq

SAP and financial industry investment firm Dediq are forming a new business unit to develop applications that help banks and ...

• Unpatched applications threaten SAP security

Cyberattacks are a significant threat to unpatched, unprotected SAP applications, according to a new threat intelligence report ...

SearchSQLServer

• SQL Server database design best practices and tips for DBAs

Good database design is a must to meet processing needs in SQL Server systems. In a webinar, consultant Koen Verbeeck offered ...

• SQL Server in Azure database choices and what they offer users

SQL Server databases can be moved to the Azure cloud in several different ways. Here's what you'll get from each of the options ...

• Using a LEFT OUTER JOIN vs. RIGHT OUTER JOIN in SQL

In this book excerpt, you'll learn LEFT OUTER JOIN vs. RIGHT OUTER JOIN techniques and find various examples for creating SQL ...

TheServerSide.com

• Incorporate diversity and inclusion in technology design

DEI in technology is about more than creating a diverse workplace. We talked to a few DEI professionals about how teams build ...

• Microsoft previews OpenJDK distro to the delight of devs

In a move meant to attract more Java developers to its Azure cloud and further support the Java community, Microsoft launched a ...

• Supreme Court ruling on Java APIs eases developer worries

Now that the Supreme Court has ruled for Google over Oracle in their high-stakes copyright battle over Java APIs, developers can ...

SearchDataCenter

• Nvidia SDK simulates quantum computing circuits on GPU systems

Nvidia edged its way into the quantum computing market with an SDK that simulates quantum circuits by adding horsepower to ...

• Programmable processor technology for next-gen data centers

The right processing technology can benefit your data center. Learn about advancements in CPU technologies, recent vendor ...

• Data processing units accelerate infrastructure performance

DPUs often run on networking packets to move information in the data center, instead of supporting processing workflows. Get an ...

SearchContentManagement

• OpenText releases Cloud Editions content services updates

OpenText CE 21.2 includes federated document compliance that extends to Microsoft Office 365, along with a revamped content ...

As the pandemic disrupts paper workflows, Adobe courts small business users with simple webforms, digital signatures and payments...

• Know when to use a headless CMS and when it's not worth it

Headless CMSes aren't a silver bullet for every circumstance. Evaluate three solid use cases for a headless CMS and three ...

SearchHRSoftware

• Face mask detection a newcomer to employee surveillance

Face mask detection has emerged as another form of employee surveillance technology, and its adoption may be helped indirectly by...

• 20 diversity and inclusion tools to power your DEI program

Diversity and inclusion tools shouldn't replace strategy, but these tools can help companies in multiple ways, from tracking ...

• DEI tools arrive at critical time for HR

Employers are being held accountable for meeting diversity, equity and inclusion goals, especially executives. HR vendors are ...

Close