Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
Window Functions in PL/SQL
Next article icon

SQL Server Window Functions ROWS vs. RANGE

Last Updated : 25 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

We will cover the important concept of the MS SQL Server which is the difference between Row and Range. The confusion between the row and range and where it is used will be eradicated by reading this article. So, be with the flow of the article for a better understanding and keep practicing side by side. Understanding this topic will help you in better understanding of queries in SQL.

So, ROW is a series of persons or things arranged in a usually straight line while RANGE is a variety of things that belong to the same group. The same concept is with SQL Server. The whole concept depends upon these two basic definitions of ROW and RANGE.

Prerequisites

  • Basics of SQL Server: It will be wonderful to start with this article if you have prior knowledge of the Basics of SQL i.e Creating Database, Creating Tables, and Inserting Values.
  • You need to be with the flow of an article and practice side by side for a better understanding of the topic.

How ROWS Works

Think of ROW as a way of counting individual rows in your result set. It gives you a single record as a row from the table. Let's understand with the help of the example if we want a record of a single data then the row is used. Helps to assess or manage the data of the single entity that is the row.

Importance: ROW is handy when you want to work with neighboring rows, like calculating the difference between the current row and the one before or after.

How RANGE Works

RANGE is a bit different. Instead of looking at individual rows, it groups rows based on their values. It will give you the set or group of the rows satisfying the condition given by the user. Let's Understand with an example that we want the data of the group of the students having age greater than 15 from th e class. There will be multiple records in the output. Helps to asses the data of the group with some condition satisfying.

Importance:-RANGE is useful when you're more interested in the values themselves rather than the specific rows. It's great for tasks like identifying trends or patterns in your data.

Difference Between ROWS and RANGE

Imagine your data as a bunch of neatly stacked boxes. Rows are like individual boxes, each holding specific information, while range is like a set of boxes grouped together based on certain criteria. Let's unravel this mystery using a virtual table view.

Aspects

Row

Range

Definition

Represents individual records.

Represents a subset based on criteria.

Query Example

SELECT * FROM Table WHERE ID = 1;

SELECT * FROM Table WHERE Column BETWEEN 10 AND 20;

Purpose

Fetches specific individual data.

Retrieves a set of data meeting criteria.

Analogous Scenario

Retrieving a specific box from a stack.

Grouping boxes with a specific label.

Query Syntax

SELECT * FROM Table WHERE condition;

SELECT * FROM Table WHERE Column BETWEEN value1 AND value2;

Result

Returns a single record.

Returns multiple records.

This tabular breakdown provides a quick reference to understand the key distinctions between rows and range in SQL Server. Rows focus on individual records, while range deals with subsets based on specific criteria. Choose rows when you want specific details, and go for range when you need to group data within a defined range.

Example of ROW and RANGE

Lets dive into an example in which we need to display the record of the Information table according to the conditions. By this example you will understand the concept of Range and row more easily.

So start with creating a table for the dummy data so that we can understand concept more easily.

Created a table Information in sql server. You can take a reference from the below given creation and insertion table.

Example_Table_Query
Example Table Query

QUERY

--Creation of Table Information
CREATE TABLE Information(
EmployeeID INT,
Name VARCHAR(50),
Salary DECIMAL(10,2)
);
--Inserting the values in the table
INSERT INTO Information VALUES(1, 'John Doe' , 50000.00);
INSERT INTO Information VALUES(2, 'Jane Doe' , 60000.00);
INSERT INTO Information VALUES(3, 'Bob Smith' , 75000.00);
INSERT INTO Information VALUES(4, 'Ankit' , 85000.00);
INSERT INTO Information VALUES(5, 'Ankush' , 77000.00);

This will create the table Information and will insert the dummy data into it.

Output:

output

Here is the Sample Table to Refer.

Example 1: Using Row

To display one single record of the table we use the WHERE clause. This will Display the Record of the 'Jane Doe' and display all the columns of it.

Query:

SELECT * FROM Information WHERE EmployeeID = 2;

