# Users with first post within period

**URL:** https://meta.discourse.org/t/users-with-first-post-within-period/275155
**Category:** Data & reporting
**Tags:** sql-query
**Created:** [August 16, 2019, 7:53pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155 "2019-08-16T19:53:48Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![ThunderThighs](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thunderthighs/32/492278_2.png) [@ThunderThighs](https://meta.discourse.org/u/ThunderThighs)
#### Post date: [August 16, 2019, 7:53pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155/1 "2019-08-16T19:53:48Z")

</div>

Hi there. I’m a SQL newb. I would like to be able to pull a list of first time posters by username within a defined time period.

Note this is the first time they post, not joined. Our use case is different than most so post is important.

Does anyone have something like that? Appreciate any assistance!

THANK YOU!

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [August 16, 2019, 8:46pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155/2 "2019-08-16T20:46:19Z")

</div>

This should do the trick @ThunderThighs. Add it to your [Data Explorer](https://meta.discourse.org/t/32566?silent=true), fill in the start and end date, and you’re off to the races. Dates must match a supported format, one of which is `YYYY-MM-DD`.

### Users with first post within period

```sql
-- [params]
-- date :start_date
-- date :end_date

SELECT username
FROM users u
JOIN user_stats us
ON u.id = us.user_id
WHERE us.first_post_created_at BETWEEN :start_date::date AND :end_date::date

```

---

<div class="post-metadata">

### Author: ![rlaliberty](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rlaliberty/32/96258_2.png) [@rlaliberty](https://meta.discourse.org/u/rlaliberty)
#### Post date: [September 17, 2019, 7:09pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155/3 "2019-09-17T19:09:48Z")

</div>

This is super helpful.

How would you go about augmenting this to actually retrieve those posts?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [September 18, 2019, 8:22pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155/4 "2019-09-18T20:22:46Z")

</div>

Would something like this meet your needs?

```sql
-- [params]
-- date :start_date
-- date :end_date

SELECT u.id AS user_id, p.id AS post_id, p.created_at
FROM users u
JOIN user_stats us
ON u.id = us.user_id
JOIN posts p
ON u.id = p.user_id
WHERE p.created_at = us.first_post_created_at
AND us.first_post_created_at BETWEEN :start_date::date AND :end_date::date

```

---

<div class="post-metadata">

### Author: ![rlaliberty](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/rlaliberty/32/96258_2.png) [@rlaliberty](https://meta.discourse.org/u/rlaliberty)
#### Post date: [September 18, 2019, 10:32pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155/5 "2019-09-18T22:32:47Z")

</div>

That’s perfect. Thanks a ton.

Can I ask a bit of a SQL newbie question? What are the `u` and the `us` doing on lines two and three? Is it sort of functioning like AS where `users` can now be referred to as the prefix `u` and `user_stats` as the prefix `us`?

---

<div class="post-metadata">

### Author: ![tshenry](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tshenry/32/119495_2.png) [@tshenry](https://meta.discourse.org/u/tshenry)
#### Post date: [September 18, 2019, 10:56pm UTC](https://meta.discourse.org/t/users-with-first-post-within-period/275155/6 "2019-09-18T22:56:33Z")

</div>

No problem!

That’s correct. The `u` and `us` are table aliases. You can read more about them here: [http://www.postgresqltutorial.com/postgresql-alias/](http://www.postgresqltutorial.com/postgresql-alias/)

It’s essentially shorthand. It allows your to use `us.user_id` instead of `user_stats.user_id` to refer to the `user_id` column in the `user_stats` table. It’s particularly helpful with long table names and big queries.
