Knowledge base / SQL
SECOND in SQL: components, fractions and event buckets
I show the syntax for four engines and explain why the same second within a minute does not establish the same moment or a duplicate event.

I want to extract 45 from the time 12:34:45. In MySQL I use SECOND; in PostgreSQL I use EXTRACT. This small operation is easily confused with calculating a duration or identifying an exact event time. I first establish which information the task needs.
The examples use synthetic times and explicitly labelled dialects. Syntax and expected results follow vendor documentation. A form accepted by one engine is not automatically universal SQL.
MySQL provides SECOND as a standalone function
MySQL 8.4 provides SECOND, returning an integer second component from zero to fifty-nine. NULL input produces NULL. It does not return the entire time expressed in seconds.
-- MySQL 8.4
SELECT
SECOND('2026-11-19 12:34:45.250000') AS second_part,
EXTRACT(SECOND FROM '2026-11-19 12:34:45.250000') AS extracted_part;
Both columns have the value 45. The SECOND field does not return the fractional part here. Analysing microseconds requires a suitable operation and an input type with sufficient precision. The appearance of fractional digits in input text does not establish the output precision.
SECOND is therefore a standalone MySQL function, rather than a name that works only “inside EXTRACT”. EXTRACT has its own syntax involving a field name.
PostgreSQL retains fractional seconds
In PostgreSQL 18, EXTRACT returns numeric, and the SECOND field includes fractional seconds. I use an explicit timestamp without time zone:
-- PostgreSQL 18
SELECT EXTRACT(
SECOND FROM TIMESTAMP '2026-11-19 12:34:45.250000'
) AS second_part;
The value is 45.25; a client may display it as 45.250000. Those represent the same number. Comparing this result with 45 will not produce true. To select any moment in the forty-fifth second of a minute, I need a range from 45 inclusive to 46 exclusive or an explicit decision to truncate the fraction.
The input type matters. A date without a time cannot supply the SECOND field for this extraction in PostgreSQL. These examples also make no assumptions about time zones in real logs. A reporting task needs an agreed type and meaning for recorded times.
SQL Server and Oracle use their own forms
In SQL Server 2022, I use DATEPART. An explicit datetime2 establishes the input type and precision:
-- SQL Server 2022
SELECT DATEPART(
second,
CAST('2026-11-19T12:34:45.250' AS datetime2(3))
) AS second_part;
The result is integer 45. DATEPART has separate fields for milliseconds and smaller units; choosing second does not preserve the whole fraction. Arbitrarily formatted date text can introduce a parsing problem before extraction even begins.
In Oracle Database 19c, I can use EXTRACT with a timestamp:
-- Oracle Database 19c
SELECT EXTRACT(
SECOND FROM TIMESTAMP '2026-11-19 12:34:45'
) AS second_part
FROM dual;
This whole-second example returns 45. Oracle requires an appropriate temporal input type. EXTRACT treats DATE as an ANSI date without time fields, so I do not substitute a plain DATE for the timestamp. Its documentation specifies valid input types for each field.
The second component repeats every minute
Imagine three events for user 17 on the same day: 12:00:45.100, 12:01:45.900 and 12:01:45.950. All fall within the forty-fifth second of their respective minutes. The first event nevertheless belongs to an earlier minute.
Filtering by the second component and grouping only by user would count three events. That does not establish three copies of one login. Even two events within the same second can represent two legitimate operations.
For full-second time buckets, I use date_trunc in PostgreSQL. It retains the date, hour and minute while discarding the finer part:
-- PostgreSQL 18
WITH events(user_id, event_time) AS (
VALUES
(17, TIMESTAMP '2026-11-19 12:00:45.100'),
(17, TIMESTAMP '2026-11-19 12:01:45.900'),
(17, TIMESTAMP '2026-11-19 12:01:45.950')
)
SELECT
user_id,
date_trunc('second', event_time) AS second_bucket,
COUNT(*) AS events_count
FROM events
GROUP BY user_id, date_trunc('second', event_time)
ORDER BY user_id, second_bucket;
| user_id | second_bucket | events_count |
|---|---|---|
| 17 | 2026-11-19 12:00:45 | 1 |
| 17 | 2026-11-19 12:01:45 | 2 |
There are two buckets. The second contains two operations within the same full second, even though their fractional timestamps differ. Identifying duplicates still requires an event definition, a key and knowledge of retries. Grouping provides something to investigate rather than an automatic deletion rule.

Calculate duration from the entire interval
An operation starts at 12:00:59 and finishes at 12:01:01. Its duration is two seconds. Subtracting the second fields would give one minus fifty-nine, or negative fifty-eight. The transition to another minute has been discarded.
For this simple PostgreSQL interval, I subtract complete timestamps and extract EPOCH:
-- PostgreSQL 18
SELECT EXTRACT(EPOCH FROM (
TIMESTAMP '2026-11-19 12:01:01'
- TIMESTAMP '2026-11-19 12:00:59'
)) AS elapsed_seconds;
The value is 2. This example does not address calendar intervals containing months or local times from different zones. It demonstrates the distinction between a clock component and the length of an interval. See DATEDIFF and time differences for a related discussion.
A short exercise before your own report
Add an event at 12:02:45.200. A third bucket should appear, although the integer second component remains forty-five. Then change that event to 12:02:46.200 and explain the difference between filtering a component and filtering a full time interval.
Before choosing a function, establish the engine, input type and information needed: a position within a minute, a full time bucket or an elapsed duration. For another recurring component, read the MINUTE entry.
Key takeaways
Does MySQL have a SECOND function?
Yes. MySQL 8.4 supports SECOND(time_value), returning a second component from 0 to 59. EXTRACT(SECOND FROM value) is another option.
Does EXTRACT(SECOND) return elapsed seconds?
No. It extracts the seconds field. In PostgreSQL, EXTRACT(EPOCH FROM interval) can obtain total seconds from a simple elapsed interval.
Does grouping by second components detect duplicates?
No. It combines recurring positions from different minutes. Grouping by full timestamps truncated to seconds identifies a shared time bucket but still does not prove a business duplicate.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
