Monday, 16 November 2026

Write a query to find the difference in days between two dates in the same table.

Let's create  table schema and sample data.

Table schema and sample data:-

CREATE TABLE orders (
  order_id INT PRIMARY KEY, 
order_date DATE NOT NULL, 
ship_date DATE 
); 

INSERT INTO orders (order_id, order_date, ship_date) VALUES 
(1, '2026-10-01', '2026-10-05'), 
(2, '2026-10-10', '2026-10-10'), 
(3, '2026-09-28', '2026-10-12'), 
(4, '2026-10-15', NULL), 
(5, '2026-10-20', '2026-10-18');

Query(MS SQL):-

SELECT 
order_id, 
order_date, 
ship_date, 
DATEDIFF(DAY, order_date, ship_date) AS days_to_ship 
FROM orders;

No comments:

Post a Comment

Note: only a member of this blog may post a comment.