213k views
4 votes
Write a SELECT statement that returns these columns from the Orders table: The order_id column The order_date column A column named approx_ship_date that’s calculated by adding 5 days to the order_date column The ship_date column A column named days_to_ship that shows the number of days between the order date and the ship date When you have this working, add a WHERE clause that retrieves just the orders for March 2018.

1 Answer

3 votes

Answer:

SELECT order_id, order_date,

DATEADD(DAY,5,order_date) AS approx_ship_date,

ship_date,

DATEDIFF(DAY,ship_date,DATEADD(DAY,2,order_date)) AS days_to_ship

FROM Orders

WHERE YEAR(order_date) = 2018 AND MONTH(order_date) = 3

Step-by-step explanation:

The first line of the SQL statement is a SELECT statement which selects order_id, order_date and ship_date columns from Orders table.

The DATEADD() is used to add date and here it is used to add 5 days to order_date column and the resultant column is named as approx_ship_date using ALIAS.

DATEDIFF() function is used to return the difference between two dates and here it shows number of days between order_date and ship_date columns.

WHERE clause is used to retrieve orders from March 2018. YEAR function represents the year of order_date which is set as 2018 to retrieve the orders for 2018. MONTH function represents the month of order_date which is set to 3 which means March in order to retrieve the orders for March.

User Shrinidhisondur
by
6.4k points