Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How do you select min date sql ?

Asked by Dan Peters Aug 29, 2023 6.8K views 4 answers
Share

About this question

 While trying select the min date SQL I am selecting records from a data extension that has an email address appearing more than one time but with a different date, using the below query:

It is showing the correct record, 127 records out of 403, and it is giving other columns like position, slot, and client, which I need to pull through. However, adding these to SELECT and GROUP BY clauses pulls all 403 records instead of 127. How to fix this?

Your answer

4 Answers

Edwardstalamantez Latest answer

Answered on Jun 9, 2026

@survival race : What a clear and practical explanation this SQL example made understanding how to select the minimum date much easier!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on Jan 29, 2025

If you're trying to find the minimum date in SQL, it’s actually pretty simple! Here’s how you can do it, along with some variations depending on what exactly you need:


1. Basic Query to Get the Minimum Date

If you just need the earliest date in a table, use the MIN() function:

  SELECT MIN(order_date) AS earliest_date FROM orders;

  • This will return the smallest (earliest) order_date from the orders table.

2. Getting the Minimum Date for Each Group

If you want the earliest date per category, use GROUP BY:

SELECT customer_id, MIN(order_date) AS first_order_date  
FROM orders  
GROUP BY customer_id;

  • This finds the first order date for each customer.

3. Finding the Entire Row with the Earliest Date

Sometimes, you need the full row where the earliest date occurs. Here’s one way to do it:

SELECT * FROM orders  
WHERE order_date = (SELECT MIN(order_date) FROM orders);

  • This returns all details for the order with the earliest date.

4. Using ORDER BY to Get the Earliest Date

Another approach is sorting and limiting results:

SELECT * FROM orders  
ORDER BY order_date ASC  
LIMIT 1;  -- Works in MySQL, PostgreSQL

In SQL Server, use:

  SELECT TOP 1 * FROM orders ORDER BY order_date ASC;

5. Handling NULL Values

  • If your date column has NULL values, MIN() will ignore them by default.
  • If you want to include them, use:

  SELECT COALESCE(MIN(order_date), '1900-01-01') AS earliest_date FROM orders;

This replaces NULL with a default date.

Final Thoughts

Using MIN() is the easiest way to get the earliest date in SQL. But depending on what you need, you might have to use GROUP BY, ORDER BY, or subqueries. Let me know if you need more help!

Was this helpful?

Ranjana Admin JanBask Expert

Answered on May 8, 2024

To select the minimum date from a SQL database table, you can use the MIN() function along with the appropriate column that holds the dates. Here's a simple example assuming you have a table called your_table with a column named

 date_column:SELECT MIN(date_column) AS min_date
FROM your_table;

This query will return the minimum (earliest) date stored in the date_column. The MIN() function will find the smallest date value in that column, and AS min_date gives the result column an alias for easy reference.

If you have time-specific requirements or need to filter based on certain conditions, you can add a WHERE clause:

SELECT MIN(date_column) AS min_date
FROM your_tableWHERE some_condition;

Replace some_condition with the condition you need to filter the rows. This condition can be based on any column in your table, depending on your specific requirements.



Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.