Output:

Row_Output
Output

Explanation:

Here the only that record will be displayed with employee id=2 which is the row of the table Information. Hence the single record is displayed according to the given condition.

Example 2: Using Range

Similarly here also we use where clause with range to display set of records. Here the Output would be all Employee Details whose Salary Comes in between the range form 50000 to 70000. hence the John Doe, Jane Doe, Bob Smith list will be displayed.

Query:

SELECT * FROM Information WHERE Salary BETWEEN 50000.00 AND 75000.00;

Output:

Range_output
Output

Explanation:

The range based condition shows the multiple rows or records that satisfy the conditions. Hence giving more records in the output.

Conclusion

And there you have it – the difference between rows and range in SQL Server, explained in the simplest terms possible. Rows are your go-to for individual details, while range is best tool for grouping data based on certain conditions. This guide is provided with great detail and is provided in user friendly language. So at last both row and range are the best tools for managing the data it's depend upon the usage and the need where to use that varies.


Next Article
Window Functions in PL/SQL
author
sharmaankit
Improve
Article Tags :
  • Geeks Premier League
  • SQL Server
  • Databases
  • Geeks Premier League 2023

Similar Reads

  • SQL Server Group Functions
    The group function in SQL Server provides a powerful tool for performing calculations on groups of rows, allowing you to group data based on specific criteria. This function is important when you want to analyze and summarize information from multiple records in a data structure. The basic group fun
    3 min read
  • Window Functions in SQL
    SQL window functions are essential for advanced data analysis and database management. They enable calculations across a specific set of rows, known as a "window," while retaining the individual rows in the dataset. Unlike traditional aggregate functions that summarize data for the entire group, win
    7 min read
  • Window Functions in PL/SQL
    In Oracle PL/SQL, analyzing and managing complex data relationships often involves performing calculations across sets of rows. This is where window functions, sometimes referred to as "Analytic functions," come into play. They enable powerful data analysis, such as sales forecasting, time-series an
    7 min read
  • SUM() Function in SQL Server
    The SUM() function in SQL Server is an essential aggregate function used to calculate the total sum of values in a numeric column. It aggregates data by summing up all values in the specified column for the rows that match the criteria of the query. In this article, We will learn about SUM() Functio
    3 min read
  • RANK() Function in SQL Server
    The RANK() function is a powerful window function in SQL Server used to assign a rank to each row within a result set. It is particularly useful when we need to assign a rank to a group of rows based on some sorting criteria and want to differentiate between rows that have the same values. Unlike ot
    5 min read
  • Find the Length of a Series Using Window Functions in SQL Server
    Window functions in SQL Server are a powerful tool for performing calculations across a series of rows related to the current row. Unlike aggregate functions, which combine rows into a single result, SQL Server window functions allow for row-by-row analysis while maintaining the entire dataset. This
    5 min read
  • SQL Server Row_Number Function With PARTITION BY
    The row number function is one of the window functions used for assigning the row number to the rows in each of the partitions in the order in which they were sorted in the partition using the PARTITION clause, PARTITION only the ORDER clause can be used inside the OVER clause in such case the whole
    5 min read
  • SET ROWCOUNT Function in SQL Server
    The ROWCOUNT Set Function causes the server to stop the query processing after the specified number of records is returned. One may limit the number of records returned by all subsequent SELECT statements within the session by using the keyword SET ROWCOUNT. Or we can say that this function causes T
    2 min read
  • Postgre Window Functions
    PostgreSQL is an advanced relational database management system, popular for its ability to handle both SQL (structured) and JSON (non-relational) queries. One of its most powerful features is window functions, which allow for complex data analysis across rows without collapsing data into a single r
    6 min read
  • GROUPING ID Function in SQL Server
    SQL Server is a Relational Database Management System that is used to create and manipulate the database. It provides advanced security measures like encryption, access control, and auditing to protect sensitive data from unauthorized access. It Supports a wide range of data types, including structu
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences