---
title: "How to Write Over Partition By SQL Query"
description: "Here's a way to write an SQL query over partition by limiting rows in Oracle"
url: https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/
date: 2022-12-10
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2022/12/pexels-photo-5965548.jpeg?fit=1880%2C1253&ssl=1
categories: ["SQL"]
tags: ["SQL Partition By"]
type: post
lang: en
---

# How to Write Over Partition By SQL Query

Here is how you can limit rows by row number and Over partition by statements in Oracle SQL.

For instance, in a year the launches of a satellite are more than 2. In that case, the output should print up to ‘2’ rows. Here is an SQL query to achieve it.

*Over partition by*

IN THIS PAGE

1. [Over and Partition By](https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/#over-and-partition-by)
2. [SQL Row number Over Partition by](https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/#sql-row-number-over-partition-by)

## **Over and Partition By**

The **over** keyword’s purpose is to get the row number that has first entry in the year. The [**partition by and order by statements**](https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions137.htm) make the data into small partition.

Here is an SQL query prints upto 2 rows if satellite launches are more than “2” in a year. Created two tables. Launch and Satellite. Inserted data and make ready to use these tables.

```
--create table satellite(official_name char(20), launch_id number);
--insert into satellite values('abc1', 1001);
--insert into satellite values('abc2', 1002);
--insert into satellite values('abc3', 1003);
--insert into satellite values('abc4', 1004);
-- into satellite values('abca1', 1005);
--insert into satellite values('abca2', 1006);
--create table launch(launch_date Date, launch_id number);
--insert into launch values ('01-JAN-1970', 1001);
--insert into launch values ('01-FEB-1971', 1002);
--insert into launch values ('01-MAR-1972', 1003);
--insert into launch values ('01-APR-1973', 1004);
--insert into launch values ('01-JAN-1970', 1005);
--insert into launch values ('01-JAN-1970', 1006);
```

Advertisements

## **SQL Row number Over Partition by **

```
--Prints per year 2 rows if more than 2 satelites launched
select launch_date, official_name
from
(
   select
      to_char(launch_date, 'YYYY-MM-DD') launch_date,
      official_name,
      row_number() over 
      (
         partition by trunc(launch_date, 'year')
         order by launch_date
      ) first_n_per_year
   from satellite
   join launch
      on satellite.launch_id = launch.launch_id
   order by launch_date, official_name
)
where first_n_per_year <= 2
order by launch_date, official_name;
```

The sub-query result

Here it prints row number. The same query you can use to get ranks.

![The sub-query result](https://i0.wp.com/srinimf.com/wp-content/uploads/2022/12/sql-over-query.png?resize=776%2C753&ssl=1)

The output achieved

| LAUNCH_DATE | OFFICIAL_NAME |
| --- | --- |
| 1970-01-01 | abc1 |
| 1970-01-01 | abca2 |
| 1971-02-01 | abc2 |
| 1972-03-01 | abc3 |

*Table output*

The satellite launches were more than 2 in 1970. So in the output, it prints only two rows for the year 1970. It shows that you can control rows [using Over Partition](https://srinimf.com/2014/09/06/db2-partition-by-sql-query/) By SQL with a condition you need.

Related

- [The Most Confusing SQL Queries Made Easy (Beginner to Pro)](https://srinimf.com/2025/07/19/the-most-confusing-sql-queries-made-easy-beginner-to-pro/)DateJuly 19, 2025
- [How to Rank the Output Using Over Partition By DB2 SQL](https://srinimf.com/2014/09/06/db2-partition-by-sql-query/)DateSeptember 6, 2014
- [SQL Window Functions: 11 Essential Examples with Use Cases](https://srinimf.com/2025/05/03/11-top-mysql-window-functions-with-use-cases/)DateMay 3, 2025

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2022%2F12%2F10%2Fhow-to-write-over-partition-by-sql-query%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2022%2F12%2Fpexels-photo-5965548.jpeg%3Ffit%3D1200%252C800%26ssl%3D1&description=How%20to%20Write%20Over%20Partition%20By%20SQL%20Query)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2022/12/10/how-to-write-over-partition-by-sql-query/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20How%20to%20Write%20Over%20Partition%20By%20SQL%20Query&body=https%3A%2F%2Fsrinimf.com%2F2022%2F12%2F10%2Fhow-to-write-over-partition-by-sql-query%2F&share=email)
-

### Like this:

Like Loading…
