SQL JOIN USING

A shorthand for ON — usable only when both tables happen to name their shared join column identically.

What & Why

USING (user_id) is a compact alternative to an equality ON clause when both inputs expose the same relationship column. growth.sessions and growth.events both contain user_id, so PostgreSQL can return one shared copy of that key.

See How It Works

BUSINESS QUESTION

Growth links sessions and events through their shared user_id column.

iduser_idstarted_atended_atpage_viewsdevice
50011012024-01-16 14:20:00+002024-01-16 14:52:00+008desktop
50021022024-02-10 11:10:00+002024-02-10 11:18:00+003mobile
50031032024-03-13 08:10:00+002024-03-13 09:04:00+0012desktop
50041042024-04-03 12:00:00+00NULL1tablet
50051052024-04-04 09:00:00+002024-04-04 09:21:00+007mobile
50061062024-04-05 10:15:00+002024-04-05 10:42:00+005tablet
50071072024-04-06 13:05:00+002024-04-06 13:38:00+0010desktop
iduser_idevent_namecreated_atproperties
1001101signup2024-01-15 09:12:00+00{"source":"organic"}
1002101activated2024-01-16 14:25:00+00{"step":"workspace"}
1003102signup2024-02-10 11:08:00+00{"source":"paid"}
1004103report_viewed2024-03-12 16:40:00+00{"report":"retention"}
EXAMPLE QUERY
SELECT
  user_id,
  s.id AS session_id,
  e.id AS event_id,
  e.event_name
FROM growth.sessions s
JOIN growth.events e USING (user_id)
ORDER BY user_id, session_id, event_id;
RESULT — exact output from the displayed Queryflo rows
user_idsession_idevent_idevent_name
10150011001signup
10150011002activated
10250021003signup
10350031004report_viewed

USING(user_id) produces the four real user-level session/event matches.

Now You Try

Practice this concept

Growth wants the first 100 session-event pairs joined through the same-named user_id column.

Available schema
growth

Prefix tables with growth.table_name.

user_idsession_idevent_idevent_name
growth.events
ColumnType
idbigint
user_idinteger
event_nametext
created_attimestamp with time zone
propertiesjsonb
deprecated_metrictext
growth.sessions
ColumnType
idbigint
user_idinteger
started_attimestamp with time zone
ended_attimestamp with time zone
page_viewsinteger
devicetext
internal_session_scoreinteger
query.sql
Intermediate business practice

Sign up free to try it on a real business scenario