Q

# Calculating quartiles

Further to my question on percentiles, how could I return a table containing the lower and upper quartile of a...

particular variable?

This is similar to the question about percentiles, which I responded to at http://searchdatabase.techtarget.com/ateQuestionNResponse/0,289625,sid13_cid450343_tax289340,00.html. I'll use the same table and data to illustrate how to calculate quartiles. Here's the DDL:

```create table ExamResults
( Student varchar(15),
Score numeric(3)
);

insert into ExamResults values ('Lenny',94);
insert into ExamResults values ('Ralph',93);
insert into ExamResults values ('Joe',91);
insert into ExamResults values ('Mary',80);
insert into ExamResults values ('Frank',89);
insert into ExamResults values ('Susan',89);
insert into ExamResults values ('Bill',74);
insert into ExamResults values ('Ben',99);
insert into ExamResults values ('Fred',66);
insert into ExamResults values ('George',100);
insert into ExamResults values ('Walter',90);
insert into ExamResults values ('Betty',89);
insert into ExamResults values ('Heather',83);
insert into ExamResults values ('Richard',91);
```
From here, we can calculate the quotient of two scalar subqueries -- one to determine how many scored at or below a particular row, and another to determine how many rows exist in the set -- to determine where a particular row falls in the ranking. Multiplying that quotient by four and taking the ceiling of the result gives us our quartile.
```select a.*, ceiling( 4.0 *
( select count(*)
from ExamResults
where Score <= a.Score ) /
( select count(*) cnt
from ExamResults )
) quartile
from ExamResults a;
```
Here are the query results:
```STUDENT         SCORE QUARTILE
--------------- ----- --------
Lenny              94        4
Ralph              93        4
Joe                91        3
Mary               80        1
Frank              89        2
Susan              89        2
Bill               74        1
Ben                99        4
Fred               66        1
George            100        4
Walter             90        3
Betty              89        2
Heather            83        2
Richard            91        3
```

• The Best SQL Web Links: tips, tutorials, scripts, and more.
• Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
• Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
• Ask the Experts yourself: Our SQL, database design, Oracle, SQL Server, DB2, metadata, object-oriented and data warehousing gurus are waiting to answer your toughest questions.

This was last published in March 2002

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Cross-platform integration, data preparation process grows in cloud

While cloud computing may be convenient and more cost-effective for users, it can also lead to new challenges and requirements in...

• ### Graph data model cements tight relationships between data elements

Graph databases can help define and discover relationships between entities -- and offer increased flexibility and better ...

• ### Microsoft SQL Server 2016 relational DBMS overview

Microsoft SQL Server 2016 for Windows comes in four editions, with updates that include a new stretch database feature, Polybase,...

• ### Business should work with IT to get self-service BI governance right

Self-service analytics is all about getting IT roadblocks out of the way of the business, but traditional tech teams can still ...

• ### Creative projects leave people guessing about future impact of AI

A push is underway to write creative AI algorithms that can engage in music, film and design projects. So far, they have ...

• ### Data visualization plays an important role in a big data environment

Enterprises are increasingly putting data visualization tools, like Tableau, at the top of their big data platforms to start ...

## SearchSAP

• ### Hillarys Blinds speeds SAP S/4HANA upgrade with Panaya tool

Hillarys Blinds, a veteran of several SAP ECC upgrades, completed a move to SAP S/4HANA in just six months using the Panaya ...

• ### Charlotte Hornets build data warehouse with SAP HANA-based Phizzle

The Charlotte Hornets implemented Phizzle FanTracker, an SAP HANA-based platform, to consolidate fan records from many data ...

• ### Does SAP ONE Support Launchpad make SAP support any easier to use?

The new Fiori user experience makes it easier to access applications and support services, but product-specific support still ...

## SearchSQLServer

• ### SQL Server on Linux signals Microsoft's changing development landscape

Expert Joey D'Antoni explains what SQL Server on Linux and the addition of Enterprise Edition features to Standard Edition say ...

• ### How to get the most out of virtual SQL Server with Microsoft Hyper-V

SQL Server is a CPU-intensive technology, which can make it tricky to run in a virtualized environment. Keep your SQL Server ...

• ### Microsoft previews SQL Server on Linux, opens features across editions

Microsoft looks to broaden the horizons of SQL Server, as it moves some Enterprise features to Standard Edition and issues the ...

## TheServerSide

• ### Docker instances become the new norm and adoption goes mainstream

Many organizations use Docker instances for many reasons, although security, data storage and monolithic fears remain barriers to...

• ### How to turn your DevOps failures into ALM successes

Doing the right thing doesn't always mean you're doing things right. But don't fret, because short-term DevOps failures can mean ...

• ### From chatbots to IBM's Watson: How software deals with conversational language

The next big thing in software development is conquering the conversational language development hurdle. Here's how the big ...

## SearchDataCenter

• ### Build a data center shutdown procedure to prepare for the worst

A data center shutdown checklist helps IT teams focus on backup, testing and system verification before pulling the plug and ...

• ### Telcos purge colocation data centers, open door to neutral connections

Enterprise customers in Verizon and CenturyLink's colocation data centers should expect better cloud and network connection ...

• ### IT slowly embraces composable infrastructure

If there will be a way to make an enterprise data center as efficient and optimized as cloud computing, composable infrastructure...

## SearchContentManagement

• ### Users prefer Office 365 suite collaboration features over SharePoint

Users don't want to jump through hoops, and they want applications to work on mobile devices. Those needs may kill traditional ...

• ### What new SharePoint features to expect in the next 12 months

Now that Microsoft is developing its SharePoint features, there are new feature rollouts all the time. Here's a rundown of the ...

• ### How WCM and AI promote smart digital user experiences

Digital user experiences and other business processes are poised to become much richer with artificial intelligence at the helm.

## SearchFinancialApplications

• ### Foot Locker seeks a good fit with Infor pre-employment assessment tool

Foot Locker is among a growing number of companies using cloud-based pre-hire assessment to screen job applicants for character ...

• ### Project planning for a new corporate performance management system

A corporate performance management system touches most aspects of any business. You need a carefully thought-out plan to ensure a...

• ### No one-size-fits-all strategy for cloud ERP software migration

Experts say a cloud ERP transition plan will vary according to a variety of factors, from company size to an organization's ...

Close