CEIL()

SQL · Math Functions

Theory

CEIL, short for ceiling, rounds a number up to the next whole number. CEIL(4.2) is 5, CEIL(4.8) is 5 and CEIL(5) stays 5. 'Up' means towards positive infinity, so for negatives CEIL(-4.8) is -4, not -5, which surprises many beginners.

CEIL is the right tool whenever a partial unit still counts as a whole one. If one box holds 6 items, an order of 14 items needs CEIL(14 / 6.0) = 3 boxes; if a page shows 10 rows, 95 results need CEIL(95 / 10.0) = 10 pages. Spelling varies slightly: PostgreSQL, MySQL, Oracle and Snowflake accept CEIL (and usually CEILING), while SQL Server only has CEILING. SQLite has ceil() and ceiling() when it is built with math functions; otherwise you can emulate it with a CASE on CAST(x AS INTEGER). As with ROUND, divide by a decimal (6.0) so integer division does not throw away the remainder first.

Why we use it

Real-world quantities often cannot be partial: you cannot ship 2.3 boxes or book 1.5 rooms. CEIL turns a fractional need into the whole number of units required, so you never under-count.

When to use it

Use CEIL for packing, paging, billing by started hour and capacity planning. Use FLOOR when only completed units count (full years of service), and ROUND when you want the nearest value.

How it works

  1. Input number, e.g. 14 / 6.0 = 2.33
  2. Is it already a whole number? Yes: Return it unchanged. No: Continue.
  3. Move up to the next integer towards +infinity
  4. Return 3 (and -4 for -4.8)

Syntax

CEIL(number)

Example

SELECT CEIL(4.2);
-- 5

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic