DATE data type: In PostgreSQL, stores date values without time. Created using DATE literal: DATE 'YYYY-MM-DD'.
Note: Format is always YYYY-MM-DD with hyphens as separators.
- Use the DATE literal: DATE 'YYYY-MM-DD'
- Follow the exact format: Year-Month-Day
- Store the result in a variable or use directly
SELECT DATE '2024-03-15' creates March 15, 2024.
Run the query to see the date result.
The result shows March 15, 2024 in the appropriate format.
Use DATE '2024-03-15' to create March 15, 2024.
• Literal format: Use DATE 'YYYY-MM-DD' format
• Hyphen separator: Separate year, month, day with hyphens
• Zero-padding: Months and days should be two digits
• Practice Tip: Remember Y-M-D order for date literals
- SELECT DATE '2023-12-25'; -- Christmas 2023
- SELECT DATE '2024-01-01'; -- New Year 2024
- SELECT DATE '2024-10-31'; -- Halloween 2024
- Always use YYYY-MM-DD format for date literals
- Pad single-digit months and days with zeros
- PostgreSQL handles leap years automatically
Q: What if I use an invalid date?
A: PostgreSQL will return an error. For example, DATE '2024-02-30' fails because February doesn't have 30 days.
Q: Can I include time in DATE literal?
A: No, DATE literal only creates dates. Use TIMESTAMP for date and time.
Date comparison: Using comparison operators (<, >, =) to determine the relationship between two dates in PostgreSQL.
Note: PostgreSQL compares dates based on their internal representation.
- Create two DATE literals or expressions
- Use comparison operator (<, >, =) between them
- Evaluate the boolean result
SELECT DATE '2024-03-15' < DATE '2024-03-20';
PostgreSQL evaluates the comparison and returns TRUE/FALSE.
Since March 15 is before March 20, the result is TRUE.
Yes, March 15, 2024 is earlier than March 20, 2024.
• Comparison operators: Use <, >, = to compare dates
• Boolean result: Comparison returns TRUE or FALSE
• Chronological order: Earlier dates are less than later dates
• Practice Tip: Test with known date relationships first
- SELECT DATE '2024-01-01' < DATE '2024-12-31'; -- returns TRUE
- SELECT DATE '2024-05-15' > DATE '2024-03-10'; -- returns TRUE
- SELECT DATE '2024-04-01' = DATE '2024-04-01'; -- returns TRUE
- < means "is earlier than", > means "is later than"
- PostgreSQL stores dates as numbers internally for comparison
- Time components matter in date comparisons if present
Q: What comparison operators can I use?
A: You can use all comparison operators: <, >, <=, >=, =, !=, <>
Q: Does time matter in date comparison?
A: No, DATE type only includes date components. For time, use TIMESTAMP.
Greater than (>): Operator that returns TRUE if the left operand is later in time than the right operand.
Note: For dates, "greater than" means chronologically later.
- Create two DATE literals
- Use the > operator between them
- Evaluate the boolean result
SELECT DATE '2024-06-10' > DATE '2024-03-15';
PostgreSQL evaluates the comparison and returns TRUE.
Since June 10 is after March 15, the result is TRUE.
Yes, June 10, 2024 is greater than March 15, 2024.
• Greater than: > operator returns TRUE if left date is later
• Chronological logic: Later dates are greater than earlier dates
• Boolean evaluation: Result is always TRUE or FALSE
• Practice Tip: Think of the number line for date ordering
- SELECT DATE '2024-12-31' > DATE '2024-01-01'; -- returns TRUE
- SELECT DATE '2024-08-15' > DATE '2024-08-10'; -- returns TRUE
- SELECT DATE '2024-02-01' > DATE '2024-12-31'; -- returns FALSE
- > means "comes later in time"
- Think of dates on a timeline from left to right
- Any date in June > any date in March of the same year
Q: What if I compare dates in different years?
A: The year is considered first, then month, then day. SELECT DATE '2023-12-31' < DATE '2024-01-01'; returns TRUE.
Q: Can I chain multiple comparisons?
A: Yes! You can use expressions like date1 > date2 AND date2 > date3.
Less than (<): Operator that returns TRUE if the left operand is earlier in time than the right operand.
Note: For dates, "less than" means chronologically earlier.
- Create two DATE literals
- Use the < operator between them
- Evaluate the boolean result
SELECT DATE '2024-01-01' < DATE '2024-02-14';
PostgreSQL evaluates the comparison and returns TRUE.
Since January 1 is before February 14, the result is TRUE.
Yes, January 1, 2024 is less than February 14, 2024.
• Less than: < operator returns TRUE if left date is earlier
• Chronological logic: Earlier dates are less than later dates
• Boolean evaluation: Result is always TRUE or FALSE
• Practice Tip: Remember: < means "earlier than"
- SELECT DATE '2024-01-01' < DATE '2024-12-31'; -- returns TRUE
- SELECT DATE '2024-05-01' < DATE '2024-05-15'; -- returns TRUE
- SELECT DATE '2024-10-31' < DATE '2024-09-15'; -- returns FALSE
- < means "comes earlier in time"
- Think of the calendar year from January to December
- Any date in January < any date in February of the same year
Q: What happens if I compare the same dates?
A: SELECT DATE '2024-05-01' < DATE '2024-05-01'; returns FALSE since they're equal.
Q: Can I use <= for "less than or equal"?
A: Yes! SELECT DATE '2024-05-01' <= DATE '2024-05-01'; returns TRUE.
Equality (=): Operator that returns TRUE if both operands represent the same date and time.
Note: For dates, equality means identical year, month, and day.
- Create two DATE literals
- Use the = operator between them
- Evaluate the boolean result
SELECT DATE '2024-03-15' = DATE '2024-03-15';
PostgreSQL evaluates the comparison and returns TRUE.
Since both dates are identical, the result is TRUE.
Yes, March 15, 2024 and March 15, 2024 are equal.
• Equality: = operator returns TRUE if dates are identical
• Exact match: Year, month, and day must all match
• Boolean evaluation: Result is always TRUE or FALSE
• Practice Tip: Use = to check for specific dates
- SELECT DATE '2024-12-25' = DATE '2024-12-25'; -- returns TRUE
- SELECT DATE '2024-01-01' = DATE '2023-01-01'; -- returns FALSE
- SELECT DATE '2024-07-04' = DATE '2024-07-04'; -- returns TRUE
- = checks for exact date match
- Year, month, and day must all be the same
- Use for validating specific dates like birthdays
Q: What if dates have different times?
A: If you include time components, they must also match for equality.
Q: Can I check inequality with !=?
A: Yes! SELECT DATE '2024-03-15' != DATE '2024-03-16'; returns TRUE.