EXTRACT function for date parts in PostgreSQL - Time & Space Complexity
We want to understand how the time it takes to get parts of a date grows as we work with more data.
How does the EXTRACT function behave when used on many rows?
Analyze the time complexity of the following code snippet.
SELECT EXTRACT(YEAR FROM order_date) AS order_year
FROM orders;
This code gets the year part from the order_date column for every row in the orders table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Extracting the year from each date in the orders table.
- How many times: Once for every row in the orders table.
Each row requires one extraction operation, so the total work grows as the number of rows grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 extractions |
| 100 | 100 extractions |
| 1000 | 1000 extractions |
Pattern observation: The work grows directly with the number of rows; doubling rows doubles work.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the number of rows increases.
[X] Wrong: "EXTRACT runs in constant time no matter how many rows there are."
[OK] Correct: EXTRACT runs once per row, so more rows mean more work and more time.
Understanding how functions like EXTRACT scale helps you write efficient queries and explain their performance clearly.
"What if we added a WHERE clause to filter rows before extracting? How would the time complexity change?"
Practice
EXTRACT function do in PostgreSQL when used with a date or timestamp?Solution
Step 1: Understand the purpose of EXTRACT
The EXTRACT function is designed to pull out specific parts such as year, month, day, hour, etc., from a date or timestamp value.Step 2: Compare with other options
Options B, C, and D describe different operations like formatting, adding days, or truncating time, which EXTRACT does not do.Final Answer:
It retrieves a specific part like year, month, or day from the date or timestamp. -> Option CQuick Check:
EXTRACT returns date parts = A [OK]
- Thinking EXTRACT changes date format
- Confusing EXTRACT with date arithmetic
- Assuming EXTRACT removes time part
created_at?Solution
Step 1: Recall correct EXTRACT syntax
The correct syntax is EXTRACT(part FROM source), so to get month: EXTRACT(MONTH FROM created_at);Step 2: Identify incorrect syntax forms
Options A, B, and D use wrong argument order, quotes around part name, missing FROM keyword, or wrong punctuation.Final Answer:
EXTRACT(MONTH FROM created_at); -> Option BQuick Check:
EXTRACT(part FROM source) = C [OK]
- Omitting FROM keyword
- Using quotes around part name
- Swapping argument order
SELECT EXTRACT(DAY FROM TIMESTAMP '2024-06-15 10:30:00');
What will be the output?
Solution
Step 1: Identify the part extracted
The query extracts the DAY part from the timestamp '2024-06-15 10:30:00'. The day is 15.Step 2: Confirm output value
Other options represent month (6), year (2024), and hour (10), which are not extracted here.Final Answer:
15 -> Option DQuick Check:
EXTRACT(DAY) = 15 [OK]
- Confusing day with month or year
- Expecting full date output
- Ignoring time part in timestamp
SELECT EXTRACT('YEAR' FROM CURRENT_DATE);Solution
Step 1: Check syntax for EXTRACT part
The part name like YEAR should be unquoted. Quotes cause syntax error.Step 2: Verify other parts of query
CURRENT_DATE is valid input, no parentheses needed, and EXTRACT uses FROM keyword, not comma.Final Answer:
The part name YEAR should not be in quotes. -> Option AQuick Check:
Part names unquoted in EXTRACT [OK]
- Using quotes around part names
- Adding commas instead of FROM
- Thinking CURRENT_DATE needs parentheses
orders where the order was placed in the first quarter (January to March) of any year. Which query correctly uses EXTRACT to filter these records?Solution
Step 1: Understand quarter extraction
PostgreSQL supports EXTRACT(QUARTER FROM date) which returns 1 for Jan-Mar, 2 for Apr-Jun, etc.Step 2: Evaluate each option
Options A (missing FROM), B (quotes around MONTH), and C (comma instead of FROM) have syntax errors. SELECT * FROM orders WHERE EXTRACT(QUARTER FROM order_date) = 1; correctly uses EXTRACT(QUARTER FROM order_date) = 1.Final Answer:
SELECT * FROM orders WHERE EXTRACT(QUARTER FROM order_date) = 1; -> Option AQuick Check:
Use EXTRACT(QUARTER) = 1 for first quarter [OK]
- Missing FROM keyword
- Using quotes around part names
- Using comma instead of FROM
- Not knowing EXTRACT supports QUARTER
Ready to prove it?
Pass the test and this pattern moves unknown
