Open In App

How to Fetch Data From Two Tables Based on Date in SQL?

Last Updated : 03 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In SQL, fetching data from multiple tables based on a specific date range is a common task. By using certain SQL operations, we can combine data from two different tables and filter the results based on a date condition. This method is particularly useful when working with multiple datasets and we need to analyze or report data within a certain time period.

In this article, we will explain how to fetch data from two tables based on date. We can perform the above function by performing the UNION operation on the tables. UNION operation appends the results of two queries and returns the result as a single set. 

What is the UNION Operation in SQL?

The UNION operation is used to combine the results of two queries. It appends the data from both queries and returns the results as a single result set. There are two variations of the UNION operation:

  • UNION ALL: Includes duplicates.
  • UNION: Excludes duplicates.

 Syntax:

SELECT * from table1 (query1)
UNION
SELECT * from table2 (query2);

Rules to Use UNION:

  • The number and the order of the columns must be the same in the queries.
  • The data types of the chosen fields must be the same.

Examples of Fetching Data From Two Tables Based on Date

For demonstration, let’s assume we have two tables in our Geek’s database:

demo_table1

demo_table2

Example 1: Fetch Data Based on Date Using UNION

In this example, we will fetch data from both tables where the DOB (Date of Birth) is between ‘1990-01-01‘ and ‘2000-01-01‘. The UNION operation is used to combine the results from both tables.

Query:

SELECT d1.NAME, d1.AGE, d1.DOB
FROM demo_table1 d1
WHERE DOB BETWEEN '1990-01-01' and '2000-01-01'
UNION
SELECT d2.NAME, d2.AGE, d2.DOB
FROM demo_table2 d2
WHERE DOB BETWEEN '1990-01-01' and '2000-01-01';

Output

Explanation:

  • The first query selects records from demo_table1 where the DOB falls between ‘1990-01-01‘ and ‘2000-01-01‘.
  • The second query selects records from demo_table2 with the same date condition.
  • The UNION operation combines the results from both queries and excludes duplicates.

Example 2: Using UNION ALL for Including Duplicates

If we want to include all records, even duplicates, use UNION ALL. This will return all matching records from both tables, regardless of whether they are duplicates.

Query:

SELECT d1.Name, d1.Age, d1.DOB
FROM demo_table1 d1
WHERE d1.DOB BETWEEN '1990-01-01' AND '2000-01-01'
UNION ALL
SELECT d2.Name, d2.Age, d2.DOB
FROM demo_table2 d2
WHERE d2.DOB BETWEEN '1990-01-01' AND '2000-01-01';

Output:

Name Age DOB
Nikhil 24 1990-05-03
Fanny 25 1996-07-08
Prem 30 2003-05-09
Preeti 21 2001-02-02
Samita 32 1994-07-23
Prem 30 2003-05-09

Explanation:

  • The UNION ALL operator does not filter out duplicates and returns all records that meet the specified condition.
  • This can be useful when you want to see all the occurrences of the records without excluding any repetitions.

Conclusion

Fetching data from two tables based on a date is a common task in SQL. By using the UNION or UNION ALL operation, we can easily combine results from different tables. The UNION operation is useful for excluding duplicates, while UNION ALL allows us to keep all records, including duplicates. Understanding these concepts ensures that we can handle and manipulate data efficiently in our SQL queries.



Next Article
Article Tags :

Similar Reads