Explore key SQL date and time handling concepts, including extracting components, formatting, arithmetic, and filtering, with practical examples inspired by Snowflake SQL usage. This quiz covers common interview topics for data analysts and engineers on manipulating dates and timestamps using SQL.
Which SQL function returns the current date in Snowflake, useful for filtering recent records?
Explanation: The CURRENT_DATE function provides the current date in SQL and is widely used for time-based filtering. The option NOW_DATE is not a recognized function; CUR_DATE is common in other systems but not in standard SQL or Snowflake. TODAY() is not a valid built-in function in Snowflake SQL.
What is the correct way to extract the year from a date column called signup_date in SQL?
Explanation: EXTRACT(YEAR FROM signup_date) is the correct syntax in SQL for extracting the year component. GETYEAR is not a valid function in standard or Snowflake SQL. YEAR with two arguments is incorrect syntax. PART() is not a supported function for this purpose.
Which function and arguments would you use to add 7 days to an order_date in SQL?
Explanation: DATEADD takes the unit (DAY), amount (7), and base date (order_date) as parameters in order. ADD_DAYS and DATEPLUS are not standard in Snowflake. PLUS_DAYS also does not follow standard SQL or Snowflake function names.
How would you subtract one month from the current date using SQL functions?
Explanation: The DATEADD function with 'MONTH' and a negative value (-1) subtracts one month from the date provided. SUB_MONTH and MINUS_MONTH are not valid functions, and MONTHS is not the correct unit name—it should be singular.
Which SQL function calculates the difference in days between shipped_date and delivered_date?
Explanation: DATEDIFF with 'DAY' specified calculates the number of days between two dates in SQL and Snowflake. DATE_DIFFERENCE and DAYS_BETWEEN might seem logical but are not actual function names in Snowflake. DATEDAY is not a standard function.
What is the correct syntax to convert the string '2024-11-01' into a date using the format 'YYYY-MM-DD'?
Explanation: TO_DATE with the string and its matching format is the correct way to parse dates from text in SQL. CAST_DATE, DATEFORMAT, and CONVERT_DATE are not recognized functions in Snowflake SQL for this operation.
How do you format a date column order_date to display as 'YYYY-MM' in SQL?
Explanation: TO_VARCHAR with the specified format is used for formatting dates as strings, such as 'YYYY-MM'. FORMAT_DATE, CONVERT, and SHOWDATE do not provide this functionality in Snowflake SQL.
Which SQL function helps group sales data by the beginning of each month from the order_date column?
Explanation: DATE_TRUNC with the 'MONTH' argument returns the first day of the month for a given date, making it ideal for grouping. MONTH_START, TRUNC_MONTH, and GROUP_BY_MONTH are not standard or recognized Snowflake SQL functions.
Which SQL condition filters records from the last 30 days based on order_date?
Explanation: Comparing order_date to the result of DATEADD(DAY, -30, CURRENT_DATE) accurately filters the last 30 days. DATE_SUB is not used in Snowflake. BETWEEN and LAST30DAYS() are incorrect in this context.
What is the correct way to extract the month from a signup_date column in SQL?
Explanation: EXTRACT(MONTH FROM signup_date) provides the numeric month. MONTHNAME returns the month's name if available, while GETMONTH and MONTHOFYEAR are not valid in Snowflake SQL.
How do you extract the day component from a signup_date in SQL?
Explanation: EXTRACT(DAY FROM signup_date) returns the day's number. DAYPART is not a standard function, DAYOFMONTH is unsupported in Snowflake SQL, and DAY_NAME typically returns a weekday name, not the day number.
Which SQL condition finds records where order_date is in the current year?
Explanation: Comparing the year part of order_date to the current year ensures records are from the same year. The other options use functions or keywords that are not part of standard or Snowflake SQL.
Which function returns both the current date and time in SQL, useful for timestamping events?
Explanation: CURRENT_TIMESTAMP gives the current date and time. NOW_DATE, DATE_TIME(), and TIME_NOW() are not valid functions in Snowflake SQL for this purpose.
Which SQL statement finds all sales records from the previous month based on today's date?
Explanation: Using DATEADD(MONTH, -1, CURRENT_DATE) gives the same day of last month. The provided range captures all days from last month up to but excluding today. The other options reference incorrect or unsupported functions and keywords.
What is the correct function to convert a timestamp value to a date value in SQL?
Explanation: CAST(timestamp_column AS DATE) safely converts timestamps, discarding the time portion. TIMESTAMP_TO_DATE, DATECAST, and CONVERT_DATEONLY are not standard functions in Snowflake for this operation.
How would you display a transaction_date as a three-letter month and four-digit year, like 'Jan-2024', in SQL?
Explanation: TO_VARCHAR with 'Mon-YYYY' formats the date as desired. FORMAT, DATE_TO_STRING, and MONTHYEAR_FORMAT are not valid formatting functions in Snowflake SQL.