Back to Chapters
Chapter Progress: 0 %0 / 5 Lessons Read
‹›

1. Meet BigQuery: store big tables, ask with SQL

1.5 Working with dates

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      Almost every business question has a date in it: this month, the last 7 days, days since the order. BigQuery has a small set of date functions that cover most of them.

      • CURRENT_DATE(): today
      • DATE_TRUNC(d, MONTH): the first day of that month. Use it to group by month.
      • EXTRACT(YEAR FROM d): one part of the date
      • DATE_DIFF(a, b, DAY): how many days from b to a
      • DATE_SUB(d, INTERVAL 7 DAY): seven days earlier
      Must Know:

      Filter the partition column directly: order_date >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY). If you wrap the column itself in a function, BigQuery may not be able to skip partitions.

      Month start and day number

      Three January orders.

      SQL

      SELECT order_id,       order_date,       DATE_TRUNC(order_date, MONTH) AS month_start,       EXTRACT(DAY FROM order_date) AS dayFROM sales.orders;

      Rows at this step

      order_id | order_date | month_start | day
      ---------+------------+-------------+----
      101      | 2026-01-05 | 2026-01-01  | 5
      102      | 2026-01-06 | 2026-01-01  | 6
      103      | 2026-01-09 | 2026-01-01  | 9

      Quick check

      What is DATE_TRUNC(DATE '2026-03-17', MONTH)?

      The last 14 days

      Pretend today is 20 January 2026.

      SQL

      SELECT order_id,       DATE_DIFF(DATE '2026-01-20', order_date, DAY) AS days_agoFROM sales.ordersWHERE order_date >= DATE_SUB(DATE '2026-01-20', INTERVAL 14 DAY);

      Rows at this step

      order_id | days_ago
      ---------+---------
      102      | 14
      103      | 11
      104      | 8
      105      | 5
      106      | 2

      Quick check

      What is DATE_DIFF(DATE '2026-01-10', DATE '2026-01-07', DAY)?

      Try it yourself

      1. Run the first walkthrough on your sales.orders table.
      2. Group by DATE_TRUNC(order_date, MONTH) and count the orders of each month.

      Question 1 · Easy

      Which function gives today's date?

      Question 2 · Medium

      You want one row per month of sales. What do you group by?

      Question 3 · Hard

      orders is partitioned by order_date. Which filter lets BigQuery skip partitions most reliably?

      Key takeaways

      DATE_TRUNC groups dates by month, EXTRACT takes one part, DATE_DIFF counts days and DATE_SUB moves back in time. Compare the partition column directly.

      Lesson 5 of 90

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousWorking with dates
      Save your progress free for 1 month. Start free · Log in